Spaces:
Sleeping
Sleeping
saifeddinemk
commited on
Commit
•
4919f63
1
Parent(s):
bc119db
Fixed app v2
Browse files
app.py
CHANGED
@@ -1,39 +1,51 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
|
3 |
+
# Set up OpenAI API key
|
4 |
+
openai.api_key = "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"
|
5 |
+
|
6 |
+
# Function to read and process log files
|
7 |
+
def read_log_file(file_path):
|
8 |
+
with open(file_path, 'r') as file:
|
9 |
+
log_data = file.read()
|
10 |
+
return log_data
|
11 |
+
|
12 |
+
# Function to analyze log data for malicious activity using OpenAI
|
13 |
+
def analyze_logs_for_malicious_activity(log_data):
|
14 |
+
# Instruction prompt to guide the model
|
15 |
+
prompt = (
|
16 |
+
"Analyze the following network log data for any indicators of malicious activity, "
|
17 |
+
"such as unusual IP addresses, unauthorized access attempts, data exfiltration, or anomalies. "
|
18 |
+
"Provide details on potential threats, IPs involved, and suggest actions if any threats are detected.\n\n"
|
19 |
+
f"{log_data}"
|
20 |
+
)
|
21 |
+
|
22 |
+
# Send request to OpenAI API
|
23 |
+
response = openai.Completion.create(
|
24 |
+
engine="gpt-3.5-turbo", # Ensure to use a suitable model for instructions
|
25 |
+
prompt=prompt,
|
26 |
+
max_tokens=500,
|
27 |
+
temperature=0.5
|
28 |
+
)
|
29 |
+
|
30 |
+
# Extract response text
|
31 |
+
analysis = response.choices[0].text.strip()
|
32 |
+
return analysis
|
33 |
+
|
34 |
+
# Main function to execute log analysis
|
35 |
+
def main():
|
36 |
+
# Path to your network log file
|
37 |
+
log_file_path = "log.txt"
|
38 |
+
|
39 |
+
# Read log data
|
40 |
+
log_data = read_log_file(log_file_path)
|
41 |
+
|
42 |
+
# Analyze log data
|
43 |
+
analysis = analyze_logs_for_malicious_activity(log_data)
|
44 |
+
|
45 |
+
# Print or save analysis result
|
46 |
+
print("Analysis of Network Logs for Malicious Activity:\n")
|
47 |
+
print(analysis)
|
48 |
+
|
49 |
+
# Run the main function
|
50 |
+
if __name__ == "__main__":
|
51 |
+
main()
|