-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstracer.py
1291 lines (1126 loc) · 51.6 KB
/
stracer.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Github/Twitter: @zom3y3
# Email: [email protected]
# Based on https://github.com/johnlcf/Stana
import io
import sys
import getopt
import re
import json
import traceback
import logging
import shutil
#import ssdeep
import os
DROP_FLODER = '/tmp/'
import hashlib
def file_md5(file):
if os.path.exists(file):
f = open(file, 'rb')
m = hashlib.md5(f.read())
md5 = m.hexdigest()
f.close()
return md5
from optparse import OptionParser
from datetime import timedelta, time, datetime
from collections import defaultdict, deque
class StraceParser:
"""
StraceParser
This is the strace parser. It parses each system call lines into a dict, and
then call the registered stat modules to process.
The defination of dict: please refer to _parseLine
"""
def __init__(self):
self._completeSyscallCallbackHook = defaultdict(list)
self._rawSyscallCallbackHook = defaultdict(list)
# regex compiled for _parseLine
self._rePointer = re.compile(r"\[([0-9a-z?]*)\]")
self._reCompleteSyscall = re.compile(r"([^(]+)\((.*)\)[ ]+=[ ]+([a-fx\d\-?]+)(.*)")
self._reUnfinishedSyscall = re.compile(r"([^(]+)\((.*) <unfinished ...>")
self._reResumedSyscall = re.compile(r"\<\.\.\. ([^ ]+) resumed\> (.*)\)[ ]+=[ ]+([a-fx\d\-?]+)(.*)")
return
def registerSyscallHook(self, fullSyscallName, func):
self._registerHookInTable(fullSyscallName, self._completeSyscallCallbackHook, func)
def registerRawSyscallHook(self, fullSyscallName, func):
self._registerHookInTable(fullSyscallName, self._rawSyscallCallbackHook, func)
def _registerHookInTable(self, name, table, func):
table[name].append(func)
def startParse(self, reader, straceOptions):
self._parse(reader, straceOptions)
def autoDetectFormat(self, reader):
""" autoDetectFormat - Detect the strace output line format, return a
dict with following:
straceOptions["havePid"] = True/False
straceOptions["haveTime"] = ""/"t"/"tt"/"ttt"
straceOptions["haveTimeSpent"] True/False
It use peek() on the reader so it will not abvance the position of
the stream.
"""
buf = reader.buffer.peek(4096);
failCount = 0
for line in buf.split('\n'):
if failCount == 3:
return None
if "unfinish" in line or "resume" in line:
continue
straceOptions = self._detectLineFormat(line)
if straceOptions:
return straceOptions
else:
failCount += 1
return None
def _detectTimeFormat(self, timeStr):
if ":" not in timeStr and "." in timeStr:
return "ttt"
if ":" in timeStr:
if "." in timeStr:
return "tt"
else:
return "t"
logging.debug("_detectTimeFormat: Failed: unable to detect time format.")
return None
def _detectLineFormat(self, line):
havePid = False
haveTime = ""
haveTimeSpent = False
havePointer = False
remainLine = line
m = re.match(r"([0-9:. ]*)([a-z]+\(.*[ ]+=[ ]+[-0-9]+)(.*)", line)
m2 = re.match(r"([0-9:. ]*)(\[[0-9a-z]*\] )([a-z]+\(.*[ ]+=[ ]+[-0-9]+)(.*)", line)
if m:
pre = m.group(1)
mid = m.group(2)
post = m.group(3)
elif m2:
pre = m2.group(1)
pointer = m2.group(2)
mid = m2.group(3)
post = m2.group(4)
havePointer = True
else:
# print "_detectLineFormat: Failed: unable to match the line, give up detection."
return
if pre != '':
preList = pre.strip().split()
if len(preList) > 2:
print "_detectLineFormat: Failed: more the 2 parts in pre."
return
if len(preList) == 2:
haveTime = self._detectTimeFormat(preList[1])
havePid = True
else:
if ':' in pre or '.' in pre:
havePid = False
haveTime = self._detectTimeFormat(preList[0])
else:
havePid = True
haveTime = ""
if post != '':
if re.search(r"(<[0-9.]+>)", line):
haveTimeSpent = True
else:
haveTimeSpent = False
straceOptions = {}
straceOptions["havePid"] = havePid
straceOptions["haveTime"] = haveTime
straceOptions["havePointer"] = havePointer
straceOptions["haveTimeSpent"] = haveTimeSpent
return straceOptions
def _parse(self, reader, straceOptions):
syscallListByPid = {}
unfinishedSyscallStack = {}
if not reader:
logging.error("Cannot read file")
return
for line in reader:
if "restart_syscall" in line: # TODO: ignore this first
continue
if "+++ exited with" in line:
continue
unfinishedSyscall = False
reconstructSyscall = False
if "<unfinished ...>" in line: # store the unfinished line for reconstruct
unfinishedSyscall = True
if straceOptions["havePid"]:
pid = (line.partition(" "))[0]
unfinishedSyscallStack[pid] = line
else:
unfinishedSyscallStack[0] = line
elif "resumed>" in line: # get back the unfinished line and reconstruct
if straceOptions["havePid"]:
pid = (line.partition(" "))[0]
if pid not in unfinishedSyscallStack:
continue # no <unfinished> line before, ignore
existLine = unfinishedSyscallStack[pid]
else:
if 0 not in unfinishedSyscallStack:
continue # no <unfinished> line before, ignore
existLine = unfinishedSyscallStack[0]
lineIndex = line.find("resumed>") + len("resumed>")
reconstructLine = existLine.replace("<unfinished ...>", line[lineIndex:])
reconstructSyscall = True
#print "debug reconstructed line:", line
# Parse the line
result = self._parseLine(line, straceOptions)
# hook here for every (raw) syscalls
if result:
if result["syscall"] in self._rawSyscallCallbackHook:
for func in self._rawSyscallCallbackHook[result["syscall"]]:
func(result)
if "ALL" in self._rawSyscallCallbackHook:
for func in self._rawSyscallCallbackHook["ALL"]:
func(result)
# determine if there is a completeSyscallResult
if unfinishedSyscall:
completeSyscallResult = None
elif reconstructSyscall:
completeSyscallResult = self._parseLine(reconstructLine, straceOptions)
else: # normal completed syscall
completeSyscallResult = result
# hook here for every completed syscalls:
if completeSyscallResult:
if completeSyscallResult["syscall"] in self._completeSyscallCallbackHook:
for func in self._completeSyscallCallbackHook[completeSyscallResult["syscall"]]:
func(completeSyscallResult)
if "ALL" in self._completeSyscallCallbackHook:
for func in self._completeSyscallCallbackHook["ALL"]:
func(completeSyscallResult)
return
def _timeStrToTime(self, timeStr, timeFormat):
""" _timeStrToTime
timeFormat: "t" = "%H:%M:%S"
"tt" = "%H:%M:%S.%f"
"ttt" = "timestamp.%f"
"""
if timeFormat == "ttt":
return datetime.utcfromtimestamp(float(timeStr))
else:
timeList = timeStr.split(":")
# in order to use datetime object for calculation, pad the time with 1970-1-1
# TODO: should handle the day boundary case in _parse function
if timeFormat == "tt":
secondList = timeList[2].split(".")
return datetime(1970, 1, 1, int(timeList[0]), int(timeList[1]), int(secondList[0]), int(secondList[1]))
else:
return datetime(1970, 1, 1, int(timeList[0]), int(timeList[1]), int(timeList[2]))
def _timeStrToDelta(self, timeStr):
return timedelta(seconds=float(timeStr))
def _parseLine(self, line, straceOptions):
#
# _parseLine
#
# It parse a complete line and return a dict with the following:
# pid : pid (if havePid enabled)
# startTime : start time of the call (if haveTime enabled)
# syscall : system call function
# args : a list of arguments ([] if no options)
# return : return value (+/- int string or hex number string or '?' (e.g. exit syscall)), not exist if it is an unfinished syscall
# timeSpent : time spent in syscall (if haveTimeSpent enable. But even so, it may not exist in some case (e.g. exit syscall) and None will be stored in this field)
# type : Type of syscall ("completed", "unfinished", "resumed")
#
# Return null if hit some error
#
# (Not implemented) signalEvent : signal event (no syscall, args, return)
#
result = {}
remainLine = line
try:
if straceOptions["havePid"]:
result["pid"], remainLine = remainLine.split(None, 1)
result["pid"] = result["pid"].encode('utf-8')
if straceOptions["haveTime"] != "":
timeStr, remainLine = remainLine.split(None, 1)
result["startTime"] = self._timeStrToTime(timeStr, straceOptions["haveTime"])
if straceOptions["havePointer"]:
pointerStr, remainLine = remainLine.split(None, 1)
m = self._rePointer.match(pointerStr)
result["pointer"] = m.group(1)
if "--- SIG" in remainLine: # a signal line
#result["signalEvent"] = remainLine
#return result
### Ignore signal line now
return
# If it is unfinished/resumed syscall, still parse it but let the
# caller (_parse) determine what to do
if "<unfinished ...>" in remainLine:
result["type"] = "unfinished"
m = self._reUnfinishedSyscall.match(remainLine)
result["syscall"] = m.group(1).encode('utf-8')
result["args"] = self._parseArgs(m.group(2).strip().encode('utf-8')) # probably only partal arguments
elif "resumed>" in remainLine:
result["type"] = "resumed"
m = self._reResumedSyscall.match(remainLine)
result["syscall"] = m.group(1).encode('utf-8')
result["args"] = self._parseArgs(m.group(2).strip().encode('utf-8')) # probably only partal arguments
result["return"] = m.group(3).encode('utf-8')
remainLine = m.group(4)
else:
# normal system call
result["type"] = "completed"
m = self._reCompleteSyscall.match(remainLine)
result["syscall"] = m.group(1).encode('utf-8')
result["args"] = self._parseArgs(m.group(2).strip().encode('utf-8'))
result["return"] = m.group(3).encode('utf-8')
remainLine = m.group(4)
remains = remainLine.split('>')
if len(remains) == 3:
result["return"] += remains[0] + '>'
remainLine = remains[1] + '>'
if result["type"] != "unfinished" and result["return"] == '-1':
m = re.search(r" \(([^\n]*)\)", remainLine)
if m:
result["error"] = m.group(1).encode('utf-8')
else:
result["error"] = ''
else:
result["error"] = ''
if result["type"] != "unfinished" and straceOptions["haveTimeSpent"]:
m = re.search(r"<([\d.]*)>", remainLine)
if m:
result["timeSpent"] = self._timeStrToDelta(m.group(1))
else:
result["timeSpent"] = ''
except AttributeError:
# logging.warning("_parseLine: Error parsing this line: " + line)
# print sys.exc_info()
#exctype, value, t = sys.exc_info()
#print traceback.print_exc()
#print sys.exc_info()
return
return result
def _countPrecedingBackslashes(self, s, pos):
initialPos = pos
while pos > 0 and s[pos-1] == '\\':
pos-=1
return (initialPos-pos)
def _parseStringArg(self, argString):
"""
Parses to the end of a string parameter.
argString must begin with a quote character. _parseStringArg() parses
to the corresponding terminating quote character.
Returns the parsed string (including quotes) and the unparsed
remainder of argString.
"""
searchEndSymbolStartAt = 1
while True:
endSymbolIndex = argString.find('"', searchEndSymbolStartAt)
if endSymbolIndex == -1:
logging.warning("_parseStringArg: strange, can't find end symbol in this arg:" + argString)
endSymbolIndex = 0
break
numPrecedingBackslashes = self._countPrecedingBackslashes(argString, endSymbolIndex)
if numPrecedingBackslashes % 2 == 1:
# if preceded by an odd number of backslashes, the quote character is escaped
searchEndSymbolStartAt = endSymbolIndex + 1
else:
break
return ( argString[0:endSymbolIndex+1], argString[endSymbolIndex+1:] )
def _parseBlockArg(self, argString, parseBlock=False):
"""
Parses a list of arguments, recursing into blocks.
argString must be a string of comma-separated arguments.
If parseBlock is True, argString must start with [ or {,
and _parseBlockArg() will only parse to the end of the matching
bracket.
Returns the parsed arguments and the unparsed remainder of argString.
"""
endSymbols = {'{':'}', '[':']', '"':'"'}
resultArgs = []
currIndex = 0
if parseBlock:
endChar = endSymbols[argString[0]]
currIndex+=1
lengthArgString = len(argString)
remainderString = argString
while currIndex < lengthArgString:
if argString[currIndex] == ' ': # ignore space
currIndex += 1
continue
content = None
if argString[currIndex] == '"':
# inner string; parse recursively till end of string
(content, remainderString) = self._parseStringArg(argString[currIndex:])
elif argString[currIndex] in ['{', '[']:
# inner block; parse recursively till end of this block
(content, remainderString) = self._parseBlockArg(argString[currIndex:], True)
else:
# normal parameter; find next comma
remainderString = argString[currIndex:]
nextCommaPos = remainderString.find(', ')
if parseBlock:
nextTerminatorPos = remainderString.find(endChar)
if nextTerminatorPos == -1:
logging.warning("_parseBlockArg: strange, can't find end symbol '%s' in this arg: '%s'" % (endChar, argString))
return (argString, "")
else:
nextTerminatorPos = lengthArgString
finished = False
if nextCommaPos == -1 or nextTerminatorPos < nextCommaPos:
# we've parsed last parameter in block
contentString = remainderString[:nextTerminatorPos]
remainderString = remainderString[nextTerminatorPos+1:]
finished = True
elif nextTerminatorPos > nextCommaPos:
# there is another parameter in this block:
contentString = remainderString[:nextCommaPos]
remainderString = remainderString[nextCommaPos+1:]
else:
assert False, "internal error (this case shouldn't be hit)"
if content is None:
# block parser didn't return any value, or current parameter is a non-block value;
# so use entire raw string as "content"
content = contentString
resultArgs.append(content)
if finished:
break
assert(remainderString)
currIndex = len(argString) - len(remainderString)
currIndex+=1
return (resultArgs, remainderString)
def _parseArgs(self, argString):
"""
Parses an argument string and returns a (possibly nested) list of arguments.
"""
endSymbol = {'{':'}', '[':']', '"':'"'}
# short-cut: if there is no {, [, " in the whole argString, use split
if all([sym not in argString for sym in endSymbol.keys()]):
# remove the comma and space at the end of argString, then split
# it by ', '
resultArgs = argString.rstrip(' ,').split(', ')
# remove all empty split
return filter(len, resultArgs)
# otherwise, use a complex method to break the argument list, in order
# to ensure the comma inside {}, [], "" would not break things.
(content, remainderString) = self._parseBlockArg(argString, False)
assert not(remainderString), "remainder left after parsing: '%s'" % remainderString
return content
class StatBase(object):
""" The base class of stat plugins """
def optionHelp(self):
""" Should return a dict for all options for this plugin.
The dict keys are the option names and dict Values are the
description of the options.
E.g. {"output":"Write the output to this file instead of stdout"}
It will be used for a help text in the command line. And it will
be used to check if user input a correct option: If an
option is specified for this plugin by user but it is not specified
here, the command line will show error.
"""
return {}
def setOption(self, pluginOptionDict):
""" The pluginOptionDict contains the key value pair of options for
specified by user in the command line for this plugin.
E.g. {"output":"/tmp/output.txt"}
If no option specified, pluginOptionDict will be an empty dict ({}).
Return False if there is some problem in the options so that this
plugin would not be used.
"""
return True
def isOperational(self, straceOptions):
""" Should return true if this plugin works in the current strace
options.
The straceOptions should be a dict contains at least:
straceOptions["havePid"] = 1/0
straceOptions["haveTime"] = "", "t", "tt", or "ttt"
straceOptions["haveTimeSpent"] = 1/0
If isOperational return false, the register function will not be
called.
"""
return True
def getSyscallHooks(self):
""" Hook the processing function for each completed syscall.
The uncomplete/resumed syscall will be merged before passing to the
hook function. And if it cannot merged then it will be ignored.
(If you want to get uncomplete/resumed saperately, use
getRawSyscallHooks instead.)
Should return a dict with key = syscall name and value = hook function
E.g. return_dict["open"] = self.funcHandleOpenSyscall
return_dict["close"] = self.funcHandleCloseSyscall
return_dict["ALL"] = self.funcHandleALLSyscall
"""
return None
def getRawSyscallHooks(self):
""" Hook the processing function for each syscall (which may be
unfinished/resumed)
Should return a dict similar to that of getSyscallHooks
"""
return None
def jsonOutput(self):
""" Should print the output to console. Would be called after parsing is
finished.
"""
pass
class StatProcessTree(StatBase):
""" Print the process fork tree in the strace file """
def __init__(self):
self._allPid = set()
self._childDict = defaultdict(list)
self._childExecName = {}
self.processtree = {}
self.processtree['result'] = ''
def isOperational(self, straceOptions):
if not straceOptions["havePid"]:
return False
return True
def getSyscallHooks(self):
return {"ALL": self.statProcessTree}
def statProcessTree(self, result):
if "pid" not in result:
# logging.warning("statProcessTree: no pid info in line")
return
pid = result["pid"]
self._allPid.add(pid)
if result["syscall"] in ['clone', 'fork', 'vfork']:
if result["return"] != '?':
childPid = result["return"]
self._childDict[pid].append(childPid)
# Copy the execuation name of parent process to child process.
# It will be overwritten by next execve call of child
if pid in self._childExecName:
if result["syscall"] == 'clone':
self._childExecName[childPid] = "(clone)"
elif result["syscall"] in ['fork', 'vfork']:
self._childExecName[childPid] = "(fork)"
if result["syscall"] == "execve":
exe_cmd = ''
for s in result["args"][1]:
if ' ' in s:
s = s
else:
s = s.replace('"', '')
exe_cmd = exe_cmd + s + ' '
exe_cmd = exe_cmd[:-1]
self._childExecName[pid] = exe_cmd
def getProcessChildern(self, pid):
return self._childDict[pid]
def getProcessExecName(self, pid):
return self._childExecName[pid]
def jsonOutput(self):
# headPid = remove child pid in _allPid, so it contains only head pid
headPid = self._allPid
for childPidList in self._childDict.values():
for childPid in childPidList:
headPid.remove(childPid)
self.processtree['result'] = self._printTree(sorted(headPid))
return self.processtree
def _printTree(self, pids, indent='', level=0):
r = []
for n, pid in enumerate(pids):
if level == 0:
s, cs = '', ''
elif n < len(pids) - 1:
s, cs = ' ├─', ' │ '
else:
s, cs = ' └─', ' '
if pid in self._childExecName:
name = self._childExecName[pid]
children = sorted(self._childDict[pid])
if children:
ccs = ' │ '
else:
ccs = ' '
name = name.replace('\n', '\n' + indent + cs + ccs + ' ')
r.append(indent + s + '{} {}\n'.format(pid, name))
r.append(self._printTree(children, indent+cs, level+1))
return ''.join(r)
class StatFileIO(StatBase):
""" Stat and print file IO of strace"""
def __init__(self):
self._fileStatList = {}
self._fidStatList = {}
self._pluginOptionDict = {}
self._straceOptions = {}
self.fileopts = {}
self.fileopts['failed'] = []
self.fileopts['sucess'] = {}
self.fileopts['sucess']['total_file'] = []
self.fileopts['sucess']['read'] = []
self.fileopts['sucess']['write'] = []
self.fileopts['sucess']['open'] = []
self.fileopts['sucess']['modify'] = []
def optionHelp(self):
return {"output":"Write the output to this file instead of stdout"}
def setOption(self, pluginOptionDict):
self._pluginOptionDict = pluginOptionDict
return True
def getSyscallHooks(self):
return_dict = {}
for syscall in ["read", "write", "open", "openat", "close"]:
return_dict[syscall] = self.statFileIO
return return_dict
def isOperational(self, straceOptions):
self._straceOptions = straceOptions
return True
def statFileIO(self, result):
if result["syscall"] in ["read", "write", "open", "openat", "close"]:
if result["return"] == "-1":
open_error = {}
if result["syscall"] == "open":
open_error['pid'] = int(result["pid"]) if self._straceOptions["havePid"] else 0
open_error['error'] = result["error"]
open_error['file'] = result["args"][0].replace('"', '')
self.fileopts['failed'].append(open_error)
elif result["syscall"] == "openat":
open_error['pid'] = int(result["pid"]) if self._straceOptions["havePid"] else 0
open_error['error'] = result["error"]
open_error['file'] = result["args"][1].replace('"', '')
self.fileopts['failed'].append(open_error)
return
if result["syscall"] in ["open", "openat"]:
fid = result["return"]
else:
fid = result["args"][0]
if self._straceOptions["havePid"]:
pid = int(result["pid"])
else:
pid = 0
if pid not in self._fidStatList:
self._fidStatList[pid] = {}
if pid not in self._fileStatList:
self._fileStatList[pid] = {}
# file close
if result["syscall"] == "close":
if fid in self._fidStatList[pid]:
#print self._fidStatList[fid]
filename = self._fidStatList[pid][fid][0]
if filename not in self._fileStatList[pid]:
self._fileStatList[pid][filename] = [1, self._fidStatList[pid][fid][1], self._fidStatList[pid][fid][2], self._fidStatList[pid][fid][3], self._fidStatList[pid][fid][4], self._fidStatList[pid][fid][5], self._fidStatList[pid][fid][6], self._fidStatList[pid][fid][7], self._fidStatList[pid][fid][8]]
else:
self._fileStatList[pid][filename][0] += 1
for i in [1, 2, 3, 4, 5, 6, 7, 8]:
self._fileStatList[pid][filename][i] += self._fidStatList[pid][fid][i]
#file close
self._fileStatList[pid][filename][8] = 1
#when file close del fid avoid the same fid
del self._fidStatList[pid][fid]
# else if fid not in self._fidStatList[pid] and this is a close syscall, just ignore and return
return
# if read/write/open
if fid not in self._fidStatList[pid]:
if result["syscall"] == "open":
# self._fidStatList[pid][fid] = [filename, read count, read acc bytes, write count, write acc bytes, read data, write data, fid, isclose]
self._fidStatList[pid][fid] = [result["args"][0], 0, 0, 0, 0, '', '', fid, 0]
elif result["syscall"] == "openat":
self._fidStatList[pid][fid] = [result["args"][1], 0, 0, 0, 0, '', '', fid, 0]
else:
self._fidStatList[pid][fid] = ["unknown:"+fid, 0, 0, 0, 0, '', '', fid, 0]
# ISSUE #8: if fid in self._fidStatList[pid] but the syscall is open/openat, that mean
# we missed a close syscall, we should update _fileStatList before we move on
# stat read/write
if result["syscall"] == "read":
self._fidStatList[pid][fid][1] += 1
try:
self._fidStatList[pid][fid][2] += int(result["return"])
except Exception as e:
pass
self._fidStatList[pid][fid][5] += result["args"][1][1:-1].decode("string_escape")
if result["syscall"] == "write":
self._fidStatList[pid][fid][3] += 1
self._fidStatList[pid][fid][4] += int(result["return"])
self._fidStatList[pid][fid][6] += result["args"][1][1:-1].decode("string_escape")
return
def jsonOutput(self):
for pid in self._fidStatList:
for fid in self._fidStatList[pid]:
#print self._fidStatList[pid][fid]
filename = self._fidStatList[pid][fid][0]
if filename not in self._fileStatList[pid]:
# pass
# unclosed file
self._fileStatList[pid][filename] = [1] + self._fidStatList[pid][fid][1:9]
else:
self._fileStatList[pid][filename][0] += 1
for i in [1, 2, 3, 4, 5, 6, 7, 8]:
self._fileStatList[pid][filename][i] += self._fidStatList[pid][fid][i]
for pid in self._fileStatList:
for filename in self._fileStatList[pid]:
if filename.startswith('unknown:'):
continue
self.fileopts['sucess']['total_file'].append(filename[1:-1])
#read
if self._fileStatList[pid][filename][2] > 0:
tmp_read = {}
if self._straceOptions["havePid"]:
tmp_read['pid'] = pid
tmp_read['file_name'] = filename
tmp_read['is_close'] = self._fileStatList[pid][filename][8]
tmp_read['fd'] = self._fileStatList[pid][filename][7]
tmp_read['read_count'] = self._fileStatList[pid][filename][1]
tmp_read['read_bytes'] = self._fileStatList[pid][filename][2]
tmp_read['data'] = self._fileStatList[pid][filename][5]
self.fileopts['sucess']['read'].append(tmp_read)
#write
if self._fileStatList[pid][filename][4] > 0:
tmp_write = {}
if self._straceOptions["havePid"]:
tmp_write['pid'] = pid
tmp_write['file_name'] = filename
tmp_write['is_close'] = self._fileStatList[pid][filename][8]
tmp_write['fd'] = self._fileStatList[pid][filename][7]
tmp_write['write_count'] = self._fileStatList[pid][filename][3]
tmp_write['write_bytes'] = self._fileStatList[pid][filename][4]
tmp_write['data'] = self._fileStatList[pid][filename][6]
self.fileopts['sucess']['write'].append(tmp_write)
#read & write
if self._fileStatList[pid][filename][2] > 0 and self._fileStatList[pid][filename][4] > 0:
tmp_modify = {}
if self._straceOptions["havePid"]:
tmp_modify['pid'] = pid
tmp_modify['file_name'] = filename
self.fileopts['sucess']['modify'].append(tmp_modify)
#open only
if self._fileStatList[pid][filename][2] == 0 and self._fileStatList[pid][filename][4] == 0:
tmp_open = {}
if self._straceOptions["havePid"]:
tmp_open['pid'] = pid
tmp_open['file_name'] = filename
self.fileopts['sucess']['open'].append(tmp_open)
return self.fileopts
class StatSpecialSyscall(StatBase):
""" Stat and print DynamicAnalysis of strace"""
def __init__(self):
self._pluginOptionDict = {}
self._straceOptions = {}
self.mkdir_syscalls = ["mkdir", "mkdirat"]
self.open_syscalls = ["open"]
self.write_syscalls = ["write"]
self.unlink_syscalls = ["unlink", "unlinkat"]
self.execve_syscalls = ["execve"]
self.socket_syscalls = ["socket"]
self.connect_syscalls = ["connect"]
self.kill_syscalls = ["kill", "killpg"]
self.syscalls = []
self.syscalls.extend(self.mkdir_syscalls)
self.syscalls.extend(self.open_syscalls)
self.syscalls.extend(self.write_syscalls)
self.syscalls.extend(self.unlink_syscalls)
self.syscalls.extend(self.execve_syscalls)
self.syscalls.extend(self.socket_syscalls)
self.syscalls.extend(self.connect_syscalls)
self.syscalls.extend(self.kill_syscalls)
self.specials = {}
self.specials['mkdir'] = []
self.specials['create'] = []
self.specials['unlink'] = []
self.specials['execve'] = []
self.specials['network'] = []
self.specials['kill'] = []
self.specials['stdout'] = []
self.specials['stderr'] = []
self.network = {}
self.network['socket'] = []
self.network['connect'] = []
def optionHelp(self):
return {"output":"Write the output to this file instead of stdout"}
def setOption(self, pluginOptionDict):
self._pluginOptionDict = pluginOptionDict
return True
def getSyscallHooks(self):
return_dict = {}
for syscall in self.syscalls:
return_dict[syscall] = self.statDynamicAnalysis
return return_dict
def isOperational(self, straceOptions):
self._straceOptions = straceOptions
return True
def parseSpecials(self, result, type, index=0):
tmp_specials = {}
tmp_specials['pid'] = int(result["pid"]) if self._straceOptions["havePid"] else 0
tmp_specials['file_name'] = result["args"][index]
if tmp_specials['file_name'][0] == '"' and tmp_specials['file_name'][-1] == '"':
tmp_specials['file_name'] = tmp_specials['file_name'][1:-1]
if result["return"] == "-1":
tmp_specials['error'] = 1
tmp_specials['error_info'] = result['error']
else:
tmp_specials['error'] = 0
self.specials[type].append(tmp_specials)
def statDynamicAnalysis(self, result):
if result["syscall"] in self.syscalls:
if self._straceOptions["havePid"]:
pid = int(result["pid"])
else:
pid = 0
if result["syscall"] in self.mkdir_syscalls:
if result["syscall"] == 'mkdirat':
self.parseSpecials(result, 'mkdir', 1)
elif result["syscall"] == 'mkdir':
self.parseSpecials(result, 'mkdir')
if result["syscall"] in self.open_syscalls and 'O_CREAT' in result["args"][1]:
self.parseSpecials(result, 'create')
if result["syscall"] in self.write_syscalls and (result["args"][0] == '1' or result["args"][0].startswith('1<')):
tmp_stdout = {}
tmp_stdout['pid'] = int(result["pid"]) if self._straceOptions["havePid"] else 0
tmp_stdout['data'] = result["args"][1][1:-1]
tmp_stdout['bytes'] = result["args"][2]
self.specials['stdout'].append(tmp_stdout)
if result["syscall"] in self.write_syscalls and (result["args"][0] == '2' or result["args"][0].startswith('2<')):
tmp_stderr = {}
tmp_stderr['pid'] = int(result["pid"]) if self._straceOptions["havePid"] else 0
tmp_stderr['data'] = result["args"][1][1:-1]
tmp_stderr['bytes'] = result["args"][2]
self.specials['stderr'].append(tmp_stderr)
if result["syscall"] in self.unlink_syscalls:
if result["syscall"] == 'unlinkat':
self.parseSpecials(result, 'unlink', 1)
elif result["syscall"] == 'unlink':
self.parseSpecials(result, 'unlink')
if result["syscall"] in self.execve_syscalls:
tmp_execve = {}
tmp_execve['pid'] = int(result["pid"]) if self._straceOptions["havePid"] else 0
exe_cmd = ''
for s in result["args"][1]:
if ' ' in s:
s = s
else:
s = s.replace('"', '')
exe_cmd = exe_cmd + s + ' '
exe_cmd = exe_cmd[:-1]
tmp_execve['command'] = exe_cmd
if result["return"] == "-1":
tmp_execve['error'] = 1
tmp_execve['error_info'] = result['error']
else:
tmp_execve['error'] = 0
self.specials['execve'].append(tmp_execve)
if result["syscall"] in self.socket_syscalls:
tmp_socket = {}
tmp_socket['pid'] = int(result["pid"]) if self._straceOptions["havePid"] else 0
tmp_socket['args'] = result["args"]
tmp_socket['fd'] = str(result["return"])
tmp_socket['timestamp'] = result["startTime"]
self.network['socket'].append(tmp_socket)
if result["syscall"] in self.connect_syscalls:
tmp_connect = {}
tmp_connect['pid'] = int(result["pid"]) if self._straceOptions["havePid"] else 0
tmp_connect['fd'] = str(result["args"][0])
tmp_connect['timestamp'] = result["startTime"]
tmp_connect['args'] = result["args"][1]
self.network['connect'].append(tmp_connect)
if result["syscall"] in self.kill_syscalls:
tmp_kill = {}
tmp_kill['pid'] = int(result["pid"]) if self._straceOptions["havePid"] else 0
tmp_kill['kill_pid'] = result["args"][0]
if result["return"] == "-1":
tmp_kill['error'] = 1
tmp_kill['error_info'] = result['error']
else:
tmp_kill['error'] = 0
self.specials['kill'].append(tmp_kill)
def jsonOutput(self):
last_time = datetime.utcfromtimestamp(0)
# print self.network['connect']
# print self.network['socket']
# FIXME
for conn in self.network['connect']:
for socks in self.network['socket']:
if conn['pid'] == socks['pid'] and conn['fd'] == socks['fd']:
if (conn['timestamp'] > last_time) and (conn['timestamp'] > socks['timestamp']):
tmp_socket = {}
tmp_socket['timestamp'] = str(conn['timestamp'])
tmp_socket['pid'] = conn['pid']
tmp_socket['fd'] = conn['fd']
tmp_socket['socket_args'] = socks['args']
tmp_socket['connect_args'] = conn['args']
self.specials['network'].append(tmp_socket)
self.network['socket'].remove(socks)
last_time = conn['timestamp']
break
else:
last_time = socks['timestamp']
return self.specials
class StatStatics(StatBase):
""" Summarize of syscall of strace, like strace -c output"""
def __init__(self):
self._syscallCount = defaultdict(int)
self._syscallTime = defaultdict(timedelta)
self._straceOptions = {}
self.result = {}
self.syscall_order = ''
self.result['summary'] = ''
self.result['syscall'] = []
#self.result['syscall_ssdeep'] = ''
def getSyscallHooks(self):
return {"ALL": self.record}
def isOperational(self, straceOptions):
self._straceOptions = straceOptions
if not straceOptions["haveTimeSpent"]:
return False
return True
def record(self, result):
self._syscallCount[result["syscall"]] += 1
if result["timeSpent"]:
self._syscallTime[result["syscall"]] += result["timeSpent"]
tmp_syscall = {}
tmp_syscall['pid'] = int(result["pid"]) if self._straceOptions["havePid"] else 0
tmp_syscall['timestamp'] = str(result["startTime"])
tmp_syscall['pointer'] = result['pointer'] if self._straceOptions["havePointer"] else 0
tmp_syscall['syscall'] = result["syscall"]
self.syscall_order += result["syscall"]
tmp_syscall['args'] = result["args"]
tmp_syscall['return'] = result["return"]
tmp_syscall['error'] = result['error']
tmp_syscall['timespent'] = str(result["timeSpent"])
self.result['syscall'].append(tmp_syscall)
def jsonOutput(self):
#self.result['syscall_ssdeep'] = ssdeep.hash(self.syscall_order)
jsonresult = ''
jsonresult += "\n time seconds calls syscall"
jsonresult += "\n------ ----------- --------- ----------------"