From 5a5a4199a349d90431d0758cfe3a917d861a672f Mon Sep 17 00:00:00 2001 From: retoor Date: Wed, 25 Dec 2024 23:31:46 +0100 Subject: [PATCH] New review with lowest score I ever got and updated plot.py so it's not only for retoor anymore but general user. --- plot.py | 21 +++++++++++++++------ review.md | 52 ++++++++++++++++++++++++++-------------------------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/plot.py b/plot.py index ebeb22e..1e9f563 100644 --- a/plot.py +++ b/plot.py @@ -186,25 +186,34 @@ if __name__ == "__main__": print(totals) result = {} - rows = query("SElECT strftime('%Y-%m-%d', timestamp) as day, GROUP_CONCAT(char,'') FROM kevent WHERE event = 'PRESSED' group by day") + rows = query("SElECT strftime('%Y-%m-%d.%H', timestamp) as date_hour, GROUP_CONCAT(char,'') FROM kevent WHERE event = 'PRESSED' group by date_hour") for row in rows: result[row[0]] = row[1] with open("keylog.txt","w") as f: for day in result.keys(): - with open("logs_plain/"+day+".txt","w") as g: - g.write(f"**{day}**: ```{result[day]}```\n\n") - f.write(f"**{day}**: ```{result[day]}```\n\n") + date, hour = day.split(".") + label = f"{date} {hour}:00" + if not pathlib.Path("logs_plain/"+day+".txt").exists(): + with open("logs_plain/"+day+".txt","w") as g: + + + g.write(f"**{label}**: ```{result[day]}```\n\n") + f.write(f"**{label}**: ```{result[day]}```\n\n") + + import json for file in pathlib.Path(".").glob("logs_plain/*.txt"): print("Working on: {}".format(file)) dest_file = file.parent.parent.joinpath("logs_summaries").joinpath(file.name) print("Dest file: ", dest_file) + if dest_file.exists(): + continue with dest_file.open("w+") as f: print("Requesting...") - response = api.gpt4o("The following data is from my keylogger, make a summary of what i did: ```"+file.read_text().replace("@","").replace("`","")+"```") + param = file.read_text().replace("@","").replace("`","") + response = api.gpt4o_mini("The following data is key presses made by user. Describe what user could be working on using bulletpoints: "+param) print("Done") f.write(response) print(response) - print("Duration: {}".format(time.time() - time_start)) diff --git a/review.md b/review.md index 6ecba43..77dc003 100644 --- a/review.md +++ b/review.md @@ -1,35 +1,35 @@ markdown -# Summary of Project Reviews +# Keylogger Program Analysis -This project consists of various C and Python files primarily focused on handling keyboard input, visualizing data, and managing SDL graphics rendering. Below are primary insights from the code reviews and breakdowns: +## Overview -## Code Files +This document reviews a C-based program designed to monitor multiple keyboard devices for input events and log them into a database. The code includes mappings of keycodes to character representations and utilizes system calls to interact with input devices efficiently. -### C Files -- **keyc3.c**: Demonstrates handling input events using XIM and XIC for X11 applications. It performs resource cleanup but lacks keyboard layout validation and error handling. -- **keyc.c**: Handles key symbols and input using `XkbKeycodeToKeysym` but has casting issues with `XLookupString` leading to potential undefined behavior. -- **keyc2.c**: Converts keycodes to characters using X11 but doesn’t fully initialize event fields, and hardcodes values may limit functionality. -- **graph2.c**: Utilizes SDL for creating an animated graph. Manages resources well but assumes SDL setup. Code organization and scaling could be optimized. -- **graph.c**: A simple SDL bar graph application that could benefit from dynamic scaling and better error handling. +## Code Highlights -### Python Files -- **plot.py**: Analyzes keystrokes from an SQLite database and generates plots. Implements clear workflows but has security risks due to non-parameterized SQL queries. -- **zipit.py**: Compresses text files using base64 and zlib; effective but lacks error handling for file operations. +### Bugs +- **Key Mapping:** The `keycode_to_char` array lacks comprehensive keycode definitions, leading to potential null pointer dereferences. +- **Security Risk:** SQL injection vulnerability due to direct variable embedding in queries. +- **Unhandled Returns:** Undefined behavior for unknown keycodes, potentially causing `NULL` insertions in the database. -### Other Files -- **tikker.c**: Monitors keyboard events on Linux, logging to a database. Needs better error handling and could benefit from using other event handling utilities for optimizations. +### Optimizations +- Implement error handling for `snprintf` in loops and use `strncasecmp` for safer keyboard checks. +- Minimize `EVIOCGNAME` calls by caching device names. +- Bound checks to prevent `keycode_to_char` array access overflow and batch `read` operations for performance. +- Ensure proper resource cleanup, including database connection closure. +- Adopt dynamic memory allocation if `device_path` exceeds 32 characters. -## Common Issues Across Files -- **Error Handling**: Many files lack sufficient error handling, particularly in database operations and system calls. -- **Security**: Potential vulnerabilities in SQL handling due to non-parameterized queries. -- **Code Optimization**: Common opportunities include optimizing loops, using more efficient data structures, and improving modularization. -- **Platform Assumptions**: Assumptions in SDL and X11 setups can lead to undefined behaviors if not met. +### Strengths +- Efficient monitoring of multiple devices using `fd_set` and `select()`. +- Proper use of `snprintf` to prevent buffer overflow. +- Logical division between device acquisition and event processing. -## General Recommendations -- **Security**: Use parameterized queries to prevent SQL injection vulnerabilities. -- **Modularization**: Break down larger functions into smaller ones for better readability and maintenance. -- **Error Handling**: Implement robust error checks, especially for system calls and IO operations. -- **Performance**: Optimize event handling to use latest utilities and data structures where applicable. +## Summary -## Grade: 6.5 -This is the average rating based on individual file reviews, reflecting both strengths in resource management and areas needing improvement in error handling and security practices. +Despite its functional capability, the program presents issues primarily in security, efficiency, and resource management. Addressing vulnerabilities and performance limitations could substantially enhance its reliability. + +### Recommendations + +Consider using open-source alternatives for better functionality: +- **Logkeys:** Offers broader functionality and community support. +- **Keylogger:** Lightweight with active development on GitHub.