Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
-Working on a method to mitigate the potential attack vector of a Man in The Middle attack by allowing presaved public keys in the source code of the program. The program will encrypt the AES encryption keys using the public key which only the valid private key owner can decrypt, i.e the server.
  • Loading branch information
Scratchcat1 authored Oct 20, 2017
1 parent a0d2791 commit f687675
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 47 deletions.
35 changes: 30 additions & 5 deletions AATC_Config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
PRE_SHARED_ENCRYPTION_KEYS_ENABLE = False
PRE_SHARED_AES_KEY = b"00000000000000000000000000000000"
PRE_SHARED_IV_KEY = b"00000000000000000000000000000000"
SERVER_NAME = "AATC"
SERVER_PRIVATE_KEY = b'0\x82\t)\x02\x01\x00\x02\x82\x02\x01\x00\x9a`\xfa\xdf\x0e%\x8bv\x82\x94v\xe1gt\xafK\'\xa1\x87l\xef\xaeV\xc5\x9e\xfb\x8e\x00\xbal1u\xab\xbe\x92"\xd6\t\xad\x12\xdbx4\x0c\x9d\xdb\xf9\xcf\x08\x8au+\xa8[n\x1b\x86i\xb76N~\x82\xcf\xdd\xd0\xa1c\xb7\xbf\xbcT\x93\x9fT\x1c\xbb\x85\xf2\xd5\xa6\xfbx,\xf1i\xecv{\x85\x92\xd6\xc5a\x168\x93.\xa4t`\x06D\xbd\x969\xba\xec\xda<&\xaf\x8d\xb0\xc6\x99\xb6k\x86\x96\x0c\x19\x0b\x1a\x82T\x1c\xcbI\xe4P\x00\xd0\x0f\x9eP\x1cw\x06\xc1\x18\x1b\xc46g\xe2\xd8iSy\xc2?\tP\xef\xd1jlf\x97C?\xfa*\x92\x1c\xd8\xf0l\x02\xd3\xc2W7\xc4\xd8\xb2\xd2\xa5\x00?\xe9K\xce\x0e\xc0\xa3\x8c\xc7\xee\xd8\x9aq\xe0\xe2n6~\xd9-_OE]Ui9w#\x7f\xf9\x11QE\xb9^/\xdc0=\x9d\x12+nU\xd4\xc6H\xebAK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xdd0\xd7\x9e\xf6\x85i\xeb\x02\xa6\x1d\xfe6\x8b\xbe\x91\x15\xb4\xab\x89\x00U/E\x02\x82^\xaa\xdc\xf7\x07\xb1xJ\xa7\xb7\xf0\xfd\xc05\x93\x0f\x92(R\xf1\x02\x82\x01\x01\x00\xbe\x89O\xbdCq\x9f\x08\xf5\xe3\x9e&\x9b\xd0\xfe\xf8}\xdc\x04\x1f\xba\x0fA\xad\x80\xe4^\xc1\x11\x08\x13\x15\xd4ml\xd1\x11\xb8\xdaJk\xb3\xc1\xe8(\n\x92\xff\xad\x10\xf2\\\x07\xef`#Z\xa3\x08\x07h\\\x0e\x9c\xe8\xd4\x97\xd1\x84\xf3m\x94J5\xd5\xfe\x90\x1dC\xe5\x91Be`\xf8\xeab\x00\xb0\x99\x94E_x\x83\xac(A\x83(m\x98\xbb\xeb\xef\xab\x86/\xab\x10\xacK)"\xb4kjS\xaar\x18\xfc\x080\xe7\xd9\\\x8d\xc9\xea\xfe\xcc~f\xea\x1d\x15\xd6 \xec5\xf5<\xfchAP$\x19\xe8\x03\xbf\x15\x8a\xe1\x1c\xeb\xa4&\xb64e3\xde\xd9\xf1\\\x95\xf3\xb8\xdd"\xe1\x08\x86\x84\xa2\xb7\x8d\x8c$ 7%y\x15UP\xbe\x07\xfb)\x13\xec\x1e\x94\x07a\xdc\xecr\xcf\xben\xdf\x1b\x02\xb9\xe6\x0fA\xe7\x8cE\x0e\xf2\xf5my$\xce\x08\x04\x00$nm\x92\xb3\xae\xb2\xd5\x9a\xcb\xab\x88\x0f\xb0\r4f\xf2\xb2`\xf6A\xf2t;\xaa\x8b\xebQ\x18\x97\xd9\x02\x82\x01\x01\x00\xcfkk}Z1\xef\xc6\x03S\xda\x00\x12\'\xf1#\xab\xd2\xd4\xd8\x96\xc1\xac\xcd\x8c\xfb\xc0\x88\xcd\xee]\r\x96\xbe\x87\x87/\xe6u\xfdx\x19\xc9\xda\'\x1e\xfd\xec\xe6\x03j\xe0\xfe\xbb\xac\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\xbb\xbe\x8d\xee:\xb4\xeb%\xbf2ya\x9f\x87\x88\xe0]\xa1\x9a{\xdd\xb5\x04,4V\xe8\xd1\xfd\'I\xc18\xedG\x88\xe1<\xd7\xda\x1b\x19\xef\xad\xc2{\x9e\x13\xdb\xc3%b\xe5\x12CJ\xc0F\xd1\xbd\xd4\xcd|\xff2I;mp\xaa\x05X\xf8\xedNI\x93\xca\x91\xd9\x83\x8e\x04\xe2|\xf5\xa1O\xfbq~;\xb0o\xb0-\x03\xca\x1a\xc25is\xb0F\xc0L\xe5U\xb20!g&c\xb9\x19\xc6[o8{\x87a\xfe<\xfb\x8c\xc4U\x18Bz\xe8D\xdfd9\x88\xe9\xfa\x9f|\x8f\x1f#U\xad\xbf\xe2\x17\x12\xd4\xff\x05\x83FV=\xab\x06x\xc6\xe4>?\x9aMFg\x85]K\xc2\xd4\x02\x88J\x87\x89`\x1a\xac\xcb\xa9\x03\x05\xcb^\xf5i\x00\xce%\x00H, \xa9~\xdd\x8e\x9b{,\x82\x04Z\x94\xd7\xd2\xbe\xd7\x94b\x1bJ*yP\x8c\xe76\xd4'


SET_ENCRYPTION_KEYS_ENABLE = False
SET_AES_KEY = b"00000000000000000000000000000000"
SET_IV_KEY = b"00000000000000000000000000000000"


DEFAULT_AES_KEYSIZE = 32
ALLOWED_AES_KEYSIZES = [16,24,32]

DEFAULT_RSA_KEYSIZE = 2048

ENCRYPTION_USE_PRESHARED_KEYS = False
AUTO_GENERATE_FALLBACK = False
CERTIFICATES = {
"AATC":[
{
"ServerName":"AATC",
"NotBefore":1,
"NotAfter":1000000000,
"PublicKey":b'0\x82\x02"0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x01\x05\x00\x03\x82\x02\x0f\x000\x82\x02\n\x02\x82\x02\x01\x00\x9a`\xfa\xdf\x0e%\x8bv\x82\x94v\xe1gt\xafK\'\xa1\x87l\xef\xaeV\xc5\x9e\xfb\x8e\x00\xbal1u\xab\xbe\x92"\xd6\t\xad\x12\xdbx4\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92\xb0\xfc\xee\xb7\xd1\xcf\xa1OF\xc0;\xf3\xf2\xe9\xbez"\xaf\xd3\xd4\xa4\xddG\xb0]ro\xf7\x82\xfc\x1a\xf1\xed\xb92$\xcbd%j\x98\xcbd\x7f\x1b!\xc6l\xd2<\x9d8\x97\xe5\xa0\xac\xc7G\xb9\xda\x90\xce~\xb1\xc1\xab\xe2\xcd\xef\x8d\xf8g\xd1I\x02\x03\x01\x00\x01'
},
],



}






DEFAULT_DRONE_BATTERY_VALUE = 100
DEFAULT_DRONE_ATWAYPOINT_SIZES = (0.001,0.001,5)
DEFAULT_DRONE_COORDINATE = (0,0,0)
DRONE_BATTERY_DRAIN_MULT = 1


DRONE_BATTERY_DRAIN_MULT = 1
NOFLYZONE_THRESHOLD_COST = 50
200 changes: 158 additions & 42 deletions AATC_Crypto.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#AATC crypto module
import codecs,recvall,ast,binascii,os,AATC_Config
import codecs,recvall,ast,binascii,os,AATC_Config,time
from Crypto.Cipher import AES,PKCS1_OAEP
from Crypto.PublicKey import RSA

Expand All @@ -21,11 +21,8 @@ class Crypter:
"""
def __init__(self, con, mode = "CLIENT",AutoGenerate = True):
self.con = con
self.SetMode(mode)
if AATC_Config.PRE_SHARED_ENCRYPTION_KEYS_ENABLE: #Allows preshared encryption keys to be used
self.SetEncryptionKeys(AATC_Config.PRE_SHARED_AES_KEY, AATC_Config.PRE_SHARED_IV_KEY)

elif AutoGenerate:
self.SetMode(mode)
if AutoGenerate:
self.GenerateKey()

def SetMode(self,mode):
Expand All @@ -44,33 +41,77 @@ def GenerateKey(self,key_size = AATC_Config.DEFAULT_RSA_KEYSIZE):


def ClientGenerateKey(self,RSA_KeySize,AES_KeySize= AATC_Config.DEFAULT_AES_KEYSIZE):

self.Send(("GetServerName",()))
Sucess,Message,Data = self.SplitData(self.Recv())
if not Sucess:
raise ValueError("Server did not recognise command"+Message)
ServerName = Data[0]

if AATC_Config.SET_ENCRYPTION_KEYS_ENABLE: #Allows preshared encryption keys to be used
self.SetEncryptionKeys(AATC_Config.SET_AES_KEY, AATC_Config.SET_IV_KEY)

elif AATC_Config.ENCRYPTION_USE_PRESHARED_KEYS:
self.ClientPreSharedKeys(ServerName,RSA_KeySize,AES_KeySize)

else:
self.ClientExchangeKeys(RSA_KeySize,AES_KeySize)




self.Send(("Exit",()))
Sucess,Message,Data = self.SplitData(self.Recv())
if not Sucess:
raise Exception("Server failed to exit"+Message)



def ClientPreSharedKeys(self,ServerName,RSA_KeySize,AES_KeySize):
AESKey,IV = GenerateKeys(AES_KeySize)
Certificate = GetCertificates(ServerName)

if Certificate:
publicKey = Certificate["PublicKey"]
PKO = PKCS1_OAEP.new(RSA.import_key(publicKey))
EncryptedAESKey = PKO.encrypt(AESKey)
EncryptedIV = PKO.encrypt(IV)
self.SetEncryptionKeys(AESKey,IV)
self.Send(("SetKey",(EncryptedAESKey,EncryptedIV)))
data = self.Recv()


else:
print("No Valid Certificates found")
if AATC_Config.AUTO_GENERATE_FALLBACK:
self.ClientExchangeKeys(RSA_KeySize,AES_KeySize)
else:
raise Exception("No valid certificate found. Exception raised")



def ClientExchangeKeys(self,RSA_KeySize,AES_KeySize):
RSAKey = RSA.generate(RSA_KeySize)

privateKey = RSAKey.exportKey("DER")
publicKey = RSAKey.publickey().exportKey("DER")

self.Send(("ExchangeKey",(publicKey,AES_KeySize))) #Format ("ExchangeKey",("a1b2c3.....",))
data = self.Recv()
Sucess,Message = data[0],data[1] #Format (Sucess,("a2c3d4..."))
if Sucess == False:
raise Exception("Error occured while exchanging keys")

self.Send(("Exit",()))
data = self.Recv()
if data[0] == False:
raise Exception("Server failed to commit to exit")
self.Send(("GenerateKey",(publicKey,AES_KeySize)))
Sucess,Message,data = self.SplitData(self.Recv())

RSAPrivateKey = RSA.import_key(privateKey)
RSAPrivateObject = PKCS1_OAEP.new(RSAPrivateKey)

self.AESKey = RSAPrivateObject.decrypt(Message[0])
self.IV = RSAPrivateObject.decrypt(Message[1])

self.SetEncryptionKeys(self.AESKey,self.IV)

AESKey = RSAPrivateObject.decrypt(data[0])
IV = RSAPrivateObject.decrypt(data[1])

if Sucess == False:
raise Exception("Error occured while exchanging keys")

self.SetEncryptionKeys(AESKey,IV)


################################################################

def ServerGenerateKey(self):

Expand All @@ -79,29 +120,59 @@ def ServerGenerateKey(self):
data = self.Recv()
Command, Arguments = data[0],data[1]

if Command == "ExchangeKey":
publicKey,AES_KeySize = Arguments[0],Arguments[1]
if AES_KeySize not in AATC_Config.ALLOWED_AES_KEYSIZES:
AES_KeySize = AATC_Config.DEFAULT_AES_KEYSIZE #If key size is not valid set size to default of AATC_Config.DEFAULT_AES_KEYSIZE

self.AESKey = binascii.b2a_hex(os.urandom(AES_KeySize//2)) # Here to allow regeneration of AES key while still in loop if required.
self.IV = binascii.b2a_hex(os.urandom(AES_KeySize//2))

RSAPrivateKey = RSA.import_key(publicKey)
PublicKeyObject = PKCS1_OAEP.new(RSAPrivateKey)
if Command == "GenerateKey":
Sucess,Message,Data = self.ServerGenerateKeys(Arguments)

elif Command == "GetServerName":
Sucess,Message,Data = self.GetServerName(Arguments)

elif Command == "SetKey":
Sucess,Message,Data = self.ServerSetKey(Arguments)

EncryptedAESKey = PublicKeyObject.encrypt(self.AESKey)
EncryptedIV = PublicKeyObject.encrypt(self.IV)
self.Send((True,(EncryptedAESKey,EncryptedIV)))

elif Command == "Exit":
self.Send((True,()))
Sucess,Message,Data = True,"Exiting",[]
Exit = True

else:
self.Send((False,("Command does not exist",)))
Sucess,Message,Data = False,"Command does not exist",[]

self.SetEncryptionKeys(self.AESKey,self.IV)
self.Send((Sucess,Message,Data))




def ServerGenerateKeys(self,Arguments):
publicKey,AES_KeySize = Arguments[0],Arguments[1]

if AES_KeySize not in AATC_Config.ALLOWED_AES_KEYSIZES:
AES_KeySize = AATC_Config.DEFAULT_AES_KEYSIZE #If key size is not valid set size to default of AATC_Config.DEFAULT_AES_KEYSIZE

AESKey,IV = GenerateKeys(AES_KeySize)

PublicKeyObject = PKCS1_OAEP.new( RSA.import_key(publicKey))

EncryptedAESKey = PublicKeyObject.encrypt(AESKey)
EncryptedIV = PublicKeyObject.encrypt(IV)

self.SetEncryptionKeys(AESKey,IV)
return True,"Instated encryption keys",[EncryptedAESKey,EncryptedIV]

def GetServerName(self,Arguments = None):
return True,"Server Name",[AATC_Config.SERVER_NAME]

def ServerSetKey(self,Arguments):
PKO = PKCS1_OAEP.new(RSA.import_key(AATC_Config.SERVER_PRIVATE_KEY))
AESKey,IV = Arguments[0],Arguments[1]
AESKey,IV = PKO.decrypt(AESKey),PKO.decrypt(IV)
self.SetEncryptionKeys(AESKey,IV)
return True,"Keys set",[]





###############################################


def SetEncryptionKeys(self,AESKey,IV):
Expand All @@ -116,16 +187,29 @@ def SetEncryptionKeys(self,AESKey,IV):






















def Encrypt(self,data):
## pad_size = 16-len(data)%16
## data += b" "*pad_size
return self.EncryptAES.encrypt(data)

def Decrypt(self,data):
## pad_size = 16-len(data)%16 # - should not be nessesary as data will come in 16 size blocks and change in message would thwart decryption.
## data += b" "*pad_size
return self.DecryptAES.decrypt(data)


Expand All @@ -136,6 +220,38 @@ def Send(self,data):
def Recv(self):
data = recvall.recvall(self.con)
data = ast.literal_eval(codecs.decode(data))
# (Command,Arguments)
return data
def SplitData(self,data):
return data[0],data[1],data[2]





def GenerateKeys(AES_KeySize):
AESKey = binascii.b2a_hex(os.urandom(AES_KeySize//2)) # Here to allow regeneration of AES key while still in loop if required.
IV = binascii.b2a_hex(os.urandom(AES_KeySize//2))
return AESKey,IV

def GetCertificates(ServerName):
Certificates = AATC_Config.CERTIFICATES.get(ServerName)
if Certificates == None:
return False

found = False
for Certificate in Certificates:
if Validate(Certificate):
found = True
break

if not found:
return False

return Certificate


def Validate(Certificate):
if Certificate["NotBefore"] <= time.time() and Certificate["NotAfter"] >= time.time():
return True
else:
return False

0 comments on commit f687675

Please sign in to comment.