Spaces:
Runtime error
Runtime error
smellslikeml
commited on
Commit
•
8671d0d
1
Parent(s):
457d1ba
initial commit
Browse files- README.md +8 -0
- app.py +4 -0
- requirements.txt +5 -0
- threat_extraction.py +73 -0
- tool_config.json +6 -0
README.md
CHANGED
@@ -10,4 +10,12 @@ pinned: false
|
|
10 |
license: mit
|
11 |
---
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
10 |
license: mit
|
11 |
---
|
12 |
|
13 |
+
## Getting Started
|
14 |
+
|
15 |
+
Please configure your OpenAI API key as an environment variable:
|
16 |
+
```
|
17 |
+
export OPENAI_API_KEY="yout-key-here"
|
18 |
+
```
|
19 |
+
and update the `FirewallManager` and `PacketFilter` classes to perform your desired tasks.
|
20 |
+
|
21 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers.tools.base import launch_gradio_demo
|
2 |
+
from threat_extraction import ThreatIntelExtractorTool
|
3 |
+
|
4 |
+
launch_gradio_demo(ThreatIntelExtractorTool)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
json
|
3 |
+
requests
|
4 |
+
huggingface_hub
|
5 |
+
transformers
|
threat_extraction.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import env
|
2 |
+
import json
|
3 |
+
import openai
|
4 |
+
import requests
|
5 |
+
from os import environ as env
|
6 |
+
|
7 |
+
class FirewallManager:
|
8 |
+
def update_rule(self, ip: str, action: str):
|
9 |
+
# This is a placeholder. You would implement this to interact with your actual firewall.
|
10 |
+
print(f"Updated firewall rule for IP {ip} with action {action}")
|
11 |
+
|
12 |
+
class PacketFilter:
|
13 |
+
def drop_packet(self, ip: str):
|
14 |
+
# This is a placeholder. You would implement this to interact with your actual packet filter.
|
15 |
+
print(f"Dropped packet from IP {ip}")
|
16 |
+
|
17 |
+
class ThreatIntelExtractorTool(Tool):
|
18 |
+
name = "threat_intel_extractor_tool"
|
19 |
+
description = """
|
20 |
+
This tool scrapes a hypothetical threat intelligence feed, uses OpenAI API to extract structured information, and takes defensive actions based on the information.
|
21 |
+
Input is a URL of threat intel feed. Output is a structured response as a string with threat information.
|
22 |
+
"""
|
23 |
+
inputs = ["text"]
|
24 |
+
outputs = ["text"]
|
25 |
+
|
26 |
+
def __init__(self, firewall_manager: FirewallManager, packet_filter: PacketFilter):
|
27 |
+
self.openai_api_key = env.get("OPENAI_API_KEY")
|
28 |
+
openai.api_key = self.openai_api_key
|
29 |
+
self.firewall_manager = firewall_manager
|
30 |
+
self.packet_filter = packet_filter
|
31 |
+
|
32 |
+
def __call__(self, threat_intel_feed_url: str):
|
33 |
+
# Scrape threat intelligence feed
|
34 |
+
response = requests.get(threat_intel_feed_url)
|
35 |
+
threat_info_raw = response.text
|
36 |
+
|
37 |
+
# Send data to OpenAI API for text extraction
|
38 |
+
example_json = {
|
39 |
+
"Threats": [
|
40 |
+
{"Threat": "Threat 1", "IP": "192.0.2.0", "Description": "This is a hypothetical threat."},
|
41 |
+
{"Threat": "Threat 2", "IP": "192.0.2.1", "Description": "This is another hypothetical threat."}
|
42 |
+
]
|
43 |
+
}
|
44 |
+
prompt = f"Extract structured information from the following threat intelligence:\n{threat_info_raw}\nExample of the expected format:\n{json.dumps(example_json, indent=2)}"
|
45 |
+
extraction_response = openai.Completion.create(engine="text-davinci-003", prompt=prompt, max_tokens=100)
|
46 |
+
|
47 |
+
# Format extracted information into a structured response
|
48 |
+
extracted_info = self.format_extraction(extraction_response.choices[0].text.strip())
|
49 |
+
|
50 |
+
# Take defensive actions based on the extracted information
|
51 |
+
self.take_defensive_actions(extracted_info)
|
52 |
+
|
53 |
+
return extracted_info
|
54 |
+
|
55 |
+
def format_extraction(self, extraction: str) -> dict:
|
56 |
+
# This method would depend on the format of the extracted information
|
57 |
+
# For this example, let's assume the extraction is a list of threats, each one formatted as "Threat: <threat>, IP: <ip>, Description: <description>"
|
58 |
+
structured_info = []
|
59 |
+
for line in extraction.split('\n'):
|
60 |
+
parts = line.split(',')
|
61 |
+
structured_info.append({
|
62 |
+
'Threat': parts[0].split(':')[1].strip(),
|
63 |
+
'IP': parts[1].split(':')[1].strip(),
|
64 |
+
'Description': parts[2].split(':')[1].strip(),
|
65 |
+
})
|
66 |
+
return json.dumps({'Threats': structured_info})
|
67 |
+
|
68 |
+
def take_defensive_actions(self, threat_info: dict):
|
69 |
+
for threat in threat_info['Threats']:
|
70 |
+
ip = threat['IP']
|
71 |
+
# For the purposes of this example, let's assume that we want to block all traffic from the IP and drop any incoming packets.
|
72 |
+
self.firewall_manager.update_rule(ip, 'block')
|
73 |
+
self.packet_filter.drop_packet(ip)
|
tool_config.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"description": "This tool scrapes a hypothetical threat intelligence feed, uses OpenAI API to extract structured information, and takes defensive actions based on the information. Input is a URL of threat intel feed. Output is a structured response as a string with threat information.",
|
3 |
+
"name": "threat_intel_extractor_tool",
|
4 |
+
"tool_class": "threat_extraction.ThreatIntelExtractorTool"
|
5 |
+
}
|
6 |
+
|