Give an example of simple Batch script which can trigger Python detection script below:…
- User
Give an example of simple Batch script which can trigger Python detection script below:
import subprocess import json
def run_powershell_command(command): """ Execute a PowerShell command from Python and return the output """ process = subprocess.run(["powershell", "-Command", command], capture_output=True, text=True) return process.stdout
def scan_event_logs(): """ Scan Windows event logs for indications of cyberattacks based on specific patterns """ # List of event IDs and logs to check based on MITRE Execution techniques events_to_check = [ {"log": "Microsoft-Windows-PowerShell/Operational", "id": [400, 4103, 4104]}, # PowerShell script block logging, etc. {"log": "Security", "id": [4688]}, # Process Creation including cmd, PowerShell.exe {"log": "System", "id": [1, 6013]}, # System start-up events (for context) ]
codedetected_threats = [] for event in events_to_check: log_name = event['log'] for event_id in event['id']: # Construct PowerShell command to fetch relevant event logs command = "Get-WinEvent -FilterHashtable @{" + f"logname='{log_name}'; id={event_id}" + "} | Select-Object TimeCreated, Id, Message | ConvertTo-Json" output = run_powershell_command(command) if output: try: events = json.loads(output) print(output) if type(events) is dict: # If only one event is found, it will be a dict, not a list events = [events] for event in events: detected_threats.append({ 'Time': event["TimeCreated"], 'EventID': event["Id"], 'Details': event["Message"] }) except json.JSONDecodeError as e: print(f"Error decoding JSON from PowerShell output: {e}") return detected_threatsif name == "main": threats_found = scan_event_logs() if threats_found: print("Potential cyberattack indications detected:") for threat in threats_found: print(f"Time: {threat['Time']}, Event ID: {threat['EventID']}, Details: {threat['Details']}") else: print("No indications of cyberattacks detected based on the specified criteria.")
- Assistant
To trigger the Python script you've provided, you can create a simple Batch script like the one below. First, ensure your Python script is saved with a filename, e.g.,
DetectThreats.py. Then proceed with the following Batch script:batch@echo off REM Change the path to your Python executable if necessary SET pythonExe=C:\path\to\python.exe REM Change the path to your Python script if necessary SET scriptPath=C:\path\to\DetectThreats.py REM Execute the Python script %pythonExe% %scriptPath% pauseHere are a few things you need to consider when creating and using this Batch file: