feat: (UNFINISH) 正则匹配检测测试
This commit is contained in:
parent
da9b2b52ac
commit
1a71a72ddf
@ -12,7 +12,7 @@ def find_dangerous_functions(
|
|||||||
r"\bexec\(": "high",
|
r"\bexec\(": "high",
|
||||||
r"\bpopen\(": "medium",
|
r"\bpopen\(": "medium",
|
||||||
r"\beval\(": "high",
|
r"\beval\(": "high",
|
||||||
r"\bsubprocess\.run\(": "medium",
|
r"\bsubprocess": "medium",
|
||||||
r"\b__getattribute__\(": "high",
|
r"\b__getattribute__\(": "high",
|
||||||
r"\bgetattr\(": "medium",
|
r"\bgetattr\(": "medium",
|
||||||
r"\b__import__\(": "high",
|
r"\b__import__\(": "high",
|
||||||
|
@ -4,7 +4,7 @@ import sys
|
|||||||
|
|
||||||
def read_file_content(file_path: str) -> str:
|
def read_file_content(file_path: str) -> str:
|
||||||
try:
|
try:
|
||||||
with open(file_path, "r", encoding="utf-8") as file:
|
with open(file_path, "r", encoding="utf-8",errors="ignore") as file:
|
||||||
return file.read()
|
return file.read()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print("Error: File not found.")
|
print("Error: File not found.")
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
from git import Repo # type: ignore
|
from git import Repo
|
||||||
import random
|
import random
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ def clone_repo(repo_url: str, clone_dir: str) -> None:
|
|||||||
|
|
||||||
def inject_random_backdoor(
|
def inject_random_backdoor(
|
||||||
path: str, pickle: bool = False, pyc: bool = False, sample_rate: float = 0.1
|
path: str, pickle: bool = False, pyc: bool = False, sample_rate: float = 0.1
|
||||||
) -> None:
|
) -> int:
|
||||||
"""
|
"""
|
||||||
Insert random backdoor into the path.
|
Insert random backdoor into the path.
|
||||||
|
|
||||||
@ -42,9 +42,10 @@ def inject_random_backdoor(
|
|||||||
# sample files to inject backdoor
|
# sample files to inject backdoor
|
||||||
if random.random() < sample_rate:
|
if random.random() < sample_rate:
|
||||||
injected_python_files.append(python_file)
|
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:
|
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()
|
lines = f.readlines()
|
||||||
total_lines = len(lines)
|
total_lines = len(lines)
|
||||||
inject_line_number = random.randint(0, total_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
|
# Write the modified content back to the file
|
||||||
f.writelines(lines)
|
f.writelines(lines)
|
||||||
|
return injectedNum
|
||||||
|
|
||||||
|
|
||||||
# 示例1: 通过 os.system 执行命令
|
# 示例1: 通过 os.system 执行命令
|
||||||
|
@ -1,10 +1,33 @@
|
|||||||
import unittest
|
import unittest
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
from detection.utils import read_file_content
|
||||||
|
from .final_tests_util import *
|
||||||
|
from detection.Regexdetection import find_dangerous_functions
|
||||||
|
from detection.GPTdetection import detectGPT
|
||||||
|
|
||||||
|
|
||||||
class TestFinalTests(unittest.TestCase):
|
class TestFinalTests(unittest.TestCase):
|
||||||
def setUp(self) -> None:
|
|
||||||
return super().setUp()
|
|
||||||
|
|
||||||
def test_final_tests(self):
|
def test_final_tests(self):
|
||||||
self.assertTrue(True)
|
shutil.rmtree("./tmp/repo", ignore_errors=True)
|
||||||
|
clone_repo("https://github.com/TheAlgorithms/Python.git", "./tmp/repo")
|
||||||
|
sampleRate = 0.1
|
||||||
|
injectedNum = inject_random_backdoor("./tmp/repo",sample_rate=sampleRate)
|
||||||
|
project_path = Path("./tmp/repo")
|
||||||
|
all_python_files = list(project_path.rglob("*.py"))
|
||||||
|
filesNum = len(all_python_files)
|
||||||
|
trueRate = injectedNum / filesNum
|
||||||
|
detectedNum = 0
|
||||||
|
for file in all_python_files:
|
||||||
|
content = read_file_content(str(file))
|
||||||
|
results = find_dangerous_functions(content, ".py")
|
||||||
|
if len(results["high"]) > 0 or len(results["medium"]) > 0 or len(results["low"]) > 0:
|
||||||
|
print(str(file))
|
||||||
|
detectedNum += 1
|
||||||
|
shutil.rmtree("./tmp/repo",ignore_errors=True)
|
||||||
|
self.assertAlmostEquals(detectedNum / filesNum, trueRate, places=1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user