File size: 1,707 Bytes
4919f63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
import openai

# Set up OpenAI API key
openai.api_key = "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"

# Function to read and process log files
def read_log_file(file_path):
    with open(file_path, 'r') as file:
        log_data = file.read()
    return log_data

# Function to analyze log data for malicious activity using OpenAI
def analyze_logs_for_malicious_activity(log_data):
    # Instruction prompt to guide the model
    prompt = (
        "Analyze the following network log data for any indicators of malicious activity, "
        "such as unusual IP addresses, unauthorized access attempts, data exfiltration, or anomalies. "
        "Provide details on potential threats, IPs involved, and suggest actions if any threats are detected.\n\n"
        f"{log_data}"
    )
    
    # Send request to OpenAI API
    response = openai.Completion.create(
        engine="gpt-3.5-turbo",  # Ensure to use a suitable model for instructions
        prompt=prompt,
        max_tokens=500,
        temperature=0.5
    )
    
    # Extract response text
    analysis = response.choices[0].text.strip()
    return analysis

# Main function to execute log analysis
def main():
    # Path to your network log file
    log_file_path = "log.txt"
    
    # Read log data
    log_data = read_log_file(log_file_path)
    
    # Analyze log data
    analysis = analyze_logs_for_malicious_activity(log_data)
    
    # Print or save analysis result
    print("Analysis of Network Logs for Malicious Activity:\n")
    print(analysis)

# Run the main function
if __name__ == "__main__":
    main()