SQL injection is a technique that exploits a security vulnerability occurring in the database layer of an application . The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed. It is in fact an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another.
Google翻译如下(20090331追加):
SQL注入是一种技术,利用一个安全漏洞发生在数据库层的应用程序。该漏洞存在时,用户输入的是不是正确过滤字符串转义字符中嵌入SQL语句或用户输入不 强类型,从而出人意料地执行。事实上,它是一个实例一个更一般类别的安全漏洞,可能会发生时,一个编程或脚本语言是内嵌另一个。
SQL注入 攻 击 例子及 Statement和 PreparedStatement 的比 较
SQL注入 攻 击 是利用是指利用 设计 上的漏洞,在目 标 服 务 器上运行 Sql语 句以及 进 行其他方式的 攻 击 ,
动态 生成 Sql语 句 时 没有 对 用 户输 入的数据 进 行 验证 是 Sql注入 攻 击 得逞的主要原因。
对 于 JDBC而言, SQL注入 攻 击 只 对 Statement有效, 对 PreparedStatement 是无效的, 这 是因 为 PreparedStatement 不允 许 在不同的插入 时间 改 变查询 的 逻辑结 构。
如 验证 用 户 是否存在的 SQL语 句 为 :
select count(*) from usertable where name='用 户 名 ' and pswd='密 码 '
如果在 用 户 名字段 中 输 入 ' or '1'='1' or '1'='1
或是在 密 码 字段 中 输 入 1' or '1'='1
将 绕过验证 ,但 这种 手段只 对 只 对 Statement有效, 对 PreparedStatement 无效。
PreparedStatement 相 对 Statement有以下 优 点:
1.防注入 攻 击
2.多次运行速度快
3.防止 数据 库缓 冲区溢出
4.代 码 的可 读 性可 维护 性好
这 四点使得 PreparedStatement 成 为访问 数据 库 的 语 句 对 象的首 选 ,缺点是灵活性不 够 好,有些 场 合 还 是必 须 使用 Statement。
代 码 下 载 :
http://www.blogjava.net/Files/sitinspring/SQLInjection20081010002141.rar
posted on 2008-10-10 00:22 sitinspring 阅读 (1) 评论 (0) 编辑 收藏 所属分 类 : DB&SQL