chg: [server] verify input data, save data to disk by uuid, cleaning

todo: redis server
pull/23/head
Terrtia 2018-12-13 10:55:27 +01:00
parent 9ee0cf0306
commit 7a2d04cfe1
No known key found for this signature in database
GPG Key ID: 1E1B1F50D84613D0
1 changed files with 58 additions and 34 deletions

View File

@ -1,9 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import sys import sys
import hmac import hmac
import stat
import binascii
from twisted.internet import ssl, task, protocol, endpoints, defer from twisted.internet import ssl, task, protocol, endpoints, defer
from twisted.python import log from twisted.python import log
@ -15,13 +15,17 @@ from twisted.internet.protocol import Protocol
from ctypes import * from ctypes import *
from uuid import UUID from uuid import UUID
path = '/mypath'
hmac_reset = '0000000000000000000000000000000000000000000000000000000000000000'
hmac_key = b'private key to change\n'
class Echo(Protocol): class Echo(Protocol):
#def __init__(self, factory): #def __init__(self, factory):
# self.factory = factory # self.factory = factory
def dataReceived(self, data): def dataReceived(self, data):
print('-----')
process_header(data) process_header(data)
#print(data[72:]) #print(data[72:])
@ -41,53 +45,73 @@ class D4Header(Structure):
def process_header(data): def process_header(data):
if len(data) > 73:
d4_header = data[:72].hex() d4_header = data[:72].hex()
print(d4_header)
#version = int(d4_header[0:2], 16)
#type = int(d4_header[2:4], 16)
uuid_header = d4_header[4:36] uuid_header = d4_header[4:36]
#timestamp = d4_header[36:52] fixme
hmac_header = d4_header[64:128] hmac_header = d4_header[64:128]
#size = d4_header[128:132] endian issue
d = unpack(D4Header, data) d4_header = d4_header.replace(hmac_header, hmac_reset)
if is_valid_uuid_v4(uuid_header):
print('version: {}'.format(d.version))
print('type: {}'.format(d.type))
print('uuid: {}'.format(uuid_header))
print('timestamp: {}'.format(d.timestamp))
print('hmac: {}'.format(hmac_header))
print('size: {}'.format(d.size))
print(len(data) - sizeof(d)) # sizeof(d)=72
print('___________________')
reset = '0000000000000000000000000000000000000000000000000000000000000000'
print(d4_header)
d4_header = d4_header.replace(hmac_header, reset)
print()
temp = bytes.fromhex(d4_header) temp = bytes.fromhex(d4_header)
data = data.replace(data[:72], temp) data = data.replace(data[:72], temp)
print(data) print(data)
data_header = unpack(D4Header, data)
HMAC = hmac.new(b'private key to change\n', msg=data, digestmod='sha256') # check if is valid uuid v4
print(HMAC.digest()) if not is_valid_uuid_v4(uuid_header):
print(HMAC.hexdigest()) print('not uuid v4')
print(hmac_header) # verify timestamp
#elif :
# print('not valid timestamp')
elif data_header['size'] != (len(data) - data_header['struct_size']):
print('invalid size')
print('size: {}'.format(data_header['size']))
print(len(data) - data_header['struct_size']) # sizeof(d)=72
else:
### Debug ###
print('version: {}'.format( data_header['version'] ))
print('type: {}'.format( data_header['type'] ))
print('uuid: {}'.format(uuid_header))
print('timestamp: {}'.format( data_header['timestamp'] ))
print('hmac: {}'.format( hmac_header ))
print('size: {}'.format( data_header['size'] ))
#print(d4_header)
### ###
# verify hmac sha256
HMAC = hmac.new(hmac_key, msg=data, digestmod='sha256')
if hmac_header == HMAC.hexdigest():
print('hmac match')
path_file = os.path.join(path,uuid_header)
if not os.path.isdir(path_file):
os.mkdir(path_file)
with open(os.path.join(path_file, 'out'), 'ab')as f:
f.write(data[72:])
print('END')
print()
# discard data
else:
print("hmac don't match")
else:
print('incomplete data')
def unpack(ctype, buffer): def unpack(ctype, buffer):
c_str = create_string_buffer(buffer) c_str = create_string_buffer(buffer)
return cast(pointer(c_str), POINTER(ctype)).contents d = cast(pointer(c_str), POINTER(ctype)).contents
data_header = {}
data_header['version'] = d.version
data_header['type'] = d.type
data_header['timestamp'] = d.timestamp
data_header['size'] = d.size
data_header['struct_size'] = sizeof(d)
return data_header
def is_valid_uuid_v4(header_uuid): def is_valid_uuid_v4(header_uuid):
#try: #try:
#print(header_uuid)
uuid_test = UUID(hex=header_uuid, version=4) uuid_test = UUID(hex=header_uuid, version=4)
#print(uuid_test.hex)
return uuid_test.hex == header_uuid return uuid_test.hex == header_uuid
#except: #except:
# return False # return False