This repository has been archived by the owner on Aug 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.py
executable file
·74 lines (59 loc) · 1.98 KB
/
sender.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
#!/usr/bin/env python
import socket
from base64 import b64encode
import keyring
import json
import requests
from time import time, sleep
from random import randint
import pysectools
from pygtail import Pygtail as tail
VAULT_URL = 'http://127.0.0.1:8200'
HOST, PORT = 'localhost', 514
LOG_FILE = 'MOCK_DATA.txt'
interval = randint(3, 5)
# Protect our memory from leaking secrets
pysectools.disallow_swap()
pysectools.disallow_core_dumps()
# SOCK_DGRAM is the socket type to use for UDP sockets
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
token = keyring.get_password('dev-vault', 'syslogd')
headers = {'X-Vault-Token': token}
def rotate_key(name):
url = VAULT_URL + '/v1/transit/keys/{}/rotate'.format(name)
try:
r = requests.post(url, headers=headers)
r.raise_for_status()
return True
except:
return False
def sign_message(name, message):
url = VAULT_URL + '/v1/transit/sign/{}'.format(name)
m = message.encode()
payload = {'input': b64encode(m).decode('ascii')}
try:
r = requests.post(url, headers=headers, json=payload)
return r.json()['data']['signature']
except:
return None
for l in tail(LOG_FILE):
l = l.strip()
sleep(randint(0, 3))
# Generate new key sort-of-randomly
if not (round(time() % interval)):
interval = randint(3, 5)
if not rotate_key('syslogd'):
print('Unable to rotate key!!! THIS IS INSECURE BUT I WILL KEEP GOING!!!')
# Sign message and send
sig = sign_message('syslogd', l)
if sig is None:
print('Unable to sign message!!!')
break
# Send message to server.
# As you can see, there is no connect() call; UDP has no connections.
# Instead, data is directly sent to the recipient via sendto().
sock.sendto(bytes('{} SIGNATURE: {}\n'.format(
l, sig), 'utf-8'), (HOST, PORT))
received = str(sock.recv(1024), 'utf-8')
print('Sent: {}'.format(l))
print('Received: {}'.format(received))