2019-11-27 00:22:04 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import socket
|
|
|
|
import struct
|
|
|
|
import hashlib
|
|
|
|
import os
|
2019-12-02 20:40:47 +01:00
|
|
|
import time
|
|
|
|
|
|
|
|
from getpass import getpass
|
2019-11-27 00:22:04 +01:00
|
|
|
|
|
|
|
def readMessage(sock):
|
|
|
|
data = sock.recv(4)
|
|
|
|
length, = struct.unpack(">I", data)
|
|
|
|
data = sock.recv(1)
|
|
|
|
success, = struct.unpack("B", data)
|
|
|
|
data = sock.recv(length-1)
|
|
|
|
message = data.decode('utf-8')
|
|
|
|
|
|
|
|
return success, message
|
|
|
|
|
|
|
|
# read the salt from the config file
|
|
|
|
with open("../data/etc/auth", "r") as authFile:
|
|
|
|
lineno = 0
|
|
|
|
for line in authFile:
|
|
|
|
if lineno == 1:
|
|
|
|
SALT = line.strip()
|
|
|
|
break
|
|
|
|
lineno += 1
|
|
|
|
|
|
|
|
_, host, port, filename = sys.argv
|
|
|
|
|
|
|
|
# read and store the password from the user
|
2019-12-02 20:40:47 +01:00
|
|
|
pwd = getpass()
|
2019-11-27 00:22:04 +01:00
|
|
|
|
|
|
|
s = socket.create_connection( (host, int(port)) )
|
|
|
|
|
|
|
|
data = s.recv(4)
|
|
|
|
length, = struct.unpack(">I", data)
|
|
|
|
|
|
|
|
if length != 4:
|
|
|
|
print(f"Unexpected challenge length: {length}")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
data = s.recv(4)
|
|
|
|
challenge, = struct.unpack(">I", data)
|
|
|
|
|
|
|
|
print(f"Challenge: {challenge}")
|
|
|
|
|
|
|
|
# build response string
|
|
|
|
responsestr = pwd + ":" + str(challenge) + ":" + SALT
|
|
|
|
|
|
|
|
m = hashlib.sha256()
|
|
|
|
m.update(responsestr.encode('utf-8'))
|
|
|
|
response = m.hexdigest()
|
|
|
|
|
|
|
|
print(f"Response: {response}")
|
|
|
|
|
|
|
|
data = struct.pack(">I64s", len(response), response.encode('ascii') )
|
|
|
|
s.send(data)
|
|
|
|
|
|
|
|
success, message = readMessage(s)
|
|
|
|
print(f"Server message: {message}")
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
print("Failed.")
|
|
|
|
exit(1)
|
|
|
|
|
2019-12-02 20:56:16 +01:00
|
|
|
print() # for proper progress display
|
|
|
|
|
2019-11-27 00:22:04 +01:00
|
|
|
with open(filename, "rb") as binfile:
|
|
|
|
filesize = os.stat(filename).st_size
|
|
|
|
|
|
|
|
data = struct.pack(">I", filesize)
|
|
|
|
s.send(data)
|
|
|
|
|
|
|
|
sent_bytes = 0
|
|
|
|
while True:
|
|
|
|
data = binfile.read(1024)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
s.send(data)
|
|
|
|
sent_bytes += len(data)
|
|
|
|
|
2019-12-02 20:56:16 +01:00
|
|
|
print(f"\rSent {sent_bytes} of {filesize} bytes.")
|
2019-11-27 00:22:04 +01:00
|
|
|
|
|
|
|
success, message = readMessage(s)
|
|
|
|
print(f"Server message: {message}")
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
print("Failed.")
|
2019-12-02 20:56:16 +01:00
|
|
|
exit(1)
|