Files
Kostas 9e0c2a7912 Enhance Linux Telemetry with New Features and Improvements (#99)
* Uptycs addition

* Update EDR_telem.json

Updates based on evidence to be provided.

* Update EDR_telem.json

Minor correction to match evidence provided.

* Post-review update for Uptycs

* Update EDR_telem.json

Changes per updated evidence provided privately.

* Update EDR_telem.json: Update Process Access to "No"

* Initial commit for linux telemetry generator script.

* Refactor Linux telemetry generator script to include user account activities

* Remove unused imports from Linux telemetry generator script

* Add process hijack demo script using ptrace as suggested here: https://github.com/tsale/EDR-Telemetry/issues/21#issuecomment-2450048423

* Update raw_access_read function to read from /dev/sda in read-only mode and improve error handling

* Fix function name typo in process_hijack_demo.py and refactor network socket management in lnx_telem_gen.py

* Rename process_access to start_hijacking and update references; add network_connect method to NetworkSocketManager

* Remove commented-out main function and unused RemoteLibraryInjector class from lnx_telem_gen.py

* Refactor error handling in driver_load.py, scheduled_task.py, and process_tampering.py; add success messages and improve exception raising. Added README file.

* Add eBPF execution functionality via pamspy

* Update LINUX_TELEMETRY_GENERATOR_GUIDE.md

* Remove requirements.txt and correct apt installations for Debian in LINUX_TELEMETRY_GENERATOR_GUIDE.md

* Enhance Linux Telemetry Generator: Add PrettyTable dependency, improve process filtering, and implement execution summary logging

* Restore ProcessAccess event handling and reduce delay between events in lnx_telem_gen.py

* Add EDR telemetry configuration for process, file, user, network, and service activities

* Linux telem update

* Add Linux support to EDR telemetry scoring and enhance command line interface

* Refactor SentinelOne field in EDR telemetry configuration to remove redundancy

* No code changes made.

---------

Co-authored-by: Josh Lemon - Uptycs <116134008+joshlemon-uptycs@users.noreply.github.com>
Co-authored-by: SecurityAura <SecurityAura@users.noreply.github.com>
Co-authored-by: Ján Trenčanský <j91321@users.noreply.github.com>
Co-authored-by: mthcht <mthcht@users.noreply.github.com>"
2024-12-16 22:17:28 -08:00

116 lines
3.8 KiB
Python

import os
import ctypes
import subprocess
###
# This script writes, compiles, and loads a simple Linux kernel module.
# It performs the following steps:
# 1. Writes a C source file for a test kernel module.
# 2. Writes a Makefile to compile the kernel module.
# 3. Compiles the kernel module using the Makefile.
# 4. Loads the compiled kernel module into the kernel using the finit_module system call.
###
# Constants for system call numbers (Linux-specific)
SYS_finit_module = 313 # On x86_64; this number may vary by architecture
# Load the C library (libc) which contains system calls
libc = ctypes.CDLL("libc.so.6")
# Define finit_module prototype and parameters in ctypes
# int finit_module(int fd, const char *param_values, int flags);
libc.syscall.argtypes = [ctypes.c_long, ctypes.c_int, ctypes.c_char_p, ctypes.c_int]
libc.syscall.restype = ctypes.c_int
def write_test_driver():
"""Write the test driver C code to a file."""
driver_code = """
#include <linux/module.h> // Needed by all kernel modules
#include <linux/kernel.h> // Needed for KERN_INFO
#include <linux/init.h> // Needed for __init and __exit macros
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A Simple Test Kernel Module");
// Function that runs when the module is loaded
static int __init test_driver_init(void) {
printk(KERN_INFO "Test Driver Loaded: Hello, Kernel!\\n");
return 0; // Return 0 means successful loading
}
// Function that runs when the module is unloaded
static void __exit test_driver_exit(void) {
printk(KERN_INFO "Test Driver Unloaded: Goodbye, Kernel!\\n");
}
// Macros that specify the initialization and cleanup functions
module_init(test_driver_init);
module_exit(test_driver_exit);
"""
# Write to a file
with open("test_driver.c", "w") as f:
f.write(driver_code)
print("Test driver code written to 'test_driver.c'.")
def write_makefile():
"""Write the Makefile to compile the kernel module."""
makefile_content = """
obj-m += test_driver.o
all:
\tmake -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
\tmake -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
"""
# Write the Makefile to the current directory
with open("Makefile", "w") as f:
f.write(makefile_content)
print("Makefile written.")
def compile_driver():
"""Compile the kernel module using the Makefile."""
try:
subprocess.run(["make"], check=True)
print("Kernel module compiled successfully.")
except subprocess.CalledProcessError:
print("Failed to compile the kernel module.")
raise Exception("Kernel module compilation failed") # Raise an exception instead
def load_kernel_module(module_path, params=""):
"""Load the kernel module using the finit_module system call."""
fd = os.open(module_path, os.O_RDONLY)
if fd < 0:
print(f"Failed to open module file: {module_path}")
return
# Make the finit_module system call
ret = libc.syscall(SYS_finit_module, fd, params.encode('utf-8'), 0)
# If ret == 0, the module was loaded successfully
if ret == 0:
print(f"Module {module_path} loaded successfully.")
else:
# Handle the case where finit_module fails
errno = ctypes.get_errno()
print(f"Failed to load module: {os.strerror(errno)}")
os.close(fd)
def loadit():
# Write the driver C code and Makefile
write_test_driver()
write_makefile()
# Compile the kernel module
compile_driver()
# Load the kernel module using finit_module system call
module_path = "./test_driver.ko" # The compiled kernel module
load_kernel_module(module_path)
return "Driver loaded successfully."