[Python] 纯文本查看 复制代码
import os
import re
import concurrent.futures
def scan_directory(directory, log_file):
with open(log_file, 'w') as log:
file_paths = get_php_file_paths(directory)
with concurrent.futures.ThreadPoolExecutor() as executor:
results = {executor.submit(scan_file, file_path): file_path for file_path in file_paths}
for future in concurrent.futures.as_completed(results):
file_path = results[future]
has_webshell = future.result()
if has_webshell:
log.write(file_path + '\n')
def get_php_file_paths(directory):
file_paths = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.php'):
file_path = os.path.join(root, file)
file_paths.append(file_path)
return file_paths
def scan_file(file_path):
with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
contents = file.read()
# 添加其他可疑代码的正则表达式
webshell_patterns = [
r'\s*\$mix=',
r'\$shellname\s*=\s*\'[^\']*\'',
r'\$PHP Encode by\s*=\s*\'[^\']*\'',
# r'@set_time_limit\s*\(\s*0\s*\)',
r'function\s+Class_UC_key\s*\(\s*\$string\s*\)\s*{',
r'eval\s*\'[^\']*\'',
# r'base64_decode\s*\(',
# r'system\s*\(',
# r'exec\s*\(',
# r'shell_exec\s*\(',
# r'passthru\s*\(',
# r'proc_open\s*\(',
# r'popen\s*\(',
# r'assert\s*\(',
# r'require\s*\(',
# r'require_once\s*\(',
# r'include\s*\(',
# r'include_once\s*\(',
# r'chmod\s*\(',
# r'chown\s*\(',
# r'copy\s*\(',
# r'delete\s*\(',
# r'file_put_contents\s*\(',
# r'fopen\s*\(',
# r'fwrite\s*\(',
# r'move_uploaded_file\s*\(',
# r'mkdir\s*\(',
# r'rmdir\s*\(',
# r'unlink\s*\(',
# r'symlink\s*\(',
# r'mysql_query\s*\(',
# r'preg_match\s*\(',
# r'md5\s*\(',
# r'sha1\s*\(',
# r'curl_exec\s*\(',
# r'base_convert\s*\(',
# r'extract\s*\(',
# r'parse_str\s*\(',
# r'ini_set\s*\(',
]
for pattern in webshell_patterns:
regex = re.compile(pattern, re.IGNORECASE)
if regex.search(contents):
return True
return False
# 指定要扫描的目录和日志文件路径
directory_to_scan = '/www/wwwroot/'
log_file_path = 'log.txt'
# 扫描目录并将未包含可疑特征的文件路径写入日志文件
scan_directory(directory_to_scan, log_file_path)
print("扫描完成。请查看 log.txt 文件获取未包含可疑特征的文件列表。")