Rithvickkr commited on
Commit
239a7af
·
1 Parent(s): 0c9db1d

DSATP integration embedded for Hugging Face Spaces

Browse files
Files changed (3) hide show
  1. app.py +78 -25
  2. dsatp/rules.yar +6 -0
  3. requirements.txt +5 -1
app.py CHANGED
@@ -1,42 +1,95 @@
1
  import gradio as gr
2
- from textblob import TextBlob
3
-
4
- # Placeholder DSATP function
5
- def mock_dsatp_scan(text: str) -> dict:
6
- """Mock vulnerability scan using sentiment analysis as a placeholder."""
7
- blob = TextBlob(text)
8
- sentiment = blob.sentiment
9
- return {
10
- "vulnerability_score": round(sentiment.polarity, 2),
11
- "details": "Positive sentiment suggests low risk; negative suggests high risk."
 
 
 
12
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Chatbot function
15
- def chatbot_response(user_input, history):
16
- """Basic chatbot response with mock DSATP integration."""
17
  if history is None:
18
- history = [] # Initialize history if None
19
- scan_result = mock_dsatp_scan(user_input)
20
- response = f"Security Analyst: Analyzed input. Vulnerability score: {scan_result['vulnerability_score']}. Details: {scan_result['details']}"
21
- # Append new message tuple to history
22
- updated_history = history + [(user_input, response)]
23
- return updated_history, scan_result # Return updated history and scan_result
24
-
25
- # Gradio interface
26
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  gr.Markdown("# AI Cybersecurity Agent")
28
  with gr.Row():
29
  with gr.Column():
30
  chatbot = gr.Chatbot(label="Security Analyst Chat")
31
- user_input = gr.Textbox(placeholder="Enter text to analyze (e.g., log data)...")
 
32
  submit_btn = gr.Button("Analyze")
33
  with gr.Column():
34
- gr.Markdown("### Placeholder for Network Graph and Charts")
35
  output_json = gr.JSON(label="Scan Results")
36
-
 
 
 
 
 
 
 
 
 
37
  submit_btn.click(
38
  fn=chatbot_response,
39
- inputs=[user_input, chatbot],
40
  outputs=[chatbot, output_json]
41
  )
42
 
 
1
  import gradio as gr
2
+ import yara
3
+ import os
4
+
5
+ # DSATP log parsing (embedded for Spaces)
6
+ def dsatp_parse_log(text: str) -> dict:
7
+ """Parse log for IoT threats."""
8
+ log = text.lower()
9
+ threats = {
10
+ "compromised": {"classification": "Threat Detected", "severity": "Critical", "mitigation": "Isolate process, run port scan"},
11
+ "unauthorized": {"classification": "Threat Detected", "severity": "High", "mitigation": "Quarantine MAC address"},
12
+ "high cpu": {"classification": "Threat Detected", "severity": "Medium", "mitigation": "Check for crypto-miner or DoS"},
13
+ "inbound traffic": {"classification": "Threat Detected", "severity": "Medium", "mitigation": "Block closed ports"},
14
+ "firmware mismatch": {"classification": "Threat Detected", "severity": "High", "mitigation": "Validate OTA or rollback"}
15
  }
16
+ for key, value in threats.items():
17
+ if key in log:
18
+ return {**value, "confidence": 0.9}
19
+ return {"classification": "No Threat", "severity": "Safe", "mitigation": "None", "confidence": 0.5}
20
+
21
+ # DSATP YARA scanning (embedded for Spaces)
22
+ def dsatp_yara_scan(file_path: str) -> dict:
23
+ """Scan file with YARA rules."""
24
+ try:
25
+ rules = yara.compile(filepath="dsatp/rules.yar")
26
+ matches = rules.match(file_path)
27
+ if matches:
28
+ return {
29
+ "classification": "Malware Detected",
30
+ "severity": "Critical",
31
+ "mitigation": "Quarantine file, run antivirus",
32
+ "confidence": 0.95
33
+ }
34
+ return {
35
+ "classification": "No Malware",
36
+ "severity": "Safe",
37
+ "mitigation": "None",
38
+ "confidence": 0.7
39
+ }
40
+ except Exception as e:
41
+ return {"error": str(e), "severity": "Unknown", "mitigation": "Check file format"}
42
 
43
  # Chatbot function
44
+ def chatbot_response(user_input, file, history):
45
+ """Process input or file with DSATP."""
46
  if history is None:
47
+ history = []
48
+ input_text = user_input
49
+ scan_result = None
50
+
51
+ if file:
52
+ input_text = open(file.name, "r").read()
53
+ scan_result = dsatp_yara_scan(file.name)
54
+ else:
55
+ scan_result = dsatp_parse_log(input_text)
56
+
57
+ response = f"Security Analyst: {scan_result['classification']}. Severity: {scan_result['severity']}. Mitigation: {scan_result['mitigation']}. Confidence: {scan_result['confidence']:.1f}"
58
+ updated_history = history + [(user_input or "File uploaded", response)]
59
+ return updated_history, scan_result
60
+
61
+ # Gradio interface with Tailwind-style threat visualizer
62
+ with gr.Blocks(css="""
63
+ .threat-card { background-color: #1f2937; color: white; padding: 16px; border-radius: 8px; margin-bottom: 16px; }
64
+ .severity-critical { background-color: #dc2626; }
65
+ .severity-high { background-color: #f59e0b; }
66
+ .severity-medium { background-color: #eab308; }
67
+ .severity-safe { background-color: #10b981; }
68
+ .severity-label { padding: 4px 8px; border-radius: 4px; }
69
+ """) as demo:
70
  gr.Markdown("# AI Cybersecurity Agent")
71
  with gr.Row():
72
  with gr.Column():
73
  chatbot = gr.Chatbot(label="Security Analyst Chat")
74
+ user_input = gr.Textbox(placeholder="Enter log data or alert (e.g., 'System compromised!')")
75
+ file_input = gr.File(label="Upload .txt/.log file", file_types=[".txt", ".log"])
76
  submit_btn = gr.Button("Analyze")
77
  with gr.Column():
78
+ gr.Markdown("### Threat Analysis Results")
79
  output_json = gr.JSON(label="Scan Results")
80
+ gr.Markdown("### Threat Visualizer")
81
+ gr.Markdown("""
82
+ <div class="threat-card" id="threat-card">
83
+ <p><strong>Classification:</strong> <span id="classification">{{classification}}</span></p>
84
+ <p><strong>Severity:</strong> <span class="severity-label {{severity_class}}" id="severity">{{severity}}</span></p>
85
+ <p><strong>Mitigation:</strong> <span id="mitigation">{{mitigation}}</span></p>
86
+ <p><strong>Confidence:</strong> <span id="confidence">{{confidence}}</span></p>
87
+ </div>
88
+ """, visible=True)
89
+
90
  submit_btn.click(
91
  fn=chatbot_response,
92
+ inputs=[user_input, file_input, chatbot],
93
  outputs=[chatbot, output_json]
94
  )
95
 
dsatp/rules.yar ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ rule MockMalware {
2
+ strings:
3
+ $a = "malware"
4
+ condition:
5
+ $a
6
+ }
requirements.txt CHANGED
@@ -1,2 +1,6 @@
1
  gradio[mcp]>=4.0.0
2
- textblob
 
 
 
 
 
1
  gradio[mcp]>=4.0.0
2
+ textblob
3
+ fastapi
4
+ uvicorn
5
+ yara-python
6
+ requests