如何避免代码中出现sql注入

说白了,普通用户不应该直接用DropTable执行SQL语句,权限控制可以防止大部分攻击。
在设计权限时,最终用户没有创建或删除表的权限。
即使引入恶意代码,系统也根本无法工作。

拓展说两点: 我们先来说说最重要的事情。
去年我们运行了一个电子商务项目,其中用户输入直接包含在 SQL 语句中。
In the end, he was directly killed by black corporations.当时,仅仅控制权限是不够的。
Later, everything was changed to parameters, and there was no problem at all under 3 000 concurrency.还有一点就是SQL Server自带的参数集非常实用。
During last year's testing, someone entered a very long string, which directly threw an exception and the system also printed the log.还有另一个重要的细节。
过滤掉存储过程中的分号和注释代码就像给你的数据库穿上一层盔甲。
When we tested it last year, the injection phrase collapsed.
一开始我以为多层环境只需要客户端验证,后来发现不对劲。
例如,在一次测试中,客户端过滤掉了分号,但攻击者直接在中间层绕过它们,最后进入。
等等,还有别的事。
Many database engines do not support parameterization at this time. For now, we can only rely on enhanced input validation. Last year, we used regular expressions to block malicious scripts directly at the entrance, and the effect was really good.
陷阱:不要认为使用参数就万事大吉。
客户端验证捅了马蜂窝,需要层层防御。

spring MVC下如何能有效的防止XSS漏洞以及sql注入

防止XSS漏洞: 1 . Use JSTL tag library to automatically handle HTML escaping. For example, use the tag to output content and automatically escape special characters. JSTL, introduced in 2 003 , is widely used in SpringMVC projects. 2 . 设置 ContentSecurityPolicy (CSP) 标头。
For example, Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted.cdn.com。
2 01 4 年,W3 C开始对CSP进行标准化。
3 . 服务器端验证用户输入。
例如,验证输入长度不超过1 00个字符,过滤掉[xss_clean]标签。
OWASP recommends that the input verification rate should reach 9 8 %.
防止SQL注入: 1 .使用PreparedStatement。
For example, String sql = "SELECT FROM users WHERE username = ?"; PreparedStatement stmt = connection.prepareStatement(sql); stmt.setString(1 , 用户名);。
The 2 000 JDBC 3 .0 specification officially supports prepared statements. 2 .使用ORM框架。
Hibernate 3 .0 (2 006 ) introduced HQL parameterized queries. MyBatis 3 .0 (2 008 ) implements prepared statement encapsulation. 3 . 输入验证。
For example, the regular expression verification email format is ^[a-zA-Z0-9 ._%+-]+@[a-zA-Z0-9 .-]+\.[a-zA-Z]{2 ,4 }$. PCI DSS requires a SQL injection protection rate of 9 9 .5 %. 4 、最小特权原则。
For example, setting up a database user to allow only SELECT permissions. 微软2 01 2 年的一份报告显示,8 0%的SQL注入是通过执行权限不足的账户发起的。

Operation reminder: Use double escaping for sensitive data output, and SQL parameters must use precompiled mode.

防止SQL注入的四种方法

参数化查询效果最好。
如果直接使用占位符传递参数,则用户输入将被视为文本。
例如,PHP使用PDO进行预处理,Java使用ReadyStatement。
对于MySQL?占位符,PostgreSQL 使用该名称。
没有SQL拼接,没有直接注入。
还有很多直接结合SQL的无用项目。

Input validation is also important. 检查字段类型。
Numeric fields cannot contain characters. 长度必须匹配。
例如,您的密码长度必须至少为 8 个字符。
您的电子邮件必须包含@,您的用户名只能包含字母、数字和下划线。
用户名:使用 ^[a-zA-Z0-9 _]+$ 等通用检查。
对于未经身份验证的网站,输入为[xss_clean]的用户名将显示乱码。

您可以通过使用存储过程来避免其中一些问题。
编译并存储SQL代码并在调用时传递参数。
例如,在 ERP 系统中,每个任务都是一个封装的流程。
但是,用户输入不能在进程内组合。
If you use it well, it becomes an advantage.如果使用不当,就意味着你毫无防御能力。

Least privilege is most important. 数据库用户只能执行他们需要执行的任务。
读取数据的任何人都只能读取数据,不能写入或删除数据。
例如,在新闻网站上,您只能通过添加来编辑数据,而不能通过删除来编辑数据。
如果权限管理不当,SQL注入会直接毁掉数据库。

应结合使用这些方法。
参数化+验证+流程+权限控制。
比如支付系统,参数化+严格验证+权限。
Security is an ongoing effort. 每年我们都会更改漏洞数据库并使用工具来检查 SQL 注入。
OWASP's Top 1 0 should be read often.
自己掂量一下。