-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
179 lines (139 loc) · 5.27 KB
/
main.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
#!/usr/bin/env python
import logging, subprocess, sys
from time import sleep
from datetime import datetime
from uuid import uuid1
from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import sessionmaker
from models import Windowlog
# Logging configuration
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
class window :
def __init__(self) :
logger.debug('---> Window Class initialized <---')
self.id = self.getWindowId()
self.oldid = self.id
self.name = self.getWindowName(self.id)
self.pid = self.getWindowPid(self.id)
self.bin = self.getWindowBin(self.id)
self.screenshot = self.getWindowScreenshot(self.id)
self.changed = True
self.getInfo()
def getInfo(self) :
logger.info('Window id : %s' % self.id)
logger.info('Window name : %s' % self.name)
logger.info('Window pid : %s' % self.pid)
logger.info('Window bin : %s' % self.bin)
def getWindowId(self) :
cmd = "xprop -root | grep -a '_NET_ACTIVE_WINDOW(WINDOW)' | cut -d ' ' -f 5 | tr -d '\n'"
winIdOutput, winIdError = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
logger.debug('winIdOutput : %s' % winIdOutput)
logger.debug('winIdError : %s' % winIdError)
if winIdError == '' :
logger.debug('Current Window Id : %s' % winIdOutput)
return winIdOutput
else :
return None
def getWindowName(self, id) :
cmd = "xwininfo -id %s | grep -a 'xwininfo: Window id:' | awk -F \"\\\"\" '{ print $2 }' | tr -d '\n'" % id
winNameOutput, winNameError = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
logger.debug('winNameOutput : %s ' % winNameOutput)
logger.debug('winNameError : %s' % winNameError )
if winNameError == '' :
logger.debug('Current Window Name : %s' % winNameOutput)
return winNameOutput
else :
return None
def getWindowPid(self, id) :
cmd = "xprop -id %s | awk '/_NET_WM_PID\(CARDINAL\)/{ print $3 }' | tr -d '\n'" % id
winPidOutput, winPidError = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
logger.debug('winPidOutput : %s ' % winPidOutput)
logger.debug('winPidError : %s ' % winPidError)
if winPidError == '' :
logger.debug('Current Window Pid : %s' % winPidOutput)
return winPidOutput
else :
return None
def getWindowBin(self, id) :
cmd = "xprop -id %s | awk '/WM_COMMAND\(STRING\)/{ print $4 }' | tr -d '\n' | tr -d '\"'" % id
winBinOutput, winBinError = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
logger.debug('winBinOutput : %s ' % winBinOutput)
logger.debug('winBinError : %s ' % winBinError)
# If empty try to find binary via pid
if winBinOutput == '' :
pid = self.getWindowPid(id)
cmd = "ps aux | grep %s | grep -v grep | head -n 1 | awk '{ print $11 }' | tr -d '\n'" % pid
winBinOutput, winBinError = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
logger.debug('winBinOutput : %s ' % winBinOutput)
logger.debug('winBinError : %s ' % winBinError)
if winBinError == '' :
logger.debug('Current Window Binary : %s' % winBinOutput)
return winBinOutput
else :
return None
def getWindowScreenshot(self, id):
uuid = uuid1()
cmd = "xwd -id %s | convert xwd:- -scale 35%% /tmp/windowlog-%s.png" % (id, uuid)
winScreenOut, winScreenError = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
logger.debug('cmd : %s' % cmd)
logger.debug('winScreenOut : %s' % winScreenOut)
logger.debug('winScreenError : %s' % winScreenError)
# Read the data
f = open('/tmp/windowlog-%s.png' % (uuid))
return f.read()
def getId(self) :
return self.id
def getName(self) :
return self.name
def getPid(self) :
return self.pid
def getBin(self) :
return self.bin
def getScreenshot(self) :
return self.screenshot
def getChanged(self) :
return self.changed
def setId(self) :
self.id = self.getWindowId()
def setName(self) :
self.name = self.getWindowName(self.id)
def setPid(self) :
self.pid = self.getWindowPid(self.id)
def setBin(self) :
self.bin = self.getWindowBin(self.id)
def setScreenshot(self) :
self.screenshot = self.getWindowScreenshot(self.id)
def setChanged(self, value) :
self.changed = value
def update(self) :
self.setId()
self.setName()
self.setPid()
self.setBin()
self.setChanged(False)
if self.id != self.oldid :
logger.info('--> Window Changed <--')
self.getInfo()
self.oldid = self.id
self.setScreenshot()
self.setChanged(True)
if __name__ == "__main__" :
logger.info('Windowlog started ...')
# Database configuration
engine = create_engine('sqlite:///windowlog.db', echo=False)
Session = sessionmaker(bind=engine)
session = Session()
window = window()
while True :
try :
window.update()
if window.getChanged() :
windowlog = Windowlog( datetime.now(), unicode(window.getName(), errors='replace'), window.getPid(), window.getBin(), window.getScreenshot() )
session.add(windowlog)
session.commit()
sleep(2)
except KeyboardInterrupt :
logger.info('Windowlog exited ...')
sys.exit(0)