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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from smolagents import InferenceClientModel, CodeAgent, MCPClient
|
| 3 |
+
|
| 4 |
+
# MCP Server URL for GitHub tools
|
| 5 |
+
MCP_SERVER_URL = "https://Baction-Vulnerability_Scanner_MCP_Server.hf.space/gradio_api/mcp/"
|
| 6 |
+
|
| 7 |
+
def parse_github_url(url):
|
| 8 |
+
"""Parse GitHub URL to extract owner, repo, and file path"""
|
| 9 |
+
import re
|
| 10 |
+
|
| 11 |
+
# Handle repository URLs
|
| 12 |
+
repo_pattern = r'https://github\.com/([^/]+)/([^/]+)/?$'
|
| 13 |
+
repo_match = re.match(repo_pattern, url.strip())
|
| 14 |
+
if repo_match:
|
| 15 |
+
return repo_match.group(1), repo_match.group(2), None
|
| 16 |
+
|
| 17 |
+
# Handle file URLs
|
| 18 |
+
file_pattern = r'https://github\.com/([^/]+)/([^/]+)/blob/[^/]+/(.+)$'
|
| 19 |
+
file_match = re.match(file_pattern, url.strip())
|
| 20 |
+
if file_match:
|
| 21 |
+
return file_match.group(1), file_match.group(2), file_match.group(3)
|
| 22 |
+
|
| 23 |
+
return None, None, None
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def analyze_vulnerabilities(message, history, hf_token):
|
| 27 |
+
"""Analyze GitHub repository or specific file for vulnerabilities using AI agent"""
|
| 28 |
+
|
| 29 |
+
# Validate HF token input
|
| 30 |
+
if not hf_token.strip():
|
| 31 |
+
return "β Please provide a Hugging Face API key. Get one from [Hugging Face](https://huggingface.co/settings/tokens)"
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
# Connect to MCP server
|
| 35 |
+
mcp_client = MCPClient({
|
| 36 |
+
"url": MCP_SERVER_URL,
|
| 37 |
+
"timeout": 120
|
| 38 |
+
})
|
| 39 |
+
tools = mcp_client.get_tools()
|
| 40 |
+
|
| 41 |
+
# Initialize AI model with user's token
|
| 42 |
+
model = InferenceClientModel(token=hf_token.strip())
|
| 43 |
+
|
| 44 |
+
# Create AI agent with GitHub MCP tools
|
| 45 |
+
agent = CodeAgent(
|
| 46 |
+
tools=[*tools],
|
| 47 |
+
model=model,
|
| 48 |
+
additional_authorized_imports=["json", "ast", "urllib", "base64", "re"],
|
| 49 |
+
max_steps=10
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Parse the GitHub URL
|
| 53 |
+
owner, repo, file_path = parse_github_url(message)
|
| 54 |
+
|
| 55 |
+
if not owner or not repo:
|
| 56 |
+
return "β Invalid GitHub URL. Please provide a valid GitHub repository or file URL."
|
| 57 |
+
|
| 58 |
+
# Generate different prompts based on whether it's a file or repository
|
| 59 |
+
if file_path:
|
| 60 |
+
enhanced_prompt = f"""
|
| 61 |
+
You are a cybersecurity expert. Analyze the specific GitHub file for security vulnerabilities.
|
| 62 |
+
|
| 63 |
+
GitHub URL: {message}
|
| 64 |
+
Repository: {owner}/{repo}
|
| 65 |
+
File Path: {file_path}
|
| 66 |
+
|
| 67 |
+
Please:
|
| 68 |
+
1. First, get repository information to verify it exists
|
| 69 |
+
2. Get the content of the specific file: {file_path}
|
| 70 |
+
3. Analyze the file content line by line for security vulnerabilities
|
| 71 |
+
4. Look for these security issues:
|
| 72 |
+
- Command injection: os.system, exec, eval calls
|
| 73 |
+
- Input validation: unvalidated user inputs
|
| 74 |
+
- Error handling: unhandled exceptions that could leak info
|
| 75 |
+
- Hardcoded secrets: API keys, passwords, tokens
|
| 76 |
+
- Unsafe operations: file operations without validation
|
| 77 |
+
|
| 78 |
+
5. Create a professional security report with:
|
| 79 |
+
- π File Overview (path, language, size)
|
| 80 |
+
- π Vulnerability Summary (counts by severity)
|
| 81 |
+
- π¨ Detailed Findings (line numbers, code snippets, impacts, fixes)
|
| 82 |
+
|
| 83 |
+
Use simple string operations and avoid complex regex patterns. Focus on clear, actionable security findings.
|
| 84 |
+
"""
|
| 85 |
+
else:
|
| 86 |
+
enhanced_prompt = f"""
|
| 87 |
+
You are a cybersecurity expert. Analyze the GitHub repository for security vulnerabilities.
|
| 88 |
+
|
| 89 |
+
Repository: {message}
|
| 90 |
+
|
| 91 |
+
Please:
|
| 92 |
+
1. First, get repository information to verify it exists
|
| 93 |
+
2. Scan the repository for code files (.py, .js, .ts, .php, .java, .cpp, .c, .cs, .go, .rb, .rs, .swift, .kt, .scala, .sh, .bash, .ps1, .ipynb, .sql, .xml, .yaml, .yml, .json, .config, .ini, .env)
|
| 94 |
+
3. For the first 5-10 most important code files, get their content and analyze for security issues
|
| 95 |
+
4. Look for these security vulnerabilities:
|
| 96 |
+
- Command injection: os.system, exec, eval calls
|
| 97 |
+
- Input validation: unvalidated user inputs, missing parameter checks
|
| 98 |
+
- Error handling: unhandled exceptions, information disclosure
|
| 99 |
+
- Hardcoded secrets: API keys, passwords, database credentials
|
| 100 |
+
- Unsafe operations: file operations, deserialization without validation
|
| 101 |
+
|
| 102 |
+
5. Generate a comprehensive security report with:
|
| 103 |
+
- π Repository Overview
|
| 104 |
+
- π Files Analyzed
|
| 105 |
+
- π Vulnerability Summary (counts by severity)
|
| 106 |
+
- π¨ Detailed Findings (file paths, line numbers, code snippets, impacts, remediation)
|
| 107 |
+
|
| 108 |
+
Use simple string operations and focus on the most critical security issues. Limit analysis to prevent timeouts.
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
# Run the AI agent analysis
|
| 112 |
+
result = agent.run(enhanced_prompt)
|
| 113 |
+
|
| 114 |
+
# Disconnect MCP client
|
| 115 |
+
mcp_client.disconnect()
|
| 116 |
+
|
| 117 |
+
return str(result)
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
return f"β Error analyzing repository: {str(e)}\n\nPlease ensure:\nβ’ Valid GitHub repository URL\nβ’ Hugging Face token is correct\nβ’ Repository is accessible"
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# Gradio UI
|
| 124 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 125 |
+
gr.Markdown("## π‘οΈ AI-Powered GitHub Vulnerability Scanner")
|
| 126 |
+
gr.Markdown("""
|
| 127 |
+
**Advanced Security Analysis Tool for GitHub Repositories**
|
| 128 |
+
|
| 129 |
+
This intelligent vulnerability scanner leverages cutting-edge AI agents and Model Context Protocol (MCP) tools to perform comprehensive security analysis of GitHub repositories and individual files.
|
| 130 |
+
|
| 131 |
+
**Key Features:**
|
| 132 |
+
- **Deep Code Analysis**: Scans for common security vulnerabilities including SQL injection, XSS, command injection, and more
|
| 133 |
+
- **AI-Powered Detection**: Uses advanced language models to understand code context and identify complex security issues
|
| 134 |
+
- **Repository & File Support**: Analyze entire repositories or focus on specific files
|
| 135 |
+
- **Detailed Reports**: Get comprehensive security reports with severity levels, line numbers, and remediation suggestions
|
| 136 |
+
- **Secure Processing**: Your API keys are used securely and never stored
|
| 137 |
+
|
| 138 |
+
**Project Links:**
|
| 139 |
+
- π **Source Code**: [GitHub Repository](https://github.com/banno-0720/vulnerability-scanner)
|
| 140 |
+
- π§ **MCP Server**: [Hugging Face Space](https://huggingface.co/spaces/HimanshuGoyal2004/github-mcp-server)
|
| 141 |
+
|
| 142 |
+
β οΈ **Important Notice**: This tool is designed for legitimate security research and vulnerability assessment purposes only. Do not use this scanner for malicious activities, unauthorized access, or any illegal purposes. Always ensure you have proper authorization before scanning repositories that don't belong to you.
|
| 143 |
+
""")
|
| 144 |
+
gr.Markdown("---")
|
| 145 |
+
|
| 146 |
+
# API Configuration Section
|
| 147 |
+
with gr.Row():
|
| 148 |
+
with gr.Column(scale=1):
|
| 149 |
+
gr.Markdown("### π API Configuration")
|
| 150 |
+
hf_token_box = gr.Textbox(
|
| 151 |
+
label="π€ Hugging Face API Key",
|
| 152 |
+
placeholder="Enter your Hugging Face API key for AI model access",
|
| 153 |
+
type="password",
|
| 154 |
+
info="π Get your free key: https://huggingface.co/settings/tokens"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
gr.Markdown("---")
|
| 158 |
+
gr.Markdown("### π¬ Security Analysis Chat")
|
| 159 |
+
gr.Markdown("Paste any GitHub repository or file URL below to start the security analysis.")
|
| 160 |
+
|
| 161 |
+
# Chatbot Interface
|
| 162 |
+
chatbot = gr.ChatInterface(
|
| 163 |
+
fn=lambda msg, hist, hf_token: analyze_vulnerabilities(msg, hist, hf_token),
|
| 164 |
+
additional_inputs=[hf_token_box],
|
| 165 |
+
type="messages",
|
| 166 |
+
examples=[
|
| 167 |
+
["https://github.com/ayushmittal62/vunreability_scanner_testing", ""],
|
| 168 |
+
["https://github.com/ayushmittal62/vunreability_scanner_testing/blob/master/database/schema.sql", ""],
|
| 169 |
+
["https://github.com/ayushmittal62/vunreability_scanner_testing/blob/master/python/database.py", ""]
|
| 170 |
+
],
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
if __name__ == "__main__":
|
| 174 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio[oauth,mcp]==5.45.0
|
| 2 |
+
fastapi==0.115.2
|
| 3 |
+
uvicorn==0.24.0
|
| 4 |
+
mcp==1.10.1
|
| 5 |
+
smolagents>=0.1.0
|
| 6 |
+
requests>=2.28.0
|
| 7 |
+
python-dotenv>=1.0.0
|
| 8 |
+
pydantic>=2.11,<2.12
|
| 9 |
+
smolagents[mcp]>=0.1.0
|