-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmemscan.py
164 lines (149 loc) · 3.96 KB
/
memscan.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
import socket
from time import sleep, time
from thread import start_new_thread
from traceback import print_exc as stacktrace
from subprocess import check_output, CalledProcessError
cached_loot_messages = set()
heap = tuple()
notifier = None
updates = list()
reads = list()
def update_heap(pid):
try:
global heap
global updates
while True:
iniT = time()
maps_file = open('/proc/%s/maps' % pid, 'r')
for line in maps_file.readlines(): # for each mapped region
mem_info = line.rsplit()
mapping = mem_info[-1]
if mapping == '[heap]': # reading only from the heap, all the messages are allocated there!
region = mem_info[0].split('-')
start = int(region[0], 16)
end = int(region[1], 16)
if (start, end) != heap: # check for heap expansions
heap = (start, end)
maps_file.close()
updates.append(time() - iniT)
sleep(1)
except KeyboardInterrupt:
pass
def is_timestamp(string):
return string[2] == ':' and string[:2].isdigit() and string[3:5].isdigit() and string[5] == ' '
def messages(chunk):
offset = 0
while True:
index = chunk.find(':', offset) - 2
if index < 0 or index > len(chunk) - 6: # No point in reading something shorter than '00:00 '
break
if is_timestamp(chunk[index: index + 6]):
endPos = chunk.find('\0', index + 6)
message = chunk[index:endPos]
if len(message) > 6:
yield message
offset = endPos + 1
else:
offset = index + 8
def read_process_memory(pid):
global heap
try:
mem_file = open('/proc/%s/mem' % pid, 'r')
except IOError:
quit() # Tibia client closed
item_drops = []
exp = dict()
try:
if not heap:
start_new_thread(update_heap, (pid,))
while not heap:
sleep(0.2)
iniT = time()
start, end = heap
mem_file.seek(start)
try:
chunk = mem_file.read(end - start) # read region contents
except:
chunk = ''
if messages(chunk):
for log_message in messages(chunk):
if log_message[5:14] == ' Loot of ':
global cached_loot_messages
if log_message in cached_loot_messages:
continue
cached_loot_messages.add(log_message)
item_drops.append(log_message)
elif log_message[5:17] == ' You gained ':
t = log_message[0:5]
try:
e = int(log_message[17:log_message.find(' ')])
if t not in exp: exp[t] = e
else: exp[t] += e
except: pass
except KeyboardInterrupt:
return dict()
except:
print 'Error while reading memory!'
stacktrace()
return
finally:
mem_file.close()
reads.append(time() - iniT)
return_values = dict()
return_values['item_drops'] = item_drops
return_values['experience'] = exp
return return_values
def quit(retval=0):
global reads
global updates
try:
notifier.close()
except: pass
print '--Memory scanner closed--'
if reads and updates:
print 'Statistics:'
print 'Average memory read time: ', (sum(reads) / len(reads)),'\nAverage heap update time: ', (sum(updates) / len(updates))
exit(retval)
def main():
global notifier
sockfile = '/tmp/flarelyzer.sock'
print 'Initializing memscan...'
try:
notifier = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
notifier.connect(sockfile)
print 'connected to notification server!'
tibiaPID = check_output(['pgrep', 'Tibia']).split('\n')[0]
print 'Client PID: ' + str(tibiaPID)
except socket.error:
print 'Unable to connect to notification agent!'
except CalledProcessError:
print 'Tibia client not found!'
quit(1)
except:
print 'Unexpected error'
stacktrace()
else:
# we're good to go
print '===Memory analyzer started==='
try:
notifier.sendall('ATTACHED')
while True:
res = read_process_memory(tibiaPID)
if not res:
quit()
for full_msg in res['item_drops']:
notifier.sendall(full_msg)
notifier.recv(8)
notifier.sendall('NEXT')
response = notifier.recv(8)
if response == 'QUIT':
raise KeyboardInterrupt
sleep(0.1)
except KeyboardInterrupt:
pass
except Exception:
stacktrace()
print '==Aborting=='
quit()
if __name__ == '__main__':
main()