KekSec

CryptoChat P2P NSA Proof Chat Client

Nov 10th, 2015
646
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.89 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #-------------------------------------------------------------------------------
  4. # Name:        CryptoChat
  5. # Purpose:     Secure P2P chat client
  6. # Notes:       Encrypted with XOR + AES + base64
  7. #              custom chat protocol
  8. #
  9. # Author:      Freak @ PopulusControl (aka sudoer)
  10. #
  11. # Created:     21/02/2015
  12. # Last update: 14/07/2015
  13. #
  14. # Copyright:   (c) Freak 2015
  15. # Licence:     GPLv3
  16. #    This program is free software: you can redistribute it and/or modify
  17. #    it under the terms of the GNU General Public License as published by
  18. #    the Free Software Foundation, either version 3 of the License, or
  19. #    (at your option) any later version.
  20. #
  21. #    This program is distributed in the hope that it will be useful,
  22. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. #    GNU General Public License for more details.
  25. #
  26. #    You should have received a copy of the GNU General Public License
  27. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  28. #-------------------------------------------------------------------------------
  29. import sys
  30. import socket
  31. import base64
  32. import random
  33. import string
  34. import hashlib
  35. from Crypto.Cipher import AES
  36. from Crypto import Random
  37. from threading import Thread
  38. import getpass
  39. class CryptoClient():
  40.     def __init__(self):
  41.         print "Welcome to CryptoChat, a secure P2P chat client coded by Freak"
  42.         print "if you dont know what your doing read the README.md!!!"
  43.         self.IP = raw_input("Please enter the IP address you wish to chat with: ")
  44.         self.PORT = int(raw_input("Enter the port for communication: "))
  45.         print
  46.         print "Now enter the keys for the different encryption methods, make sure they are different."
  47.         print "Please note they will note be printed for your security."
  48.         print
  49.         self.EncryptKeyXOR = getpass.getpass("Enter desired key for XOR encryption: ")
  50.         self.EncryptKeyAES = hashlib.md5(getpass.getpass("Enter a secure passphrase for AES: ")).hexdigest()
  51.         print
  52.         raw_input("Press enter when both clients are ready.")
  53.         ###Shit for AES padding
  54.         BS = 16
  55.         self.pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
  56.         self.unpad = lambda s : s[:-ord(s[len(s)-1:])]
  57.         ###Start chat server and client
  58.         try:
  59.             Thread(target=self.RecvMSG, args=()).start()
  60.         except:
  61.             print self.IP + " is not ready! Press enter when " + self.IP + " is ready."
  62.             raw_input()
  63.             Thread(target=self.RecvMSG, args=()).start()
  64.         self.SendMSG()
  65.     def EncryptAES(self,raw):
  66.         raw = self.pad(raw)
  67.         iv = Random.new().read( AES.block_size )
  68.         cipher = AES.new( self.EncryptKeyAES, AES.MODE_CBC, iv )
  69.         return base64.b64encode( iv + cipher.encrypt( raw ) )
  70.     def DecryptAES(self,enc):
  71.         enc = base64.b64decode(enc)
  72.         iv = enc[:16]
  73.         cipher = AES.new(self.EncryptKeyAES, AES.MODE_CBC, iv )
  74.         return self.unpad(cipher.decrypt( enc[16:] ))
  75.     def XOR(self,securekey,data,mode):
  76.         if mode == 1:
  77.             securekey=securekey[::-1]
  78.         temp=''
  79.         for char in securekey:
  80.             for x in data:
  81.                 temp += chr(ord(x)^ord(char))
  82.             data=temp
  83.             temp=''
  84.         return data
  85.     def EncryptMSG(self,data):
  86.         data = self.XOR(self.EncryptKeyXOR,data,0)
  87.         data = self.EncryptAES(data)
  88.         return data
  89.     def DecryptMSG(self,data):
  90.         data = self.DecryptAES(data)
  91.         data = self.XOR(self.EncryptKeyXOR,data,1)
  92.         return data
  93.     def SendMSG(self):
  94.         clientsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
  95.         print
  96.         print "You are now talking to '"+self.IP+"'"
  97.         while 1:
  98.             message = raw_input("")
  99.             if message.startswith("/send"): #send file command
  100.                 self.SendFILE(message.split(" ")[1])
  101.                 continue
  102.             if message=="/leave":
  103.                 message = self.EncryptMSG("\x03")
  104.                 clientsock.sendto(message, (self.IP, self.PORT))
  105.                 sys.exit(0)
  106.             if message.startswith("/msg"):
  107.                 self.IP=message.split(" ")[1]
  108.                 print "[CLIENT] You are now talking to '"+self.IP+"'"
  109.                 continue
  110.             else:
  111.                 message = self.EncryptMSG("\x01"+message)
  112.                 clientsock.sendto(message, (self.IP, self.PORT))
  113.     def SendFILE(self,file_):
  114.         if file_.startswith(".") or file_.startswith("/"): #added security measure.
  115.             print "[CLIENT] For security and safety reasons, filenames starting with '.' or '/' will not be sent. Aborting."
  116.         else:
  117.             clientsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
  118.             data="\x02"+file_+"\xFF"
  119.             f=open(file_,"rb")
  120.             data+=f.read()
  121.             f.close()
  122.             data = self.EncryptMSG(data)
  123.             clientsock.sendto(data, (self.IP, self.PORT))
  124.             print "[CLIENT] File Sent!"
  125.     def RandStr(self,length):
  126.         return ''.join(random.choice(string.letters) for _ in range(length))
  127.     def RecvMSG(self):
  128.         serversocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #UDP
  129.         serversocket.bind(('', self.PORT))
  130.         while 1:
  131.             (data, addr) = serversocket.recvfrom(1073741824) # buffer size is 1 gbit (for large files/images)
  132.             data = self.DecryptMSG(data)
  133.             if data.startswith("\x02"):
  134.                 filename=''
  135.                 data=list(data)
  136.                 del data[0]
  137.                 for i in data:
  138.                     if i=="\xFF":
  139.                         break
  140.                     else:
  141.                         filename += i
  142.                 del data[0] # delete protocol char
  143.                 del data[:len(filename)] # delete file end char
  144.                 if filename.startswith(".") or filename.startswith("/"): #attempted exploit!
  145.                     print "[!!!ALERT!!!] "+addr[0] + " has attempted to overwrite your " + filename
  146.                 else:
  147.                     print "[CLIENT] " + addr[0] + " has sent " + filename
  148.                     print "[CLIENT] Downloading..." #download dat shit
  149.                     data=''.join(data)
  150.                     f = open(filename,"wb")
  151.                     f.write(data)
  152.                     f.close()
  153.                     print "[CLIENT] Saved."
  154.             elif data.startswith("\x01"): # all messages start with "\x01" to prevent file spamming
  155.                 data=list(data)
  156.                 del data[0]
  157.                 data=''.join(data)
  158.                 print "["+addr[0]+"] >  |   "+data
  159.             elif data.startswith("\x03"):
  160.                 print "[CLIENT] "+addr[0]+" has left."
  161.                 sys.exit(0)
  162.  
  163. if __name__=="__main__":
  164.     CryptoClient()
Comments
  • User was banned
Add Comment
Please, Sign In to add comment