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 文件获取未包含可疑特征的文件列表。")
在Linux环境下运行Python的几种方法
方法一:使用命令行在Linux命令行中,可以直接输入“python”命令来运行Python解释器。如果你的系统中同时安装了Python 2和Python 3,可以使用“python3”命令来运行Python 3。在命令行中输入以下命令即可:
python
或者
python3
这将启动Python解释器,并显示Python版本号和命令提示符“>>>”。在提示符下,你可以输入Python代码并按下Enter键来执行。
方法二:使用脚本文件
你可以将Python代码保存到一个脚本文件中,并在命令行中使用“python”命令来运行它。假设你的脚本文件名为“script.py”,在命令行中输入以下命令即可:
python script.py
或者
python3 script.py
这将执行脚本文件中的Python代码。
方法三:使用集成开发环境(IDE)
你可以在Linux上使用集成开发环境(IDE)来编写和运行Python代码。一些流行的Linux IDE包括PyCharm、Visual Studio Code和Atom等。这些IDE提供了更友好的界面和更多功能,例如自动补全、调试和代码管理等。你可以下载并安装这些IDE,并在其中打开Python文件并运行它们。
方法四:使用图形化界面工具
除了命令行和IDE外,还有一些基于图形化界面的工具可以帮助你在Linux上运行Python代码。例如,Thonny是一个简单易用的Python IDE,提供了一个直观的用户界面,适用于初学者和教育用途。你可以下载并安装Thonny,并在其中打开Python文件并运行它们。
无论你选择哪种方法,都可以在Linux环境下运行Python代码。对于初学者来说,建议从命令行开始入手,熟悉Python解释器和基本的命令行操作。一旦掌握了基本知识,可以尝试使用IDE或图形化界面工具来提高效率和方便性。