Repository navigation
Expand file tree
/
Copy pathmalCAP.py
More file actions
58 lines (45 loc) · 1.88 KB
/
Copy pathmalCAP.py
File metadata and controls
58 lines (45 loc) · 1.88 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
# TODO Add whitelist
import sys
import time
import shutil
import os
import datetime
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
def copyFile(srcfile): # copies and renames files
dstfile = os.path.abspath(logPath) + '\\' + datetime.datetime.now().isoformat().replace(':', '-') + '-' + os.path.basename(srcfile)
cwPath = os.path.dirname(os.path.abspath(__file__))
if cwPath.lower() not in srcfile.lower():
try:
shutil.copyfile(srcfile, dstfile)
print(srcfile + ' --> ' + dstfile)
with open('malCAP.log', 'a') as f: # log files captured to capture-py.log
f.write(datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S') + ',' + srcfile + ',' + dstfile + '\n')
except (OSError, IOError):
pass
class FileEventHandler(FileSystemEventHandler): # Fires on file event
def on_modified(self, event): # runs on file modified
srcfile = str(event).split("'")[1].replace('\\\\', '\\') # extract file name and path
copyFile(srcfile)
def on_deleted(self, event): # runs of file deleted
srcfile = str(event).split("'")[1].replace('\\\\', '\\') # extract file name and path
copyFile(srcfile)
if len(sys.argv) != 3: # check if all arguments are provded or exit
print('malCAP.py <capture-directory> <save-directory>')
sys.exit()
watchPath = sys.argv[1] # files to watch
logPath = sys.argv[2] # where to log/archive files
if not os.path.exists(logPath): # if logPath doesn't exsist, create it.
os.makedirs(logPath)
event_handler = FileEventHandler()
observer = Observer()
observer.schedule(event_handler, watchPath, recursive=True)
observer.start()
print('Capture started. Press Ctrl-C to stop')
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
print('Capture Stopped')
observer.join()