-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeystroke.py
394 lines (295 loc) · 11.7 KB
/
keystroke.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/python
# -*- coding: utf-8 -*-
from AppKit import NSApplication, NSApp, NSWorkspace
from Foundation import NSObject, NSLog, NSAppleScript
from Cocoa import NSEvent, NSKeyDownMask, NSLeftMouseUpMask
from PyObjCTools import AppHelper, Conversion
import keycode
import appscript
import string
from subprocess import Popen, PIPE
from datetime import datetime, timedelta
import mozrepl
from uptime import uptime
from urlparse import urlparse, parse_qs
import psycopg2
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
mask1 = NSKeyDownMask
mask2 = NSLeftMouseUpMask
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask1, handler)
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask2, handler)
def handler(event):
#############
#VARS
#activeAppName
global lastAppName
global lastUrl
global url
global lastDomain
#global domains
#lastDomain = ""
appTime = "0"
bootTime = ""
#incomingTimestamp# = ""
global appStartedTimestamp
#global queryValue
queryValue = ""
global lastQueryValue
domain = None
#############
browserList = ["Google Chrome", "Safari", "Firefox", "Firefox Plugin Content (Shockwave Flash)"]
browserSet = set(browserList)
#appStartedTimestamp = ""
try:
incomingTimestamp = datetime.now()
print "******* NEW EVENT ********************"
activeAppName = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationName']
print "Active APP"
print activeAppName
print "Last APP = "
print lastAppName
if activeAppName in browserSet:
print "About to launch getURLData"
url, domain, queryValue = getURLData(activeAppName)
print "GOT QUERY VALUES"
print url
print domain
print queryValue
else:
url = ""
domain =""
queryValue = ""
###Keyboard Input- keydown activity###
if event.type() == 10:
print "keyDown Event=" + event.characters()
print ""
###Mouse Click- Left Click up Mouse Input###
if event.type() == 2:
print "mouseUp Event"
print activeAppName
print type(activeAppName) , type(lastAppName)
print activeAppName
print lastAppName
if activeAppName == lastAppName:
print "No Change in Active App"
if activeAppName in browserSet:
print lastUrl
if lastUrl:
print "lastUrl tested as TRUE"
print url
print lastUrl
if lastUrl != url:
#write DB here
duration = printLogEntry(incomingTimestamp)
print "Writing to DB..."
user = "matt"
userType = "userType1"
tenant = "tenant1"
insertDB(appStartedTimestamp, incomingTimestamp, duration, activeAppName, lastAppName, user, userType, tenant, lastDomain, lastQueryValue)
appStartedTimestamp = incomingTimestamp
else:
print "NO APP CHANGE & NO URL FROM BROWSER"
url = ""
domain = ""
queryValue = ""
elif lastAppName is None:
print "lastAppName was set to NULL"
#printLogEntry()
lastAppName = activeAppName
print lastAppName
appStartedTimestamp = incomingTimestamp
#lastUrl.append(url)
else:
duration = printLogEntry(incomingTimestamp)
print "DURATION HIT"
print duration
user = "matt"
userType = "userType1"
tenant = "tenant1"
print activeAppName
print lastAppName
print lastUrl
print url
print lastDomain
print lastQueryValue
insertDB(appStartedTimestamp, incomingTimestamp, duration, activeAppName, lastAppName, user, userType, tenant, lastDomain , lastQueryValue)
appStartedTimestamp = incomingTimestamp
#Rotate Settings
#######
#appStartedTimestamp = incomingTimestamp
lastAppName = activeAppName
activeAppName = ""
print "DOMAIN: " + domain
lastDomain = domain
domain = ""
print "Rotated last domain"
lastUrl = url
url = ""
print "Rotated last url"
print "QUERY VALUE: " + str(queryValue)
lastQueryValue = queryValue
queryValue = ""
print "Rotated queryValue "
except KeyboardInterrupt:
print "Something fucked up! Exception!!!"
AppHelper.stopEventLoop()
def ensureUniqueInList(newList, oldList):
length = len(oldList)
if newList not in oldList:
return True
else:
return False
def printLogEntry(incomingTimestamp):
#duration = str((incomingTimestamp - appStartedTimestamp)).split(".")[0]
print "###############"
duration = (incomingTimestamp - appStartedTimestamp)
duration_str = str(duration).split(".")[0]
print duration
print "********** TOTAL TIME **********"
print incomingTimestamp
print "The time now is: " + incomingTimestamp.strftime("%H:%M:%S")
print lastAppName + " Start: " + appStartedTimestamp.strftime("%H:%M:%S")
print lastAppName + " End: " + incomingTimestamp.strftime("%H:%M:%S")
print lastAppName + " Duration: " + str(duration) + "\n"
return duration
def getURLData(browserType):
global url
#global domains
#global queryValues
domain = ""
queryValue = ""
print "Starting getURLData Function"
if browserType == "Google Chrome":
out = NSAppleScript.alloc().initWithSource_("tell application \"Google Chrome\" to return URL of active tab of front window")
urls = out.executeAndReturnError_(None)
url = urls[0]
url = url.stringValue()
elif browserType == "Safari":
out = NSAppleScript.alloc().initWithSource_("tell application \"Safari\" to return URL of front document")
urls = out.executeAndReturnError_(None)
url = urls[0]
url = url.stringValue()
elif browserType == "Firefox" or browserType == "Firefox Plugin Content (Shockwave Flash)":
repl = mozrepl.Mozrepl()
url = repl.execute("gLastValidURLStr")
print url
print "THATs the NON hacked value"
print "Got the URL \n" + url
urlParsed = urlparse(url)
print urlParsed
domain = urlParsed.netloc
print "Got the domain: " + domain
### Parse Search String ####
print "Running the Parse Search String Section"
if bool(parse_qs(urlParsed.query)) or bool(parse_qs(urlParsed.fragment)):
print "The user did a search on Google or Bing"
if domain == "google.com" or domain == "www.google.com":
try:
print "google section"
queryValue = parse_qs(urlParsed.fragment)['q']
print queryValue
print "JUST PRINTED QUERY VALUES"
except KeyError:
print "Not a valid google url search"
elif "bing.com" in domain:
try:
print "bing"
queryValue = parse_qs(urlParsed.query)['q']
except KeyError:
print "Not a valid bing url search"
print "This is the queryValue: " + str(queryValue)
print "Made it to end of function- get url"
#START HERE
print "VALUES to RETURN:"
print url
print domain
print queryValue
return url, domain, queryValue
def insertDB(appStartedTimestamp, incomingTimestamp, duration, activeAppName, lastAppName, userid, userType, tenant, lastDomain, lastQueryValue):
global url, lastUrl
print "About to Write to DB...."
print "got these"
print appStartedTimestamp, incomingTimestamp, duration, lastAppName, userid, userType, tenant, lastDomain, lastQueryValue
try:
conn = psycopg2.connect("dbname='dev' user='postgres' host='localhost' password='matt22'")
except psycopg2.Error as e:
print "Unable to connect to the DB"
pass
try:
# data = 'appStartedTimestamp, incomingTimestamp, duration, lastAppName, "matt", "userType1", "tenant1", url, domain, lastQueryValues'
#data = (
# ('appStartedTimestamp', 'incomingTimestamp', 'duration', 'lastAppName', 'matt', 'userType1', 'tenant1', 'url', 'domain',\
# 'queryValues')
#)
# SQLfields = '"StartTimeStamp", "EndTimeStamp", "Duration", "AppName", "UserID", "UserType", tenant, url, domain, "keywordsSearched"'
#query = 'INSERT into "events" ("StartTimeStamp", "EndTimeStamp", "Duration","AppName", "UserID", "UserType",\
# "Tenant", "Url", "Domain", "KeywordsSearched") VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
cur = conn.cursor()
print "About to blow up- executing now"
#cur.executemany(query, data)
try:
cur.execute('INSERT INTO "events" ("start_time_stamp", "end_time_stamp", "duration","app_name", "user_id", "user_type", "tenant", "url", "domain", "search_words") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', (appStartedTimestamp, incomingTimestamp, duration, lastAppName, userid, userType, tenant, lastUrl, lastDomain, lastQueryValue))
conn.commit()
print "********* SUCCESSFULLY WROTE TO DB *********"
except psycopg2.DatabaseError, e:
if conn:
conn.rollback()
print 'Error %s' %e
finally:
print "Running the Finally Section"
if conn:
conn.close()
#Rotate Settings
#######
#lastAppName = activeAppName
#appStartedTimestamp = incomingTimestamp
#lastUrl = url
#lastDomain = domain
print "SENDING THESE VALUES"
print "********************"
print appStartedTimestamp
print incomingTimestamp
print duration
print lastAppName
print "matt"
print "userType1"
print "tenant1"
print lastUrl
print lastDomain
print lastQueryValue
#cur.execute("""
# INSERT into "EventsTable" ("StartTimeStamp", "EndTimeStamp", "Duration",
# "AppName", "UserID", "UserType", "Tenant", "Url", "Domain", "KeywordsSearched")
# VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s),
# (appStartedTimestamp, incomingTimestamp, duration, lastAppName, "matt",
# "userType1", "tenant1", url, domain, queryValues) """)
#cur.execute("""INSERT into "EventsTable" (SQLfields) VALUES (data) """)
#print cur.fetchall()
conn.close()
except psycopg2.Error as e:
print "The query is fucked up"
print 'Error %s' % e
pass
def main():
global lastAppName
global url
global lastUrl
global lastDomain
#global queryValue
global lastQueryValue
global lastDomain
lastAppName = None
url = None
lastUrl = None
#queryValue = None
lastQueryValue = None
lastDomain = ""
#domain = ""
#queryValue =
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
AppHelper.runEventLoop()
if __name__ == '__main__':
main()