Skip to content

Instantly share code, notes, and snippets.

@bob-the-builder-v
Last active December 27, 2024 08:59
Show Gist options
  • Save bob-the-builder-v/7cbbd4efe89306e056d6696f739d6942 to your computer and use it in GitHub Desktop.
Save bob-the-builder-v/7cbbd4efe89306e056d6696f739d6942 to your computer and use it in GitHub Desktop.
## Note: Tested on Linux only as of now.
How to use?
1. There should be an alert.mp3 file in your directory for the sound to be played.
2. Use a python virtualenv to install requirements.
3. Just run the system_honeypot.py file.
evdev==1.7.1
numpy==2.1.3
opencv-python==4.10.0.84
pillow==11.0.0
pygame==2.6.1
pynput==1.7.7
python-xlib==0.33
six==1.16.0
import os
import cv2
import pygame
import threading
from pynput import keyboard, mouse
import subprocess
import logging
from datetime import datetime
import time
import sys
class SecureScreenMonitor:
def __init__(self, sound_path):
logging.basicConfig(filename='security_monitor.log', level=logging.INFO)
self.security_triggered = False
self.event_lock = threading.Lock()
# Initialize listeners for keyboard and mouse events
self.keyboard_listener = keyboard.Listener(on_press=self.on_keyboard_event)
self.mouse_listener = mouse.Listener(on_move=self.on_mouse_event)
pygame.mixer.init()
self.alert_sound_path = sound_path
self.alert_sound_thread = None
self.camera_index = self.find_camera()
# Timer to reset security state
self.shutdown_timer = None
def on_keyboard_event(self, key):
try:
# Detect any key press, including special keys (like Ctrl)
logging.info(f"Keyboard event detected: {key}")
self.trigger_security()
except AttributeError:
# Handle cases where the key is a special key (e.g., Ctrl, Shift, etc.)
logging.info(f"Special keyboard event detected: {key}")
self.trigger_security()
def on_mouse_event(self, x, y):
logging.info(f"Mouse moved to position ({x}, {y})")
self.trigger_security()
def trigger_security(self):
with self.event_lock:
if not self.security_triggered:
logging.info("Security triggered by keyboard or mouse event")
self.security_triggered = True
threading.Thread(target=self.security_response).start()
# Start the timer to reset after 20 seconds
self.shutdown_timer = threading.Timer(20.0, self.reset_security)
self.shutdown_timer.start()
def find_camera(self):
for i in range(3):
cap = cv2.VideoCapture(i)
if cap.isOpened():
cap.release()
return i
logging.error("No camera found")
return None
def capture_evidence(self):
if self.camera_index is None:
logging.warning("No camera available")
return
try:
cap = cv2.VideoCapture(self.camera_index)
ret, frame = cap.read()
cap.release()
if ret:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
cv2.imwrite(f'evidence/security_evidence_{timestamp}.jpg', frame)
logging.info(f"Evidence captured at {timestamp}")
except Exception as e:
logging.error(f"Evidence capture failed: {e}")
def lock_system(self):
lock_commands = [
"xdg-screensaver lock",
"gnome-screensaver-command -l",
"loginctl lock-session",
"i3lock -c 000000"
]
for cmd in lock_commands:
try:
subprocess.run(cmd, shell=True, check=True)
logging.info(f"System locked with: {cmd}")
return True
except Exception:
continue
logging.warning("Could not lock system")
return False
def play_alert(self):
def sound_loop():
try:
pygame.mixer.music.load(self.alert_sound_path)
pygame.mixer.music.play(loops=-1, start=0.0) # Play sound in infinite loop
while pygame.mixer.music.get_busy(): # Ensure sound keeps playing
time.sleep(1)
except Exception as e:
logging.warning(f"Sound play failed: {e}")
# Start sound playback in a separate thread
self.alert_sound_thread = threading.Thread(target=sound_loop, daemon=True)
self.alert_sound_thread.start()
def security_response(self):
self.lock_system()
self.capture_evidence()
self.play_alert()
def reset_security(self):
with self.event_lock:
self.security_triggered = False
pygame.mixer.music.stop() # Stop the sound
if self.shutdown_timer:
self.shutdown_timer.cancel()
logging.info("Security state reset and program continues.")
def countdown_timer(self):
for i in range(5, 0, -1):
print(f"Program starts in {i} seconds...", end='\r')
time.sleep(1)
print("\nProgram started!")
def run(self):
# Display countdown timer
threading.Thread(target=self.countdown_timer, daemon=True).start()
# Wait for the countdown to finish
time.sleep(5)
# Start the keyboard and mouse listeners in separate threads
self.keyboard_listener.start()
self.mouse_listener.start()
# Run the program indefinitely
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
logging.info("Program interrupted manually.")
# Stop the keyboard and mouse listeners
self.keyboard_listener.stop()
self.mouse_listener.stop()
# Cancel any active timer
if self.shutdown_timer:
self.shutdown_timer.cancel()
def main(sound_path):
monitor = SecureScreenMonitor(sound_path)
monitor.run()
if __name__ == "__main__":
main(
"alert.mp3" # Path to the sound file
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment