From SQL Injection to Shell (PostgreSQL edition)
环境配置
虚拟机1
- Debian x64
- NAT Network
虚拟机2
- Kali Linux
- NAT Network
修改 Kali 的 hosts 文件,添加如下内容:
1 | 虚拟机1的ip地址 vulnerable |

漏洞发现
打开 Web 站点发现好像和实验 从 SQL 注入到 Shell 的类似,从网页中可以看到有四个页面
- index.php
- cat.php
- all.php
- admin/login.php

同样 cat.php 页面存在 SQL 注入漏洞

admin 用户的图片上传文件夹可以直接访问,无需登录

漏洞利用
手工 SQL 注入
枚举字段数
枚举字段数量,暴露了数据库信息,使用 PostgreSQL 数据库,查询 pictures 表

枚举得到 4 个字段,但是注入后导致 SQL 语法错误

使用 null
进行枚举,结果如下,可以推测 1、4 字段是数字,而 2、3 字段是字符串




数据库信息
可以利用字符串类型的字段查看数据库信息:
数据库版本

当前查询使用的数据库

数据表信息
查看当前数据库的所有表,tablename
是 pg_tables 中的一个字段,

查看 information_schema 的字段信息

字段拼接查询,MySQL 中使用 concat
函数,PostgreSQL 中使用 ||
,这里使用 :
作为拼接的分隔符

查看 users 表的信息,把密码扔到 md5 在线破解,或者 使用 john
破解


sqlmap
获取目标数据库信息,sqlmap 同时给出检测到的 SQL 注入漏洞
1 | sqlmap http://vulnerable/cat.php?id=1 --banner |
获取所有数据库信息,得到 3 个数据库
1 | sqlmap http://vulnerable/cat.php?id=1 --dbs |
获取 public 数据库的数据表信息,得到 3 张表
1 | sqlmap http://vulnerable/cat.php?id=1 -D public --tables |
获取 users 表的字段,得到 3 个字段
1 | sqlmap http://vulnerable/cat.php?id=1 -D public -T users --columns |
获取 users 表的数据,--batch
表示使用默认值,不询问用户
- 尝试对密码爆破
1 | sqlmap http://vulnerable/cat.php?id=1 -D public -T users --dump --batch |
Webshell
登陆后台,可以管理上传的图片,直接上传 shell.php 失败,尝试修改文件后缀绕过,.php
、.php3
都失败
1 |
|

.htaccess
文件提供了一种基于目录进行配置更改的方法,其中可以配置 AddType
,由 Apache 的 mod_mime 模块提供支持,指明了如何解析特定类型的文件
首先上传一个 .htacess
文件,设置使用 PHP 解析 .blah
文件,然后将 shell.php 后缀改为 .blah
上传
- 这里能够上传
.htacess
文件进行配置覆盖是因为 Apache 的AllowOverride
选项配置为了All
,表示当遇到.htacess
文件就对其进行解析 - 通常不会过滤后缀为
.blah
的文件(常见文件后缀列表中没找到)
1 | AddType application/x-httpd-php .blah |

代码执行成功

参阅
- From SQL Injection to Shell: PostgreSQL edition
- 从 SQL 注入到 Shell
- 9.25. System Information Functions
- 51.90. pg_tables
- Chapter 36. The Information Schema
- 36.17. columns
- How do I list all columns for a specified table
- md5在线解密破解
- Apache HTTP Server Tutorial: .htaccess files
- What is “AddType” in .htaccess?
- Apache Module mod_mime
- List of filename extensions