我刚开始在网上开发东西.到目前为止,我花了很多时间(50%左右)来尝试阻止坏人将sql注入等内容放入我的输入表单并验证服务器端.这是正常的吗?
问题
我们需要在我们的java应用程序中防御'WAITFOR DELAY'sql注入攻击.
背景
[这很长.跳到'解决方案?' 以下部分,如果你匆忙]
我们的应用程序主要使用预准备语句和可调用语句(存储过程)来访问数据库.
在一些地方,我们动态构建并执行查询以供选择.在此范例中,我们使用条件对象根据用户输入条件构建查询.例如,如果用户为first_name和last_name指定了值,则查询结果总是如下所示:
SELECT first_name,last_name FROM MEMBER WHERE first_name ='joe' AND last_name='frazier'
Run Code Online (Sandbox Code Playgroud)
(在这个例子中,用户会指定"joe"和"frazier"作为他/她的输入值.如果用户有更多或更少的批评,我们会有更长或更短的查询.我们发现这种方法比使用准备更容易声明,比存储过程更快/更高效.
攻击
漏洞审计报告了sql注入失败.攻击者为'last_name'参数注入'frazier WAITFOR DELAY '00:00:20'值,导致这个sql:
SELECT first_name,last_name FROM MEMBER WHERE first_name ='joe' AND last_name='frazier' WAITFOR DELAY '00:00:20'
Run Code Online (Sandbox Code Playgroud)
结果:查询成功执行,但执行时间为20秒.攻击者可以占用数据库池中的所有数据库连接,并有效地关闭您的站点.
关于这种'WAITFOR DELAY'攻击的一些观察
我曾经想过,因为我们使用了Statement executeQuery(String),所以我们可以安全地从sql注入.executeQuery(String)不会执行DML或DDL(删除或删除).并且在分号上执行Query(String)choke,因此'Bobby Tables'范例将失败(即用户输入'frazier; DROP TABLE成员'作为参数.参见http://xkcd.com/327/)
"WAITFOR"攻击在一个重要方面有所不同:WAITFOR修改现有的"SELECT"命令,而不是单独的命令.
攻击仅适用于生成的查询中的"最后一个参数".即'WAITFOR'必须出现在sql语句的最后
解决方案,廉价黑客还是两者
最明显的解决方案是简单地将"AND 1 = 1"添加到where子句中.
生成的sql立即失败并阻止攻击者:
SELECT first_name,last_name FROM MEMBER WHERE first_name ='joe' AND last_name='frazier' WAITFOR DELAY '00:00:20' AND 1=1
Run Code Online (Sandbox Code Playgroud)
问题
以下代码来自SAMATE Reference Dataset.我用它来测试静态分析工具.正如您所看到的,代码应该通过使用清理方法以及使用预准备语句来阻止SQL注入.
由于SCA工具无法知道自定义的santitzation方法,因此不会检测到该allowed方法用于防止注入.
public class SQLInjection_good_089 extends HttpServlet
{
private static final long serialVersionUID = 1L;
public SQLInjection_good_089()
{
super();
}
// Table of allowed names to use
final String allowed_names[] = { "Mickael", "Mary",
"Peter", "Laura", "John"};
// Function to check if the current name takes part of the allowed ones
public boolean allowed( String in )
{
boolean bool = false;
for( int i = 0; i < 5; i++ )
{
if( …Run Code Online (Sandbox Code Playgroud) java security static-analysis sql-injection prepared-statement
问题说这一切都有希望,如果我检查一个变量为is_numeric()返回true,是否可以直接放入MySQL查询,或者我是否需要应用标准转义?我在想零字符,溢出漏洞和东西.
一个模棱两可的例子是:
if(is_numeric($_GET['user_id'])) {
mysql_query("SELECT * FROM `users` WHERE id = ".$_GET['user_id']);
}
Run Code Online (Sandbox Code Playgroud)
MySQL中的数据类型是INT().
有人解释如果不包括cfqueryparam的cfsqltype仍然对SQL注入保护有用吗?以及cfqueryparam与cfsqltype和w/o cfsqltype实际发生的情况.
<!--- without cfsqltype--->
<cfqueryparam value="#someValue#">
<!--- with cfsqltype--->
<cfqueryparam value="#someValue#" cfsqltype="cf_sql_char">
Run Code Online (Sandbox Code Playgroud) 我希望使我的网站安全抵御SQL注入攻击.有没有人有任何良好的链接,以使网站在ASP.NET站点(c#,Web表单)中抵御这些类型的攻击?
编辑:
我应该指出我正在使用实体框架
我知道sql语句中参数的用法,但只是为了好奇,使用Format函数来防止sql注入而不是使用参数是安全的.
喜欢这个样本
sCustomer : string
begin
AdoSql.CommandText:=Format('Select SUM(value) result from invoices where customer=%s',[QuotedStr(sCustomer)]);
end;
Run Code Online (Sandbox Code Playgroud) 对于搜索功能,我编写了一个由PHP脚本执行的MySQL查询.我没有进行全文搜索.相反,我正在使用以下方法进行搜索:
... WHERE field LIKE '%etc%' AND field REGEXP '[[:<:]]etc[[:>:]]'
现在,我的想法是在PHP中准备这些动态值,例如:
$word = '2*3%5_1^0'; // just an example
$wordLike = strtr($word,array('\\'=>'\\\\','%'=>'\\%','_'=>'\\_'));
// instead of my old solution:
// $wordLike = preg_replace('~([%_])~', '\\\\$1', $word);
$wordLike = $db_con->escape('%' . $wordLike . '%');
$spaces = '[[:blank:]]|[[:punct:]]|[[:space:]]';
// I'm not sure about the difference between blank & space, though
$wordRX = preg_quote($word);
$wordRX = $db_con->escape('(^|'.$spaces.')'.$wordRX.'($|'.$spaces.')');
// instead of my old solution:
// $wordRX = $db_con->escape('[[:<:]]' . $wordRX . '[[:>:]]');
Run Code Online (Sandbox Code Playgroud)
然后使用这些值,如...
... WHERE field LIKE …
所以我有一个update_all查询,如下所示:
Task.where(...).update_all('position = position - X, parent_id = Y')
Run Code Online (Sandbox Code Playgroud)
我想用整数值替换X和Y. 而且我想安全地做到:"铁路方式".知道我怎么能做到这一点?
编辑:我的rails控制器中没有位置变量.如果X = 1,则最终查询应该包含"position = position-1".
此外,update_all文档指定此方法只接受一个参数:表示SQL语句的SET部分的字符串,数组或散列.
编辑2:好吧,我通过略微调整Arup Rakshit解决方案来实现它.这是最终的工作解决方案:
Task.update_all(['position = position - ?, parent_id = ?', X, Y])
Run Code Online (Sandbox Code Playgroud) 我一直在我的一台服务器上看到这个SQL注入/攻击,并想知道它想要做什么.我知道这是一次攻击,谷歌搜索后我发现它被大量使用,但我没有看到它的作用以及十六进制或二进制是什么的解释.这是攻击
press-detail.php?id=999999.9+%2f**%2fuNiOn%2f**%2faLl+%2f**%2fsElEcT+0x393133353134353632312e39,0x393133353134353632322e39,0x393133353134353632332e39,0x393133353134353632342e39,0x393133353134353632352e39,0x393133353134353632362e39,0x393133353134353632372e39,0x393133353134353632382e39,0x393133353134353632392e39,0x39313335313435363231302e39,0x39313335313435363231312e39,0x39313335313435363231322e39,0x39313335313435363231332e39,0x39313335313435363231342e39,0x39313335313435363231352e39,0x39313335313435363231362e39,0x39313335313435363231372e39,0x39313335313435363231382e39,0x39313335313435363231392e39,0x39313335313435363232302e39,0x39313335313435363232312e39,0x39313335313435363232322e39,0x39313335313435363232332e39,0x39313335313435363232342e39,0x39313335313435363232352e39,0x39313335313435363232362e39,0x39313335313435363232372e39,0x39313335313435363232382e39,0x39313335313435363232392e39,0x39313335313435363233302e39,0x39313335313435363233312e39,0x39313335313435363233322e39,0x39313335313435363233332e39,0x39313335313435363233342e39,0x39313335313435363233352e39,0x39313335313435363233362e39,0x39313335313435363233372e39,0x39313335313435363233382e39,0x39313335313435363233392e39,0x39313335313435363234302e39,0x39313335313435363234312e39,0x39313335313435363234322e39,0x39313335313435363234332e39,0x39313335313435363234342e39,0x39313335313435363234352e39,0x39313335313435363234362e39,0x39313335313435363234372e39,0x39313335313435363234382e39,0x39313335313435363234392e39,0x39313335313435363235302e39,0x39313335313435363235312e39,0x39313335313435363235322e39,0x39313335313435363235332e39,0x39313335313435363235342e39,0x39313335313435363235352e39,0x39313335313435363235362e39,0x39313335313435363235372e39,0x39313335313435363235382e39,0x39313335313435363235392e39,0x39313335313435363236302e39,0x39313335313435363236312e39,0x39313335313435363236322e39,0x39313335313435363236332e39,0x39313335313435363236342e39,0x39313335313435363236352e39,0x39313335313435363236362e39,0x39313335313435363236372e39,0x39313335313435363236382e39,0x39313335313435363236392e39,0x39313335313435363237302e39,0x39313335313435363237312e39,0x39313335313435363237322e39,0x39313335313435363237332e39+and+'1'='1
Run Code Online (Sandbox Code Playgroud)
我想知道为什么"999999.9",什么是"0x39313335313435363237322e39".当这种攻击有效时,黑客会对数据库进行哪些信息或编辑.最后这次攻击能不能一次又一次地降低/崩溃服务器?
对此代码的任何解释都将不胜感激.
sql-injection ×10
sql ×4
security ×3
escaping ×2
mysql ×2
php ×2
user-input ×2
asp.net ×1
c# ×1
coldfusion ×1
delphi ×1
java ×1
jdbc ×1
sql-server ×1
xss ×1