
مكتبة Python للتحليل الأمني المدعوم بـ LLM محلي مع تحليل ثنائي Ghidra، وفحص ثغرات C/C++، وتكامل أدوات MCP لأتمتة الهندسة العكسية وإنشاء التقارير.
مكتبة TrustedSec للـ LLM هي مكتبة بايثون للتفاعل مع نماذج LLM المحلية التي تدعم استخدام الأدوات. وهي تتيح تشغيل سير عمل كبيرة لم تكن ممكنة تقليديًا إلا مع النماذج المتطورة (frontier models) من خلال الاستفادة من نقاط نهاية LLM المحلية وتكامل MCP (بروتوكول سياق النموذج).
# Clone or copy the repository
git clone https://github.com/trustedsec/ts_llmlib.git
cd ts_llmlib
# Install with pip
pip install -e .
انسخ دليل ts_llmlib/ إلى مشروعك:
cp -rf ts_llmlib /path/to/your/project/
from ts_llmlib import ChatSession
# Initialize with defaults (connects to http://localhost:1234/v1/chat/completions)
chat = ChatSession()
# Run a prompt
response = chat.run_prompt("What files are in the current directory?")
print(response['content'])
from ts_llmlib import ChatSession
# Configure with custom settings
chat = ChatSession(
system_prompt="You are a helpful assistant that uses file tools.",
tool_list=[], # Empty = use default file tools
mcp_servers={
"default": "http://localhost:3000/mcp"
},
llm_endpoint_url="http://localhost:1234/v1/chat/completions",
model_name="qwen3-coder-next",
timeout=60,
max_runtime=300
)
response = chat.run_prompt("Write 'hello' to /tmp/greeting.txt")
print(response['content'])
from ts_llmlib import ChatSession
chat = ChatSession()
history = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": "The answer is 4."}
]
response = chat.run_prompt("Can you write that to a file?", history=history)
| الأداة | المعاملات | الوصف |
|---|---|---|
read_local_file | path: str | قراءة محتويات ملف محلي |
write_local_file | path: str, content: str | كتابة محتوى إلى ملف محلي |
list_directory | path: str | سرد الملفات والدلائل في مسار |
يتيح MCP (بروتوكول سياق النموذج) التكامل مع الأدوات والخدمات الخارجية. عند تكوين خوادم MCP، ستقوم ts_llmlib بما يلي:
from ts_llmlib import ChatSession
chat = ChatSession(
mcp_servers={
"ghidraSvr": "http://localhost:8081/sse"
},
llm_endpoint_url="http://localhost:1234/v1/chat/completions",
model_name="qwen3-coder-next"
)
# The chat session will automatically fetch and integrate Ghidra tools
# such as list_methods, decompile_function, get_xrefs_to, etc.
تدعم ts_llmlib كلاً من نقاط النهاية بنمط RPC ونقاط نهاية SSE (أحداث يرسلها الخادم):
http://localhost:3000/mcphttp://localhost:3000/sse (يتم التحويل تلقائيًا إلى /mcp لاستدعاءات RPC)ChatSession(
system_prompt: str | None = None,
tool_list: list | None = None,
mcp_servers: dict[str, str] | None = None,
llm_endpoint_url: str = "http://localhost:1234/v1/chat/completions",
model_name: str = "default",
timeout: int = 60,
max_runtime: int = 300
)
المعاملات:
system_prompt (str | None): موجه النظام المخصص. القيمة الافتراضية هي موجه مساعد بسيط.tool_list (list | None): قائمة بتعريفات الأدوات المخصصة. القائمة الفارغة تستخدم الأدوات المدمجة.mcp_servers (dict[str, str] | None): قاموس يربط أسماء الخوادم بعناوين URL.llm_endpoint_url (str): عنوان URL لنقطة نهاية LLM API.model_name (str): معرّف النموذج لنقطة نهاية LLM.timeout (int): مهلة طلب HTTP بالثواني.max_runtime (int): أقصى وقت تنفيذ للموجه بالثواني.response = chat.run_prompt(
user_prompt: str,
conversation_history: list[dict] | None = None,
disable_tools: list[str] | None = None,
max_runtime: int | None = None
) -> dict
المعاملات:
user_prompt (str): رسالة المستخدم أو سؤاله.conversation_history (list[dict] | None): سجل محادثة اختياري على شكل قائمة من أزواج الدور/المحتوى.disable_tools (list[str] | None): قائمة بأسماء الأدوات التي سيتم تعطيلها لهذا الاستدعاء.max_runtime (int | None): تجاوز الحد الأقصى الافتراضي لوقت التشغيل لهذا الاستدعاء المحدد.القيمة المُرجعة:
{
"content": str, # LLM response text
"tool_calls": list, # List of tool calls made (if any)
"usage": dict | None, # Token usage if available from the LLM
"error": str | None # Error message if failed
}
يدير ToolRegistry جميع الأدوات المتاحة لجلسة الدردشة:
tool_listلتجاوز المسارات الافتراضية، يمكنك تعيين المتغيرات التالية، ويتم التحقق منها في ChatSession.
TS_LLM_MODEL=qwen3-coder-next TS_LLM_ENDPOINT=http://HOSTNAME:1234/v1/chat/completions
# Example usage
export TS_LLM_MODEL=qwen3-coder-next
export TS_LLM_ENDPOINT=http://HOSTNAME:1234/v1/chat/completions
ts_llmlib-redclippy
# OR
TS_LLM_MODEL=qwen3-coder-next TS_LLM_ENDPOINT=http://HOSTNAME:1234/v1/chat/completions ts_llmlib-redclippy
تحليل ملفات المصدر C/C++ بحثًا عن الثغرات الأمنية:
ts_llmlib-cpp-analyze <source_folder> <output_folder>
تحليل ثنائيات بالهندسة العكسية مع تكامل Ghidra:
# Basic analysis
ts_llmlib-ghidra-analyze <output_folder>
# Rename-only mode (first pass)
ts_llmlib-ghidra-analyze --rename_only <output_folder>
# Process only previously unnamed functions
ts_llmlib-ghidra-analyze --process_unnamed_only <output_folder>
# Grouped analysis for call relationship grouping
ts_llmlib-ghidra-analyze --grouped <output_folder>
بعد الانتهاء من التشغيل، إذا لم تكن تستخدم --rename_only، يمكنك تنظيف البنية بتشغيل:
ts_llmlib-ghidra-cleanup <input_folder> <output_folder>
توليد تقارير ثغرات منسقة من ملفات المراجعة JSON:
ts_llmlib-ghidra-report <review_folder>
تشغيل واجهة الدردشة RedClippy المبنية على Qt (لاحظ أن هذا المثال يتطلب pyside6):
ts_llmlib-redclippy
تتصل ts_llmlib بأي نقطة نهاية API متوافقة مع OpenAI. خوادم LLM المحلية الشائعة:
| الخادم | عنوان URL الافتراضي |
|---|---|
| Ollama | http://localhost:11434/v1/chat/completions |
| LM Studio | http://localhost:1234/v1/chat/completions |
| vLLM | http://localhost:8000/v1/chat/completions |
إعدادان للمهلة يتحكمان في التنفيذ:
timeout): أقصى وقت لطلب API واحدmax_runtime): إجمالي الوقت الفعلي المسموح به لمعالجة الموجه (بما في ذلك استدعاءات الأدوات)إذا تم تجاوز أي من الحدين، فسيحتوي الرد على رسالة خطأ.
تُعاد جميع الأخطاء داخل قاموس الاستجابة:
response = chat.run_prompt("Some prompt")
if response.get('error'):
print(f"Error: {response['error']}")
else:
print(response['content'])
max_runtimets_llmlib/
├── __init__.py # Package initialization, exports ChatSession
├── client.py # LLMClient for HTTP requests to LLM endpoints
├── chat.py # ChatSession class (main API)
├── mcp.py # MCPClient for Model Context Protocol integration
├── tools.py # ToolRegistry for tool management
├── HOW_TO_TS_LLMLIB.md # Original documentation
└── examples/ # Example scripts
├── c_cpp_analyze.py # C/C++ vulnerability analysis script
├── redclippy.py # Qt-based GUI chat application
├── ghidra_analyze.py # Ghidra binary analysis with MCP integration
├── ghidra_vuln_report.py # Vulnerability report generator
├── ghidra_cleanup.py # Output file reorganization utility
└── example_ts_llmlib.py # Example script demonstrating library usage
pyproject.toml # Modern Python package configuration (scripts defined here)
LICENSE.txt # BSD-3-Clause License
README.md # This file
رخصة BSD-3-Clause - راجع ملف LICENSE.txt للحصول على التفاصيل.
المساهمات مرحب بها! لا تتردد في تقديم طلب سحب (Pull Request).
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)