-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_timeline.py
More file actions
1374 lines (1352 loc) · 71.9 KB
/
create_timeline.py
File metadata and controls
1374 lines (1352 loc) · 71.9 KB
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 python3
# -*- coding: utf-8 -*-
#Create Json timeline from linux/aix artefacts - Import in TimeSketch !!
# (c) 2019 - 2022, Lionel PRAT <lionel.prat9 (at) gmail.com>
#TODO:
# add tag in file for:
# - type =! ext =! entropy
# create stat on type and entropy, get moy and identify file > moy
# create stat of file type uniq & sort
# - md5sum malicious (source: MISP, Threat Intel)
# - much io/fd use
import sys
import os
import json
import re
from datetime import datetime, timedelta;
#convert mounth to number "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
# "janv." "févr." "mars" "avr." "mai" "juin" "juil." "août" "sept." "oct." "nov." "déc."
def main():
print("Create Json timeline from linux/aix artefacts by lionel.prat9 (at) gmail.com\n")
if len(sys.argv) != 2:
print("Usage: ./create_timeline.py dir_artefac_extract/\n")
sys.exit()
print("Wait finish parsing...\n")
filepath = sys.argv[1]
##############
debug=True
find='/all_files'
find_file='/all_files_file' #aix case
package_list_deb='/packages_deb-list_files'
package_list_rpm='/packages_rpm-list_files'
package_list_aix='/packages_aix-list_files'
kernel_module_file='/kernel_modules'
crontab_file='/crontab'
env_file='/env'
tmpfs_file='/disk_mount'
networks_file='/network'
package_list_deb_int='/packages-integrity-deb'
package_list_rpm_int='/packages-integrity-rpm' #http://ftp.rpm.org/max-rpm/s1-rpm-verify-output.html
proccksum_file='/process_cksum'
process_faout_file='/process_faout'
procexe_file='/process_exe'
procldd_file='/process_ldd'
procfd_file='/process_fd'
proclsof_file='/process_lsof'
procps_file='/process_ps'
systemd_file='service-systemd'
initd_file='/services-initd_exe'
aix_services='/services'
package_cve_deb_file='/debsecan'
package_cve_rpm_file='/yum-security'
hostname_file='/general'
sudoers_file='/sudoers'
##############
if not os.path.isdir(filepath):
print("Directory path {} does not exist.".format(filepath))
sys.exit()
#now
now = datetime.now()
#GET HOSTNAME
hostname = ""
if not os.path.isfile(filepath+hostname_file):
print("File extracted hostname {} does not exist.".format(filepath+hostname_file))
else:
print("Extract info hostname...")
with open(filepath+hostname_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
get=False
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r")
if get:
hostname=tmp.strip()
print("Hostname is {}".format(hostname))
break
if tmp.startswith('host:'):
get=True
#DEB PACKAGES LIST CVE
package_cve_deb = []
if not os.path.isfile(filepath+package_cve_deb_file):
print("File extracted package list DEB CVE {} does not exist.".format(filepath+package_cve_deb_file))
else:
print("Extract info package DEB CVE")
with open(filepath+package_cve_deb_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
package_current=''
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if len(tmp) > 1:
if not tmp[1] in package_cve_deb:
package_cve_deb.append(tmp[1])
if debug:
with open('result-debcve.json', 'w') as fp:
json.dump(package_cve_deb, fp, indent=4)
#DEB PACKAGES LIST
package_files_deb_service = []
package_files_deb = {}
if not os.path.isfile(filepath+package_list_deb):
print("File extracted package list DEB {} does not exist.".format(filepath+package_list_deb))
else:
print("Extract info package DEB")
with open(filepath+package_list_deb, encoding="utf-8", errors='ignore') as fp:
cnt = 0
package_current=''
cve=False
for line in fp:
#print("line {} contents {}".format(cnt, line))
if line.startswith('Package Name:'):
rtmp=line.split(':')
if rtmp and len(rtmp) > 1:
package_current=rtmp[1].strip()
elif '/.' == line:
continue
else:
#verify service in package
if line.rstrip("\n\r").startswith('/etc/rc') or line.rstrip("\n\r").startswith('/etc/systemd/') or line.rstrip("\n\r").startswith('/etc/init.d/') or line.rstrip("\n\r").startswith('/etc/xinet') or line.rstrip("\n\r").startswith('/etc/rc.d/init.d/'):
if not package_current in package_files_deb_service:
package_files_deb_service.append(package_current)
if line.rstrip("\n\r") in package_files_deb:
package_files_deb[line.rstrip("\n\r")]=[package_current]+package_files_deb[line.rstrip("\n\r")]
else:
package_files_deb[line.rstrip("\n\r")]=[package_current]
if debug:
with open('result-deb.json', 'w') as fp:
json.dump(package_files_deb, fp, indent=4)
#DEB PACKAGES LIST INTEGRITY
package_files_deb_int = []
if not os.path.isfile(filepath+package_list_deb_int):
print("File extracted package list DEB integrity {} does not exist.".format(filepath+package_list_deb_int))
else:
print("Extract info package DEB integrity ")
with open(filepath+package_list_deb_int, encoding="utf-8", errors='ignore') as fp:
cnt = 0
package_current=''
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if len(tmp) == 3:
if not tmp[2] in package_files_deb_int:
package_files_deb_int.append(tmp[2])
elif len(tmp) == 2:
if tmp[1].startswith('/') and not tmp[1] in package_files_deb_int:
package_files_deb_int.append(tmp[1])
if debug:
with open('result-debint.json', 'w') as fp:
json.dump(package_files_deb_int, fp, indent=4)
#RPM PACKAGES LIST CVE
package_cve_rpm = []
if not os.path.isfile(filepath+package_cve_rpm_file):
print("File extracted package list RPM CVE {} does not exist.".format(filepath+package_cve_rpm_file))
else:
print("Extract info package RPM CVE")
with open(filepath+package_cve_rpm_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
package_current=''
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if len(tmp) > 2 and 'security' in tmp[1]:
if not tmp[2] in package_cve_rpm:
package_cve_rpm.append(tmp[2])
if debug:
with open('result-rpmcve.json', 'w') as fp:
json.dump(package_cve_rpm, fp, indent=4)
#RPM PACKAGES LIST
package_files_rpm = {}
package_files_rpm_service = []
if not os.path.isfile(filepath+package_list_rpm):
print("File extracted package list RPM {} does not exist.".format(filepath+package_list_rpm))
else:
print("Extract info package RPM")
with open(filepath+package_list_rpm, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
if line.startswith('Package Name:'):
rtmp=line.split(':')
if rtmp and len(rtmp) > 1:
package_current=rtmp[1].strip()
else:
#verify service in package
if line.rstrip("\n\r").startswith('/etc/rc') or line.rstrip("\n\r").startswith('/etc/systemd/') or line.rstrip("\n\r").startswith('/etc/init.d/') or line.rstrip("\n\r").startswith('/etc/xinet') or line.rstrip("\n\r").startswith('/etc/rc.d/init.d/'):
if not package_current in package_files_rpm_service:
package_files_rpm_service.append(package_current)
if line.rstrip("\n\r") in package_files_rpm:
package_files_rpm[line.rstrip("\n\r")]=[package_current]+package_files_rpm[line.rstrip("\n\r")]
else:
package_files_rpm[line.rstrip("\n\r")]=[package_current]
if debug:
with open('result-rpm.json', 'w') as fp:
json.dump(package_files_rpm, fp, indent=4)
#RPM PACKAGES LIST INTEGRITY - http://ftp.rpm.org/max-rpm/s1-rpm-verify-output.html
package_files_rpm_int = []
if not os.path.isfile(filepath+package_list_rpm_int):
print("File extracted package list RPM integrity {} does not exist.".format(filepath+package_list_rpm_int))
else:
print("Extract info package RPM integrity ")
with open(filepath+package_list_rpm_int, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if len(tmp) == 3:
if tmp[2].startswith('/') and not tmp[2] in package_files_rpm_int:
package_files_rpm_int.append(tmp[2])
elif len(tmp) == 2:
if tmp[1].startswith('/') and not tmp[1] in package_files_rpm_int:
package_files_rpm_int.append(tmp[1])
if debug:
with open('result-rpmint.json', 'w') as fp:
json.dump(package_files_rpm_int, fp, indent=4)
#AIX PACKAGES LIST
package_files_aix = {}
package_files_aix_service = []
if not os.path.isfile(filepath+package_list_aix):
print("File extracted package list AIX {} does not exist.".format(filepath+package_list_aix))
else:
print("Extract info package AIX")
with open(filepath+package_list_aix, encoding="utf-8", errors='ignore') as fp:
cnt = 0
package_current=''
for line in fp:
#print("line {} contents {}".format(cnt, line))
if line.startswith('Package Name:'):
rtmp=line.split(':')
if rtmp and len(rtmp) > 1:
package_current=rtmp[1].strip()
else:
tmp=line.rstrip("\n\r").strip()
if tmp.startswith('Path:') or tmp.startswith('----') or tmp.startswith('Fileset') or tmp.startswith('NONE'):
continue
else:
if tmp.startswith('/'):
if ' -> ' in tmp:
ftmp=tmp.split(' -> ')
#verify service in package
if ftmp[0].startswith('/etc/rc') or ftmp[0].startswith('/etc/systemd/') or ftmp[0].startswith('/etc/init.d/') or ftmp[0].startswith('/etc/xinet') or ftmp[0].startswith('/etc/rc.d/init.d/') or ftmp[1].startswith('/etc/rc') or ftmp[1].startswith('/etc/systemd/') or ftmp[1].startswith('/etc/init.d/') or ftmp[1].startswith('/etc/xinet') or ftmp[1].startswith('/etc/rc.d/init.d/'):
if not package_current in package_files_aix_service:
package_files_aix_service.append(package_current)
if ftmp[0] in package_files_aix:
package_files_aix[ftmp[0]]=[package_current]+package_files_aix[ftmp[0]]
else:
package_files_aix[ftmp[0]]=[package_current]
if ftmp[1] in package_files_aix:
package_files_aix[ftmp[1]]=[package_current]+package_files_aix[ftmp[1]]
else:
package_files_aix[ftmp[1]]=[package_current]
else:
#verify service in package
if tmp.startswith('/etc/rc') or tmp.startswith('/etc/systemd/') or tmp.startswith('/etc/init.d/') or tmp.startswith('/etc/xinet') or tmp.startswith('/etc/rc.d/init.d/'):
if not package_current in package_files_aix_service:
package_files_aix_service.append(package_current)
if tmp in package_files_aix:
package_files_aix[tmp]=[package_current]+package_files_aix[tmp]
else:
package_files_aix[tmp]=[package_current]
elif '/' in tmp:
tmpx=tmp.split('/')
tmp='/'+'/'.join(tmpx[1:])
if ' -> ' in tmp:
ftmp=tmp.split(' -> ')
#verify service in package
if ftmp[0].startswith('/etc/rc') or ftmp[0].startswith('/etc/systemd/') or ftmp[0].startswith('/etc/init.d/') or ftmp[0].startswith('/etc/xinet') or ftmp[0].startswith('/etc/rc.d/init.d/') or ftmp[1].startswith('/etc/rc') or ftmp[1].startswith('/etc/systemd/') or ftmp[1].startswith('/etc/init.d/') or ftmp[1].startswith('/etc/xinet') or ftmp[1].startswith('/etc/rc.d/init.d/'):
if not package_current in package_files_aix_service:
package_files_aix_service.append(package_current)
if ftmp[0] in package_files_aix:
package_files_aix[ftmp[0]]=[package_current]+package_files_aix[ftmp[0]]
else:
package_files_aix[ftmp[0]]=[package_current]
if ftmp[1] in package_files_aix:
package_files_aix[ftmp[1]]=[package_current]+package_files_aix[ftmp[1]]
else:
package_files_aix[ftmp[1]]=[package_current]
else:
#verify service in package
if tmp.startswith('/etc/rc') or tmp.startswith('/etc/systemd/') or tmp.startswith('/etc/init.d/') or tmp.startswith('/etc/xinet') or tmp.startswith('/etc/rc.d/init.d/'):
if not package_current in package_files_aix_service:
package_files_aix_service.append(package_current)
if tmp in package_files_aix:
package_files_aix[tmp]=[package_current]+package_files_aix[tmp]
else:
package_files_aix[tmp]=[package_current]
if debug:
with open('result-aix.json', 'w') as fp:
json.dump(package_files_aix, fp, indent=4)
#AIX integrity TODO!
#kernel module loaded
kernel_modules = []
if not os.path.isfile(filepath+kernel_module_file):
print("File extracted kernel modules loaded{} does not exist.".format(filepath+kernel_module_file))
else:
print("Extract info kernel modules loaded")
with open(filepath+kernel_module_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
if line.startswith('/'):
kernel_modules.append(line.rstrip("\n\r"))
elif '/' in line:
#genkex AIX
tmpx=line.rstrip("\n\r").split('/')
kernel_modules.append('/'+'/'.join(tmpx[1:]))
if debug:
with open('result-kernelmodules.json', 'w') as fp:
json.dump(kernel_modules, fp, indent=4)
#sudoers_file
sudoers = []
if not os.path.isfile(filepath+sudoers_file):
print("File extracted sudoers call {} does not exist.".format(filepath+sudoers_file))
else:
print("Extract info sudoers call")
with open(filepath+sudoers_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
user_current=''
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if tmp and len(tmp) > 5:
for fpat in tmp[5:]:
if fpat.startswith('/') or fpat.startswith('./') or fpat.startswith('../'):
if not fpat in sudoers:
sudoers.append(fpat)
if debug:
with open('result-sudoers.json', 'w') as fp:
json.dump(sudoers, fp, indent=4)
#crontab
crontab = {}
if not os.path.isfile(filepath+crontab_file):
print("File extracted crontab call {} does not exist.".format(filepath+crontab_file))
else:
print("Extract info crontab call")
with open(filepath+crontab_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
user_current=''
for line in fp:
#print("line {} contents {}".format(cnt, line))
if line.startswith('Crontab user:'):
rtmp=line.split(':')
if rtmp and len(rtmp) > 1:
user_current=rtmp[1].strip()
else:
tmp=line.rstrip("\n\r").split()
if tmp and len(tmp) > 5:
for fpat in tmp[5:]:
if fpat.startswith('/') or fpat.startswith('./') or fpat.startswith('../'):
if fpat in crontab:
if not user_current in crontab[fpat]:
crontab[fpat]=[user_current]+crontab[fpat]
else:
crontab[fpat]=[user_current]
if debug:
with open('result-crontab.json', 'w') as fp:
json.dump(crontab, fp, indent=4)
#env
envar = {}
if not os.path.isfile(filepath+env_file):
print("File extracted env var {} does not exist.".format(filepath+env_file))
else:
print("Extract info env var")
with open(filepath+env_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
if '=' in line:
tmp=line.rstrip("\n\r").split('=')
if tmp and len(tmp) > 1:
var_current=tmp[0]
tmpx=tmp[1].split(':')
for tmpy in tmpx:
tmpz=tmpy.split()
for fpat in tmpz:
if fpat.startswith('/') or fpat.startswith('./') or fpat.startswith('../'):
fpat.split()
if fpat in envar:
if not var_current in envar[fpat]:
envar[fpat]=[var_current]+envar[fpat]
else:
envar[fpat]=[var_current]
if debug:
with open('result-env.json', 'w') as fp:
json.dump(envar, fp, indent=4)
#tmpfs
tmpfs = []
if not os.path.isfile(filepath+tmpfs_file):
print("File extracted tmpfs file {} does not exist.".format(filepath+tmpfs_file))
else:
print("Extract info tmpfs file")
with open(filepath+tmpfs_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
mode = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
if ' tmpfs ' in line and not ',noexec,' in line:
tmp=line.rstrip("\n\r").split()
if tmp and len(tmp) > 1:
for fpat in tmp:
if fpat.startswith('/'):
if not fpat in tmpfs:
tmpfs.append(fpat)
if debug:
with open('result-tmpfs.json', 'w') as fp:
json.dump(tmpfs, fp, indent=4)
#network
networks = []
if not os.path.isfile(filepath+networks_file):
print("File extracted network informations {} does not exist.".format(filepath+networks_file))
else:
print("Extract info network informations")
with open(filepath+networks_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if len(tmp) > 8 and ('TCP' in line or 'UDP' in line):
if not tmp[1] in networks:
networks.append(tmp[1])
if debug:
with open('result-net.json', 'w') as fp:
json.dump(networks, fp, indent=4)
#process exe
procexe = {}
if not os.path.isfile(filepath+procexe_file):
print("File extracted process exe {} does not exist.".format(filepath+procexe_file))
else:
print("Extract info process exe")
with open(filepath+procexe_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if len(tmp) == 11 and ' -> ' in line:
#extract pid
if '/proc/' in tmp[8]:
pidx=tmp[8].split('/')
if len(pidx) == 4:
pid=pidx[2]
if tmp[10] in procexe:
if not pid in procexe[tmp[10]]:
procexe[tmp[10]]=procexe[tmp[10]]+[pid]
else:
procexe[tmp[10]]=[pid]
if debug:
with open('result-procexe.json', 'w') as fp:
json.dump(procexe, fp, indent=4)
process_faout_file
#process aout use procexe var
if not os.path.isfile(filepath+process_faout_file):
print("File extracted process aout {} does not exist.".format(filepath+process_faout_file))
else:
print("Extract info process aout")
with open(filepath+process_faout_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if ':' in tmp[0]:
if tmp[0][0:-1].isdigit():
pid=tmp[0][0:-1]
for fpat in tmp[1:]:
if fpat.startswith('/'):
if fpat in procexe:
if not pid in procexe[fpat]:
procexe[fpat]=procexe[fpat]+[pid]
else:
procexe[fpat]=[pid]
if debug:
with open('result-procaout.json', 'w') as fp:
json.dump(procexe, fp, indent=4)
#process ldd
procldd = {}
if not os.path.isfile(filepath+procldd_file):
print("File extracted process ldd {} does not exist.".format(filepath+procldd_file))
else:
print("Extract info process ldd")
with open(filepath+procldd_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
current_pid=''
for line in fp:
#print("line {} contents {}".format(cnt, line))
if ':' in line:
pidx=line.split(':')
if len(pidx) == 2:
current_pid=pidx[0]
elif line.startswith('/'):
fpat=line.rstrip("\n\r")
#aix case
if fpat.endswith(']'):
tmpz=line.split('[')
fpat=tmpz[0]
if fpat in procldd:
if not current_pid in procldd[fpat]:
procldd[fpat]=procldd[fpat]+[current_pid]
else:
procldd[fpat]=[current_pid]
if debug:
with open('result-procldd.json', 'w') as fp:
json.dump(procldd, fp, indent=4)
#process fd
procfd = {}
if not os.path.isfile(filepath+procfd_file):
print("File extracted process fd {} does not exist.".format(filepath+procfd_file))
else:
print("Extract info process fd")
with open(filepath+procfd_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
current_pid=''
for line in fp:
#print("line {} contents {}".format(cnt, line))
if line.startswith('/proc/') and line.rstrip("\n\r").endswith(':'):
pidx=line.rstrip("\n\r").split('/')
if len(pidx) == 4:
current_pid=pidx[2]
elif ' -> ' in line:
tmp=line.rstrip("\n\r").split(' -> ')
if tmp and tmp[-1].startswith('/'):
fpat=tmp[-1]
if fpat in procfd:
if not current_pid in procfd[fpat]:
procfd[fpat]=procfd[fpat]+[current_pid]
else:
procfd[fpat]=[current_pid]
if debug:
with open('result-procfd.json', 'w') as fp:
json.dump(procfd, fp, indent=4)
#process lsof
proclsof = {}
if not os.path.isfile(filepath+proclsof_file):
print("File extracted process lsof {} does not exist.".format(filepath+proclsof_file))
else:
print("Extract info process lsof")
with open(filepath+proclsof_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if tmp[1].isdigit() and tmp[-1].startswith('/') and len(tmp[-1]) > 1:
pid=tmp[1]
fpat=tmp[-1]
if fpat in proclsof:
if not pid in proclsof[fpat]:
proclsof[fpat]=proclsof[fpat]+[pid]
else:
proclsof[fpat]=[pid]
if debug:
with open('result-proclsof.json', 'w') as fp:
json.dump(proclsof, fp, indent=4)
#services aix - pid
aixservices = []
if not os.path.isfile(filepath+aix_services):
print("File extracted Aix Services {} does not exist.".format(filepath+aix_services))
else:
print("Extract info Aix Services")
with open(filepath+aix_services, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if len(tmp) == 4 and tmp[2].isdigit():
if not tmp[2] in aixservices:
aixservices.append(tmp[1])
if debug:
with open('result-aixservices.json', 'w') as fp:
json.dump(aixservices, fp, indent=4)
#process cksum
proccksum = {}
if not os.path.isfile(filepath+proccksum_file):
print("File extracted process cksum {} does not exist.".format(filepath+proccksum_file))
else:
print("Extract info process cksum")
with open(filepath+proccksum_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if len(tmp) == 3 and '/proc/' in tmp[-1]:
#extract pid
pidx=tmp[-1].split('/')
if len(pidx) == 5:
pid=pidx[2]
tmpf=tmp[0]+' '+tmp[1]
if tmpf in proccksum:
if not pid in proccksum[tmpf]:
proccksum[tmpf]=proccksum[tmpf]+[pid]
else:
proccksum[tmpf]=[pid]
if debug:
with open('result-proccksum.json', 'w') as fp:
json.dump(proccksum, fp, indent=4)
#process ps
procps = {}
if not os.path.isfile(filepath+procps_file):
print("File extracted process ps {} does not exist.".format(filepath+procps_file))
else:
print("Extract info process ps")
with open(filepath+procps_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if tmp[1].isdigit():
pid=tmp[1]
for fpat in tmp:
if fpat.startswith('/') and len(fpat) > 1:
if fpat in procps:
if not pid in procps[fpat]:
procps[fpat]=procps[fpat]+[pid]
else:
procps[fpat]=[pid]
#aix case
elif tmp[4].isdigit():
pid=tmp[4]
for fpat in tmp:
if fpat.startswith('/') and len(fpat) > 1:
if fpat in procps:
if not pid in procps[fpat]:
procps[fpat]=procps[fpat]+[pid]
else:
procps[fpat]=[pid]
if debug:
with open('result-procps.json', 'w') as fp:
json.dump(procps, fp, indent=4)
#systemd file
systemd = []
if not os.path.isfile(filepath+systemd_file):
print("File extracted systemd files {} does not exist.".format(filepath+systemd_file))
else:
print("Extract info systemd files")
with open(filepath+systemd_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split()
if len(tmp) == 2 and tmp[1].startswith('/'):
if not tmp[1] in systemd:
systemd.append(tmp[1])
if debug:
with open('result-systemd.json', 'w') as fp:
json.dump(systemd, fp, indent=4)
#initd file
initd = []
if not os.path.isfile(filepath+initd_file):
print("File extracted initd files {} does not exist.".format(filepath+initd_file))
else:
print("Extract info inetd files")
with open(filepath+initd_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
tmp=line.rstrip("\n\r").split('=')
if len(tmp) == 2:
if tmp[1].startswith('"/'):
tmpx=tmp[1].split('"')
if not "$" in tmpx[1] and not tmpx[1] in initd:
initd.append(tmpx[1])
elif tmp[1].startswith('/') and not "$" in tmp[1]:
if not tmp[1] in initd:
initd.append(tmp[1])
if debug:
with open('result-initd.json', 'w') as fp:
json.dump(initd, fp, indent=4)
#FIND
stat_type={}
stat_ext={}
stat_filename={}
stat_path={}
stat_filenamewithoutext={}
files = {}
if not os.path.isfile(filepath+find_file):
print("File extracted find file aix {} does not exist.".format(filepath+find_file))
else:
with open(filepath+find_file, encoding="utf-8", errors='ignore') as fp:
cnt = 0
for line in fp:
#print("line {} contents {}".format(cnt, line))
if line.startswith('/'):
#type result
#print("TYPE: line {} contents {}".format(cnt, line))
rtmp=line.split(': ')
if rtmp and len(rtmp) > 1 and not 'ERROR: cannot read' in ': '.join(rtmp[1:]).rstrip("\n\r").strip():
if rtmp[0] in files:
files[rtmp[0]]['type'] = ': '.join(rtmp[1:]).rstrip("\n\r").strip()
else:
files[rtmp[0]] = {'type': ': '.join(rtmp[1:]).rstrip("\n\r").strip()}
if 'statically linked' in ': '.join(rtmp[1:]).rstrip("\n\r").strip():
files[rtmp[0]]['static_executable'] = True
if ': '.join(rtmp[1:]).rstrip("\n\r").strip() in stat_type:
stat_type[': '.join(rtmp[1:]).rstrip("\n\r").strip()]=stat_type[': '.join(rtmp[1:]).rstrip("\n\r").strip()]+1
else:
stat_type[': '.join(rtmp[1:]).rstrip("\n\r").strip()]=1
if not os.path.isfile(filepath+find):
print("File extracted find {} does not exist.".format(filepath+find))
else:
with open(filepath+find, encoding="utf-8", errors='ignore') as fp:
cnt = 0
current_file=''
for line in fp:
#print("line {} contents {}".format(cnt, line))
if line.startswith('\''):
#entropy result
#print("ENT: line {} contents {}".format(cnt, line))
rtmp=line.split('\'')
if rtmp and len(rtmp) == 4:
if rtmp[1] in files:
files[rtmp[1]]['entropy'] = rtmp[3]
else:
files[rtmp[1]] = {'entropy': rtmp[3]}
elif line.startswith('/'):
#type result
#print("TYPE: line {} contents {}".format(cnt, line))
rtmp=line.split(': ')
if rtmp and len(rtmp) > 1 and not ('ERROR: cannot read' in ': '.join(rtmp[1:]).rstrip("\n\r").strip() or 'cannot open' in ': '.join(rtmp[1:]).rstrip("\n\r").strip()):
if rtmp[0] in files:
files[rtmp[0]]['type'] = ': '.join(rtmp[1:]).rstrip("\n\r").strip()
else:
files[rtmp[0]] = {'type': ': '.join(rtmp[1:]).rstrip("\n\r").strip()}
if 'statically linked' in ': '.join(rtmp[1:]).rstrip("\n\r").strip():
files[rtmp[0]]['static_executable'] = True
if ': '.join(rtmp[1:]).rstrip("\n\r").strip() in stat_type:
stat_type[': '.join(rtmp[1:]).rstrip("\n\r").strip()]=stat_type[': '.join(rtmp[1:]).rstrip("\n\r").strip()]+1
else:
stat_type[': '.join(rtmp[1:]).rstrip("\n\r").strip()]=1
elif line.startswith(' ') or line[0].isdigit() or line.startswith('STAT:'):
#find result
#print("FIND: line {} contents {}".format(cnt, line))
rtmp=None
vstat=False
if line.startswith('STAT:'):
rtmp=line[5:].split('|')
vstat=True
else:
rtmp=line.split()
#md5sum
if rtmp and len(rtmp) == 2 and rtmp[1].startswith('/'):
#print("MD5SUM: line {} contents {}".format(cnt, line))
if rtmp[1] in files:
files[rtmp[1]]['md5sum'] = rtmp[0]
else:
files[rtmp[1]] = {'md5sum': rtmp[0]}
#cksum
elif rtmp and len(rtmp) == 3 and rtpm[0].isdigit() and rtpm[1].isdigit() and rtmp[-1].startswith('/'):
#print("CKSUM: line {} contents {}".format(cnt, line))
if rtmp[-1] in files:
files[rtmp[-1]]['cksum'] = rtmp[0]+' '+rtmp[1]
if rtmp[0]+' '+rtmp[1] in proccksum:
files[rtmp[-1]]['pid']=proccksum[rtmp[0]+' '+rtmp[1]]
for pid in proccksum[rtmp[0]+' '+rtmp[1]]:
if pid in aixservices:
files[rtmp[-1]]['service']=True
else:
files[rtmp[-1]] = {'cksum': rtmp[0]+' '+rtmp[1]}
if rtmp[0]+' '+rtmp[1] in proccksum:
files[rtmp[-1]]['pid']=proccksum[rtmp[0]+' '+rtmp[1]]
for pid in proccksum[rtmp[0]+' '+rtmp[1]]:
if pid in aixservices:
files[rtmp[-1]]['service']=True
#check if --time-style=long-iso (linux) or Date: (ls - aix) or stat
elif rtmp and len(rtmp) > 8:
filetmp=''
filelink=''
if vstat:
filetmp=rtmp[13]
if ' -> ' in rtmp[14]:
filelink=rtmp[14].split(' -> ')[1][1:-1]
else:
if '-' in rtmp[7]:
filetmp=' '.join(rtmp[9:])
else:
if ',' in rtmp[6] and rtmp[8][0].isalpha():
filetmp=' '.join(rtmp[11:])
else:
filetmp=' '.join(rtmp[10:])
ftmp=filetmp.split(' -> ')
if len(ftmp) == 2:
filetmp=ftmp[0]
current_file=filetmp
filelink=ftmp[1]
if filetmp in files:
files[filetmp]['inode'] = rtmp[0]
files[filetmp]['blocksize'] = rtmp[1]
files[filetmp]['permissions'] = rtmp[2]
files[filetmp]['numberoflink'] = rtmp[3]
files[filetmp]['owner'] = rtmp[4]
files[filetmp]['group'] = rtmp[5]
if vstat:
files[filetmp]['message']=line[5:].rstrip("\n\r")
files[filetmp]['size'] = rtmp[6]
files[filetmp]['device_major'] = rtmp[7]
files[filetmp]['device_minor'] = rtmp[8]
files[filetmp]['createdate'] = rtmp[9]
files[filetmp]['lastaccess'] = rtmp[10]
files[filetmp]['lastmodified'] = rtmp[11]
files[filetmp]['lastchange'] = rtmp[12]
else:
files[filetmp]['message']=line.rstrip("\n\r")
if not ',' in rtmp[6]:
files[filetmp]['size'] = rtmp[6]
if '-' in rtmp[7]:
files[filetmp]['lastmodified'] = " ".join(rtmp[7:9])
else:
files[filetmp]['lastmodified'] = " ".join(rtmp[7:10])
else:
files[filetmp]['device_major'] = rtmp[6]
files[filetmp]['device_minor'] = rtmp[7]
if rtmp[8][0].isalpha():
files[filetmp]['lastmodified'] = " ".join(rtmp[8:11])
else:
files[filetmp]['lastmodified'] = " ".join(rtmp[8:10])
else:
if vstat:
files[filetmp] = {'message': line[5:].rstrip("\n\r"), 'inode': rtmp[0], 'blocksize': rtmp[1], 'permissions': rtmp[2], 'numberoflink': rtmp[3], 'owner': rtmp[4], 'group': rtmp[5], 'size': rtmp[6], 'device_major': rtmp[7], 'device_minor': rtmp[8], 'createdate': rtmp[9], 'lastaccess': rtmp[10], 'lastmodified': rtmp[11], 'lastchange': rtmp[12]}
else:
files[filetmp] = {'message': line.rstrip("\n\r"), 'inode': rtmp[0], 'blocksize': rtmp[1], 'permissions': rtmp[2], 'numberoflink': rtmp[3], 'owner': rtmp[4], 'group': rtmp[5]}
if not ',' in rtmp[6]:
files[filetmp]['size'] = rtmp[6]
if '-' in rtmp[7]:
files[filetmp]['lastmodified'] = " ".join(rtmp[7:9])
else:
files[filetmp]['lastmodified'] = " ".join(rtmp[7:10])
else:
files[filetmp]['device_major'] = rtmp[6]
files[filetmp]['device_minor'] = rtmp[7]
if rtmp[8][0].isalpha():
files[filetmp]['lastmodified'] = " ".join(rtmp[8:11])
else:
files[filetmp]['lastmodified'] = " ".join(rtmp[8:10])
#files[filetmp]['ext'] = extx #?? verif
if files[filetmp]['lastmodified'][0].isalpha() and not vstat:
if ':' in files[filetmp]['lastmodified']:
tmpdx=files[filetmp]['lastmodified'].split()
# j=01, f=02, mar=03 sinon ma=04, ju&l=07 sinon 06, a=08, s=09, o=10, n=11, d=12
if tmpdx[0].lower().startswith('j'):
tmpd="01"
elif tmpdx[0].lower().startswith('f'):
tmpd="02"
elif tmpdx[0].lower().startswith('mar'):
tmpd="03"
elif tmpdx[0].lower().startswith('a'):
if 'r' in tmpdx[0].lower():
tmpd="04"
else:
tmpd="08"
elif tmpdx[0].lower().startswith('ma'):
tmpd="05"
elif tmpdx[0].lower().startswith('ju'):
if 'l' in tmpdx[0].lower():
tmpd="07"
else:
tmpd="06"
elif tmpdx[0].lower().startswith('s'):
tmpd="09"
elif tmpdx[0].lower().startswith('o'):
tmpd="10"
elif tmpdx[0].lower().startswith('n'):
tmpd="11"
elif tmpdx[0].lower().startswith('d'):
tmpd="12"
files[filetmp]['lastmodified']=str(now.year)+"-"+tmpd+"-"+tmpdx[1]+" "+tmpdx[2]
else:
tmpdx=files[filetmp]['lastmodified'].split()
# j=01, f=02, mar=03 sinon ma=04, ju&l=07 sinon 06, a=08, s=09, o=10, n=11, d=12
if tmpdx[0].lower().startswith('j'):
tmpd="01"
elif tmpdx[0].lower().startswith('f'):
tmpd="02"
elif tmpdx[0].lower().startswith('mar'):
tmpd="03"
elif tmpdx[0].lower().startswith('a'):
if 'r' in tmpdx[0].lower():
tmpd="04"
else:
tmpd="08"
elif tmpdx[0].lower().startswith('ma'):
tmpd="05"
elif tmpdx[0].lower().startswith('ju'):
if 'l' in tmpdx[0].lower():
tmpd="07"
else:
tmpd="06"
elif tmpdx[0].lower().startswith('s'):
tmpd="09"
elif tmpdx[0].lower().startswith('o'):
tmpd="10"
elif tmpdx[0].lower().startswith('n'):
tmpd="11"
elif tmpdx[0].lower().startswith('d'):
tmpd="12"
print(str(files[filetmp]['message']))
files[filetmp]['lastmodified']=tmpdx[2]+"-"+tmpd+"-"+tmpdx[1]+" 00:00"
if files[filetmp]['permissions'].startswith('d'):
files[filetmp]['type_file'] = 'directory'
elif files[filetmp]['permissions'].startswith('-'):
files[filetmp]['type_file'] = 'file'
#get filename withotu path & ext
fnx=os.path.basename(filetmp)
pathx=os.path.dirname(filetmp)
fnxwe, extx = os.path.splitext(fnx)
if extx in stat_ext:
stat_ext[extx]=stat_ext[extx]+1
else:
stat_ext[extx]=1
if fnx in stat_filename:
stat_filename[fnx]=stat_filename[fnx]+1
else:
stat_filename[fnx]=1
if pathx in stat_path:
stat_path[pathx]=stat_path[pathx]+1
else:
stat_path[pathx]=1
if fnxwe in stat_filenamewithoutext:
stat_filenamewithoutext[fnxwe]=stat_filenamewithoutext[fnxwe]+1
else:
stat_filenamewithoutext[fnxwe]=1
files[filetmp]['ext'] = extx
files[filetmp]['file_name'] = fnx
files[filetmp]['file_path'] = pathx
files[filetmp]['file_name_withoutext'] = fnxwe
elif files[filetmp]['permissions'].startswith('c'):
files[filetmp]['type_file'] = 'device'
elif files[filetmp]['permissions'].startswith('b'):
files[filetmp]['type_file'] = 'device'
elif files[filetmp]['permissions'].startswith('s'):
files[filetmp]['type_file'] = 'socket'
elif files[filetmp]['permissions'].startswith('p'):
files[filetmp]['type_file'] = 'pipe'
elif files[filetmp]['permissions'].startswith('l'):
files[filetmp]['type_file'] = 'link'
if filelink:
files[filetmp]['link'] = True
files[filetmp]['filelink'] = filelink
#enrichissemnt
#important configuration file
if filetmp.startswith('/etc/'):
files[filetmp]['etc_file']=True
if filetmp.endswith('.hushlogin') or filetmp.endswith('.profile') or filetmp.endswith('.rhost') or filetmp.endswith('.cshrc') or filetmp.endswith('.login') or filetmp.endswith('.logout') or filetmp.endswith('.bashrc') or filetmp.endswith('.hosts') or filetmp.endswith('.bash_profile') or '/.ssh/' in filetmp or 'ftpusers' in filetmp:
files[filetmp]['configuration']=True
if filetmp.startswith('/etc/hosts.') or filetmp.startswith('/etc/sudoers') or filetmp.startswith('/etc/at.') or filetmp.startswith('/etc/grub.conf') or filetmp.startswith('/etc/default/') or filetmp.startswith('/etc/securetty') or filetmp.startswith('/etc/security/') or filetmp.startswith('/etc/motd') or filetmp.startswith('/etc/issue') or filetmp.startswith('/etc/exports') or filetmp.startswith('/etc/bashrc') or filetmp.startswith('/etc/shells') or filetmp.startswith('/etc/profile') or filetmp.startswith('/etc/pam.d/') or filetmp.startswith('/etc/passwd') or filetmp.startswith('/etc/group') or filetmp.startswith('/etc/group-') or filetmp.startswith('/etc/shadow') or filetmp.startswith('/etc/login.defs'):
files[filetmp]['configuration']=True
#docker file
if '/docker/containers/' in filetmp and (filetmp.endswith('.log') or filetmp.endswith('.json')):
files[filetmp]['docker_conf']=True
#history
if '.' in filetmp and 'history' in filetmp:
files[filetmp]['history']=True
#browser
if '.mozilla' in filetmp:
files[filetmp]['browser']=True
#browser
if '.mozilla' in filetmp or '/.cache/chromium/' in filetmp or '/.cache/google-chrome/' in filetmp:
files[filetmp]['browser']=True
#hidden
if '/.' in filetmp:
files[filetmp]['hidden']=True
#service
if filetmp.startswith('/etc/rc') or filetmp.startswith('/etc/systemd/') or filetmp.startswith('/etc/init.d/') or filetmp.startswith('/etc/xinet') or filetmp.startswith('/etc/rc.d/init.d/'):
files[filetmp]['service']=True
#executable file
if 'x' in files[filetmp]['permissions'] and not (filetmp.startswith('/usr/') or filetmp.startswith('/sbin/') or filetmp.startswith('/bin/') or filetmp.startswith('/opt/') or filetmp.startswith('/proc/')):
files[filetmp]['executable_nocommun_path'] = True
if 'x' in files[filetmp]['permissions']:
files[filetmp]['executable'] = True
#suid or sgid file
if 's' in files[filetmp]['permissions']:
files[filetmp]['suid_sgid'] = True
#device file
if 'b' in files[filetmp]['permissions']:
files[filetmp]['device'] = True
#writable file by all users
if len(files[filetmp]['permissions']) > 9 and 'w' == files[filetmp]['permissions'][-2] and not 'l' in files[filetmp]['permissions']:
files[filetmp]['writable'] = True
#unknown user or group
if files[filetmp]['owner'].isdigit() or files[filetmp]['group'].isdigit():
files[filetmp]['unknown_owner'] = True
#space at the end
if filetmp.endswith('\\') or filetmp.endswith(' '):
files[filetmp]['space_end'] = True
#verify if in package
if filetmp in package_files_rpm:
files[filetmp]['rpm']=package_files_rpm[filetmp]
#use in service (autorun) & CVE