feat: (UNFINISH) 正则匹配检测测试

This commit is contained in:
2024-06-03 13:44:03 +08:00
parent da9b2b52ac
commit 1a71a72ddf
4 changed files with 35 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
from typing import Tuple
from git import Repo # type: ignore
from git import Repo
import random
from pathlib import Path
@@ -20,7 +20,7 @@ def clone_repo(repo_url: str, clone_dir: str) -> None:
def inject_random_backdoor(
path: str, pickle: bool = False, pyc: bool = False, sample_rate: float = 0.1
) -> None:
) -> int:
"""
Insert random backdoor into the path.
@@ -42,9 +42,10 @@ def inject_random_backdoor(
# sample files to inject backdoor
if random.random() < sample_rate:
injected_python_files.append(python_file)
injectedNum = len(injected_python_files)
print([str(i) for i in injected_python_files])
for python_file in injected_python_files:
with open(python_file, "r+") as f:
with open(python_file, "r+",errors="ignore") as f:
lines = f.readlines()
total_lines = len(lines)
inject_line_number = random.randint(0, total_lines)
@@ -57,6 +58,7 @@ def inject_random_backdoor(
# Write the modified content back to the file
f.writelines(lines)
return injectedNum
# 示例1: 通过 os.system 执行命令