-
扫描指定的代码项目目录
//支持只扫描指定的文件后缀比如.php 只扫描.php文件 也可以扫描全部的文件类型
def scan_directory(directory, file_types=None, scan_all=False):
try:
if scan_all:
files_to_scan = [os.path.join(root, file) for root, _, files in os.walk(directory) for file in files]
else:
files_to_scan = [os.path.join(root, file) for root, _, files in os.walk(directory) for file in files if
any(file.endswith(ft) for ft in file_types)]
# Saving results to file
scan_results = []
filename = f"scan_results.txt"
directory = "./"
filepath = os.path.join(directory, filename)
for file_path in tqdm(files_to_scan, desc="Scanning files"):
file_scan_results = scan_file(file_path, scan_results, directory)
if file_scan_results is not None and len(file_scan_results) > 0:
save_results_to_file(filepath, file_scan_results)
except Exception as e:
print(e)
2.代码文件切片发送给deepseek做安全审计
//从项目中的各个目录提取代码文件后,开始对代码进行切片发送给deepseek做安全审计
def scan_file(file_path, scan_results, directory):
try:
with open(file_path, 'r') as file:
content = file.readlines()
total_chunks = (len(content) - 1) // 100 + 100
file_scan_results = []
for chunk_start in range(0, len(content), 100):
chunk_end = min(chunk_start + 100, len(content))
code_chunk = ''.join(content[chunk_start:chunk_end])
response = analyze_security(code_chunk)
if hasattr(response, 'content'):
results = response.content
elif isinstance(response, dict) and 'content' in response:
results = response['content']
else:
results = response
if results:
# Split the result into individual issues using "@@@@", it can be unreliable depending on the output of the model
individual_results = results.split('@@@@')
for result in individual_results:
if "存在风险" in result:
try:
_, line_numbers, issue_description, code_snippet = result.split(' | ', 3)
adjusted_line_numbers = line_numbers.strip()
issue_description = issue_description.strip()
code_snippet = code_snippet.strip()
file_scan_results.append(
(file_path, adjusted_line_numbers, issue_description, code_snippet))
except ValueError:
continue
# Append this file's results to the main scan_results
# scan_results.extend(file_scan_results)
return file_scan_results
except Exception as e:
print(e)
return None
//严格定义prompt为资深安全专家实现代码安全审计
def analyze_security(content):
try:
completion = client.chat.completions.create(
model="deepseek-chat", # field is not currently used in LM studio
messages=[
{"role": "system", "content": '''你是一个安全专家严格分析以下代码片段,检查其中是否存在安全漏洞,请详细分析'''},
{"role": "user", "content": content}
],
temperature=0.7,
)
return completion.choices[0].message
except Exception as e:
print(e)
return None
//对项目中的所有代码进行安全审计
python scanner.py E:worksqli-secound-order --all
原文始发于微信公众号(网络安全技术点滴分享):DeepseekScanner deepseek+python实现代码审计实战
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论