Compare commits
14 Commits
4faf9a970a
...
master
Author | SHA1 | Date | |
---|---|---|---|
f65db31c12 | |||
e7c4945ae9 | |||
94fcda4c2c | |||
c40e59ceb9 | |||
57af60ee07 | |||
48fd34117d
|
|||
d66cd15ec9
|
|||
863a42e982 | |||
92c0f4a90a | |||
098e279eb8
|
|||
25fbb03259
|
|||
1e54e17cea
|
|||
3a20592cff
|
|||
01e26bf6c5
|
@ -11,6 +11,8 @@ from netwrapper import NetWrapper
|
|||||||
ABSOLUTE_PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
|
ABSOLUTE_PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
|
||||||
DOWNLOAD_LOCATION = ABSOLUTE_PATH + os.path.sep + 'download' + os.path.sep
|
DOWNLOAD_LOCATION = ABSOLUTE_PATH + os.path.sep + 'download' + os.path.sep
|
||||||
CONFIG_LOCATION = ABSOLUTE_PATH + os.path.sep + 'config' + os.path.sep + 'config.txt'
|
CONFIG_LOCATION = ABSOLUTE_PATH + os.path.sep + 'config' + os.path.sep + 'config.txt'
|
||||||
|
PASSPHRASE = ''
|
||||||
|
SERVER_ADDRESS = ''
|
||||||
LOGGED_IN = False
|
LOGGED_IN = False
|
||||||
|
|
||||||
|
|
||||||
@ -54,20 +56,21 @@ def printCommand():
|
|||||||
' Get current directory -> GWD \n' +
|
' Get current directory -> GWD \n' +
|
||||||
' Change current directory -> CWD <path> \n' +
|
' Change current directory -> CWD <path> \n' +
|
||||||
' List content of current directory -> LST \n' +
|
' List content of current directory -> LST \n' +
|
||||||
|
' Remove file from current directory -> RMF <filename> \n' +
|
||||||
' Upload file to current directory -> UPL <filename> \n' +
|
' Upload file to current directory -> UPL <filename> \n' +
|
||||||
' Download file from current directory -> DNL <filename> \n' +
|
' Download file from current directory -> DNL <filename> \n' +
|
||||||
' Login -> LIN <username> \n' +
|
' Login -> LIN <username> <password> \n' +
|
||||||
' Logout -> LOUT \n')
|
' Logout -> LOUT \n')
|
||||||
|
|
||||||
|
|
||||||
def printCommandsWihtoutLogin():
|
def printCommandsWihtoutLogin():
|
||||||
print('\nYou must log in before issuing other commads!\n',
|
print('\nYou must log in before issuing other commads!\n',
|
||||||
' Login -> LIN <username> \n',
|
' Login -> LIN <username> <password> \n',
|
||||||
' Exit -> EXIT\n')
|
' Exit -> EXIT\n')
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], 'hp:')
|
opts, args = getopt.getopt(sys.argv[1:], 'hp:s:')
|
||||||
except getopt.GetoptError:
|
except getopt.GetoptError:
|
||||||
print('Error: Unknown option detected.')
|
print('Error: Unknown option detected.')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@ -76,10 +79,13 @@ for opt, arg in opts:
|
|||||||
if opt in '-p':
|
if opt in '-p':
|
||||||
PASSPHRASE = arg
|
PASSPHRASE = arg
|
||||||
|
|
||||||
|
if opt in '-s':
|
||||||
|
SERVER_ADDRESS = arg
|
||||||
|
|
||||||
if PASSPHRASE == '':
|
if PASSPHRASE == '':
|
||||||
print('Key required to start client!')
|
print('Key required to start client!')
|
||||||
print('Usage:')
|
print('Usage:')
|
||||||
print(' client.py -p <passphrase>')
|
print(' client.py -p <passphrase> -s <server_address>')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not os.path.isfile(CONFIG_LOCATION) or os.stat(CONFIG_LOCATION).st_size == 0:
|
if not os.path.isfile(CONFIG_LOCATION) or os.stat(CONFIG_LOCATION).st_size == 0:
|
||||||
@ -89,6 +95,9 @@ if not os.path.isfile(CONFIG_LOCATION) or os.stat(CONFIG_LOCATION).st_size == 0:
|
|||||||
SERVER_PUBLIC_KEY = loadPublicKey()
|
SERVER_PUBLIC_KEY = loadPublicKey()
|
||||||
CLIENT_PRIVATE_KEY = loadPrivateKey(PASSPHRASE)
|
CLIENT_PRIVATE_KEY = loadPrivateKey(PASSPHRASE)
|
||||||
CLIENT_ADDRESS = loadAddress()
|
CLIENT_ADDRESS = loadAddress()
|
||||||
|
LOGGED_IN = False
|
||||||
|
|
||||||
|
network = NetWrapper(CLIENT_PRIVATE_KEY, CLIENT_ADDRESS, SERVER_PUBLIC_KEY, serverAddr=SERVER_ADDRESS)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
command = input("Type a command:")
|
command = input("Type a command:")
|
||||||
@ -99,15 +108,24 @@ while True:
|
|||||||
print("Invalid command format!")
|
print("Invalid command format!")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if separatedCommand[0] == 'LIN' and len(separatedCommand) == 2:
|
if not LOGGED_IN:
|
||||||
network = NetWrapper(CLIENT_PRIVATE_KEY, CLIENT_ADDRESS, separatedCommand[1], SERVER_PUBLIC_KEY)
|
if separatedCommand[0] == 'LIN' and len(separatedCommand) == 3:
|
||||||
|
network.username = separatedCommand[1]
|
||||||
try:
|
try:
|
||||||
network.connectToServer(separatedCommand[2])
|
network.connectToServer(separatedCommand[2])
|
||||||
|
response = network.recieveMessage().decode('UTF-8')
|
||||||
|
print(response)
|
||||||
|
if response == 'OK':
|
||||||
|
LOGGED_IN = True
|
||||||
|
else:
|
||||||
|
LOGGED_IN = False
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error: "+str(e))
|
print("Error: "+str(e))
|
||||||
|
LOGGED_IN = False
|
||||||
continue
|
continue
|
||||||
LOGGED_IN = True
|
else:
|
||||||
continue
|
print('You are already logged in!')
|
||||||
|
|
||||||
|
|
||||||
if separatedCommand[0] == 'EXIT':
|
if separatedCommand[0] == 'EXIT':
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@ -119,6 +137,7 @@ while True:
|
|||||||
if separatedCommand[0] == 'LOUT' and len(separatedCommand) == 1:
|
if separatedCommand[0] == 'LOUT' and len(separatedCommand) == 1:
|
||||||
network.sendMessage(command.encode('UTF-8'))
|
network.sendMessage(command.encode('UTF-8'))
|
||||||
print(network.recieveMessage().decode('UTF-8'))
|
print(network.recieveMessage().decode('UTF-8'))
|
||||||
|
LOGGED_IN = False
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if separatedCommand[0] == 'MKD' and len(separatedCommand) == 2:
|
if separatedCommand[0] == 'MKD' and len(separatedCommand) == 2:
|
||||||
@ -146,13 +165,18 @@ while True:
|
|||||||
print(network.recieveMessage().decode('UTF-8'))
|
print(network.recieveMessage().decode('UTF-8'))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if separatedCommand[0] == 'RMF' and len(separatedCommand) == 2:
|
||||||
|
network.sendMessage(command.encode('UTF-8'))
|
||||||
|
print(network.recieveMessage().decode('UTF-8'))
|
||||||
|
continue
|
||||||
|
|
||||||
if separatedCommand[0] == 'UPL' and len(separatedCommand) == 2:
|
if separatedCommand[0] == 'UPL' and len(separatedCommand) == 2:
|
||||||
if os.path.isfile(separatedCommand[1]):
|
if os.path.isfile(separatedCommand[1]):
|
||||||
cmd = 'UPL ' + separatedCommand[1].split(os.path.sep)[-1]
|
cmd = 'UPL ' + separatedCommand[1].split(os.path.sep)[-1]
|
||||||
network.sendMessage(cmd.encode('UTF-8'))
|
network.sendMessage(cmd.encode('UTF-8'))
|
||||||
|
|
||||||
with open(separatedCommand[1], "rb") as file:
|
with open(separatedCommand[1], "rb") as file:
|
||||||
network.sendMessage(file.readlines())
|
network.sendMessage(file.read())
|
||||||
|
|
||||||
network.sendMessage('EOF'.encode('UTF-8'))
|
network.sendMessage('EOF'.encode('UTF-8'))
|
||||||
response = network.recieveMessage().decode('UTF-8')
|
response = network.recieveMessage().decode('UTF-8')
|
||||||
@ -166,12 +190,15 @@ while True:
|
|||||||
dnlFilename = separatedCommand[1].split(os.path.sep)[-1]
|
dnlFilename = separatedCommand[1].split(os.path.sep)[-1]
|
||||||
cmd = 'DNL ' + dnlFilename
|
cmd = 'DNL ' + dnlFilename
|
||||||
network.sendMessage(cmd.encode('UTF-8'))
|
network.sendMessage(cmd.encode('UTF-8'))
|
||||||
file = network.recieveMessage().decode('UTF-8')
|
filecontent = network.recieveMessage()
|
||||||
response = network.recieveMessage().decode('UTF-8')
|
response = network.recieveMessage().decode('UTF-8')
|
||||||
|
|
||||||
if response == 'OK':
|
print(DOWNLOAD_LOCATION + dnlFilename)
|
||||||
|
|
||||||
|
if response == 'EOF':
|
||||||
with open(DOWNLOAD_LOCATION + dnlFilename, "wb+") as file:
|
with open(DOWNLOAD_LOCATION + dnlFilename, "wb+") as file:
|
||||||
file.writelines(file)
|
file.write(filecontent)
|
||||||
|
print('OK')
|
||||||
else:
|
else:
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
@ -181,3 +208,4 @@ while True:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error: ' + str(e))
|
print('Error: ' + str(e))
|
||||||
|
continue
|
||||||
|
File diff suppressed because one or more lines are too long
@ -13,7 +13,7 @@ from netsim import network_interface
|
|||||||
|
|
||||||
|
|
||||||
class NetWrapper:
|
class NetWrapper:
|
||||||
def __init__(self, privateKey: RsaKey, clientAddress: str, username: str, serverPubKey: RsaKey,
|
def __init__(self, privateKey: RsaKey, clientAddress: str, serverPubKey: RsaKey, username: str = "",
|
||||||
serverAddr: str = 'A'):
|
serverAddr: str = 'A'):
|
||||||
self.network = network_interface('./../../netsim/network/', clientAddress)
|
self.network = network_interface('./../../netsim/network/', clientAddress)
|
||||||
self.serverAddr = serverAddr
|
self.serverAddr = serverAddr
|
||||||
@ -26,13 +26,14 @@ class NetWrapper:
|
|||||||
allowed_chars: str = string.ascii_letters + string.punctuation) -> str:
|
allowed_chars: str = string.ascii_letters + string.punctuation) -> str:
|
||||||
return ''.join(random.choice(allowed_chars) for x in range(str_size))
|
return ''.join(random.choice(allowed_chars) for x in range(str_size))
|
||||||
|
|
||||||
def ecryptRSAMessage(self, message: str) -> bytes:
|
def ecryptRSAMessage(self, message: bytes) -> bytes:
|
||||||
cipher_rsa = PKCS1_OAEP.new(self.serverPubKey)
|
cipher_rsa = PKCS1_OAEP.new(self.serverPubKey)
|
||||||
encrypted_msg = cipher_rsa.encrypt(message.encode('UTF-8'))
|
encrypted_msg = cipher_rsa.encrypt(message)
|
||||||
return encrypted_msg
|
return encrypted_msg
|
||||||
|
|
||||||
def signRSAHeader(self, type: str, extradata: dict) -> (bytes, bytes):
|
def signRSAHeader(self, type: str, extradata: dict) -> (bytes, bytes):
|
||||||
header = json.dumps({'type': type, 'source': self.network.own_addr}.update(extradata)).encode('UTF-8')
|
mandatory = {'type': type, 'source': self.network.own_addr}
|
||||||
|
header = json.dumps({**mandatory, **extradata}).encode('UTF-8')
|
||||||
h = SHA512.new(header)
|
h = SHA512.new(header)
|
||||||
headersignature = pkcs1_15.new(self.privateKey).sign(h)
|
headersignature = pkcs1_15.new(self.privateKey).sign(h)
|
||||||
return header, headersignature
|
return header, headersignature
|
||||||
@ -58,7 +59,7 @@ class NetWrapper:
|
|||||||
|
|
||||||
def identifyServer(self) -> bool:
|
def identifyServer(self) -> bool:
|
||||||
randommsg = self.randomStringGenerator()
|
randommsg = self.randomStringGenerator()
|
||||||
encrypted_msg = self.ecryptRSAMessage(randommsg)
|
encrypted_msg = self.ecryptRSAMessage(randommsg.encode('UTF-8'))
|
||||||
header, headersignature = self.signRSAHeader('IDY', {'username': self.username})
|
header, headersignature = self.signRSAHeader('IDY', {'username': self.username})
|
||||||
identMsg = json.dumps(
|
identMsg = json.dumps(
|
||||||
{'header': b64encode(header).decode('UTF-8'),
|
{'header': b64encode(header).decode('UTF-8'),
|
||||||
@ -79,11 +80,11 @@ class NetWrapper:
|
|||||||
|
|
||||||
def createEncryptedChannel(self) -> None:
|
def createEncryptedChannel(self) -> None:
|
||||||
dh = pyDH.DiffieHellman()
|
dh = pyDH.DiffieHellman()
|
||||||
mypubkey = self.ecryptRSAMessage(str(dh.gen_public_key()))
|
mypubkey = self.ecryptRSAMessage(str(dh.gen_public_key()).encode('UTF-8'))
|
||||||
header, headersignature = self.signRSAHeader("DH",{})
|
header, headersignature = self.signRSAHeader("DH",{})
|
||||||
jsonmsg = json.dumps(
|
jsonmsg = json.dumps(
|
||||||
{'header': b64encode(header).decode('UTF-8'), 'headersignature': b64encode(headersignature).decode('UTF-8'),
|
{'header': b64encode(header).decode('UTF-8'), 'headersignature': b64encode(headersignature).decode('UTF-8'),
|
||||||
'message': mypubkey}).encode('UTF-8')
|
'message': b64encode(mypubkey).decode('UTF-8')}).encode('UTF-8')
|
||||||
self.network.send_msg(self.serverAddr, jsonmsg)
|
self.network.send_msg(self.serverAddr, jsonmsg)
|
||||||
decodedmsg, header = self.recieveAndUnpackRSAMessage()
|
decodedmsg, header = self.recieveAndUnpackRSAMessage()
|
||||||
if not self.verifyRSAHeaderSignature(b64decode(decodedmsg['header']),
|
if not self.verifyRSAHeaderSignature(b64decode(decodedmsg['header']),
|
||||||
@ -104,6 +105,10 @@ class NetWrapper:
|
|||||||
print("Authentication error")
|
print("Authentication error")
|
||||||
|
|
||||||
def connectToServer(self, password: str) -> None:
|
def connectToServer(self, password: str) -> None:
|
||||||
|
if self.username == "":
|
||||||
|
raise Exception('Username is not initialized')
|
||||||
|
if password == "":
|
||||||
|
raise Exception('Passowrd may not be empty')
|
||||||
identStatus = self.identifyServer()
|
identStatus = self.identifyServer()
|
||||||
if not identStatus:
|
if not identStatus:
|
||||||
raise Exception('Server identification faliure')
|
raise Exception('Server identification faliure')
|
||||||
|
Reference in New Issue
Block a user