Ⅳ. 기타

vul 폴더에 있는 patch 파일을 통해 취약한 코드 있는 여부 확인해보기

agencies 2024. 11. 25. 20:47
# Load and read the provided files to analyze the content
patch_file_path = "test.patch"
old_vul_file_path = "test_OLD.vul"

with open(patch_file_path, 'r') as patch_file:
    patch_content = patch_file.readlines()

with open(old_vul_file_path, 'r') as old_vul_file:
    old_vul_content = old_vul_file.readlines()

# Extract removed lines (starting with '-') from the patch file
removed_lines = [line[1:].strip() for line in patch_content if line.startswith('-')]

# Prepare to check if these lines exist in the old vulnerability file
old_vul_lines = [line.strip() for line in old_vul_content]

# Identify which removed lines exist in the OLD file
vulnerable_lines = [line for line in removed_lines if line in old_vul_lines]

# Print all removed lines with their status
print("=== Removed Lines Vulnerability Details ===")
for line in removed_lines:
    if line in vulnerable_lines:
        print(f"VULNERABLE: {line}")  # Highlight vulnerable lines
    else:
        print(f"SAFE: {line}")  # Show safe lines

# Print overall status
print("\n=== Overall Status ===")
if vulnerable_lines:
    print("Status: Vulnerable (At least one removed line exists in OLD file)")
else:
    print("Status: Safe (No removed lines exist in OLD file)")

# Save the results to a CSV file for further analysis
import pandas as pd

# Prepare data for saving
results_data = [{"Removed Line": line, "Status": "Vulnerable" if line in vulnerable_lines else "Safe"}
                for line in removed_lines]

# Save the details to a CSV file
results_df = pd.DataFrame(results_data)
results_df.to_csv("removed_lines_vulnerability_results.csv", index=False)
print("\nDetails saved to 'removed_lines_vulnerability_results.csv'.")