예제 #1
0
def create_payload(sender,unique_id,reply):
	try:
		additional1 = "COMMAND-REPLY"
		additional2 = unique_id
		con = sql.connect("info.db")
		con.row_factory = sql.Row
		cur = con.cursor()
		cur.execute('SELECT * FROM accounts')
		accounts = cur.fetchall()
		account = accounts[0]["identifier"]
		private_key_hex = accounts[0]["private_key_hex"]
		public_key_hex = accounts[0]["public_key_hex"]
		cur.execute('SELECT * FROM users WHERE identifier=?', (sender,))
		result = cur.fetchall()
		if len(result) == 1:
			key = result[0]["EncryptionKey"]
		else:
			requests.post("http://127.0.0.1:10001/user/search", data=sender)
			return "User is offline"
		data = reply
		data = encrypt.encryptWithRSAKey(key, data)
		timestamp = str(int(time.time()))
		final = "TREESSH" + ":" + account + ":" + sender + ":" + timestamp + ":" + additional1 + ":" + additional2 + ":" + public_key_hex + ":" + data
		tx_hash = sha256(final.rstrip()).hexdigest()
		signature = messages.sign_message(private_key_hex, tx_hash)
		payload = "TREESSH" + "," + account + "," + sender + "," + timestamp + "," + additional1 + "," + additional2 + "," + public_key_hex + "," + data + "," + tx_hash + "," + signature.encode("hex")
		return payload
	except:
		return False
	finally:
		try:
			con.close()
		except:
			pass
예제 #2
0
def new_data(peer,payload):
	try:
		con = sql.connect("info.db")
		con.row_factory = sql.Row
		cur = con.cursor()
		cur.execute('SELECT * FROM fake_account')
		accounts = cur.fetchall()
		Account = accounts[0]["fakeidentifier"]
		private_key_hex = accounts[0]["fake_private_key_hex"]
		public_key_hex = accounts[0]["fake_public_key_hex"]
		Address = address.keyToAddr2(public_key_hex, Account)
		timestamp = str(int(time.time()))
		signature = messages.sign_message(private_key_hex, Address+":"+timestamp)
		signature = signature.encode("hex")
		ip_result = whatis(peer)
		if ip_result == False:
			return
		if ip_result == "4":
			return_data = requests.get("http://"+peer+":12995/proofofwork/"+Address+"/"+public_key_hex+"/"+timestamp+"/"+signature)
		else:
			return_data = requests.get("http://["+peer+"]:12995/proofofwork/"+Address+"/"+public_key_hex+"/"+timestamp+"/"+signature)
		mining = return_data.content
		mining_details = mining.split(",")
		hashed = mining_details[0]
		deadline = mining_details[1]
		nonce = "None"
		if hashed != "None" and deadline != "None":
			nonce = proof_of_work.solve(hashed,deadline)
			if nonce == False:
				return
		cur.execute('SELECT * FROM peers WHERE peer=?', (peer,))
		result = cur.fetchall()
		if len(result) == 1:
			fakeIdentifier = result[0]["identifier"]
			cur.execute('SELECT * FROM fakeAccounts WHERE identifier=?', (fakeIdentifier,))
			result = cur.fetchall()
			if len(result) == 1:
				usersEncryptionKey = result[0]["EncryptionKey"]
			else:
				return
		else:
			return
		payload = encrypt.encryptWithRSAKey(usersEncryptionKey,payload)
		if payload == False:
			return
		if ip_result == "4":
			return_data = requests.post("http://"+peer+":12995/data/new/"+Address+"/"+public_key_hex+"/"+timestamp+"/"+signature+"/"+hashed+"/"+nonce, data=payload)
		else:
			return_data = requests.post("http://["+peer+"]:12995/data/new/"+Address+"/"+public_key_hex+"/"+timestamp+"/"+signature+"/"+hashed+"/"+nonce, data=payload)
	except:
		pass
	finally:
		try:
			con.close()
		except:
			pass
예제 #3
0
def create_payload(sender,payment_type,address_type,address,amount,additional2,product_hash,quantity):
	try:
		additional1 = "REPLY"
		con = sql.connect("info.db")
		con.row_factory = sql.Row
		cur = con.cursor()
		cur.execute('SELECT * FROM accounts')
		accounts = cur.fetchall()
		account = accounts[0]["identifier"]
		private_key_hex = accounts[0]["private_key_hex"]
		public_key_hex = accounts[0]["public_key_hex"]
		cur.execute('SELECT * FROM users WHERE identifier=?', (sender,))
		result = cur.fetchall()
		if len(result) == 1:
			key = result[0]["EncryptionKey"]
		else:
			requests.post("http://127.0.0.1:10000/user/search", data=sender)
			return "User is offline"
		transaction_on_success = os.urandom(32)
		transaction_on_success = transaction_on_success.encode("hex")
		transaction_on_success = sha256(transaction_on_success.rstrip()).hexdigest()
		data = address + "," + amount + "," + transaction_on_success
		data = encrypt.encryptWithRSAKey(key, data)
		timestamp = str(int(time.time()))
		final = "TREEPAY" + ":" + account + ":" + sender + ":" + timestamp + ":" + additional1 + ":" + additional2 + ":" + public_key_hex + ":" + data
		tx_hash = sha256(final.rstrip()).hexdigest()
		signature = messages.sign_message(private_key_hex, tx_hash)
		payload = "TREEPAY" + "," + account + "," + sender + "," + timestamp + "," + additional1 + "," + additional2 + "," + public_key_hex + "," + data + "," + tx_hash + "," + signature.encode("hex")
		time_generated = str(int(time.time()))
		cur.execute('INSERT INTO addresses (type,identifier,ticker,address,amount,time_generated,transaction_on_success) VALUES (?,?,?,?,?,?,?)', (payment_type,sender,address_type,address,amount,time_generated,transaction_on_success))
		con.commit()
		cur.execute('INSERT INTO messages (type,sender,address,times,refers_to,transaction_on_success,time_generated) VALUES (?,?,?,?,?,?,?)', (payment_type,sender,address,quantity,product_hash,transaction_on_success,time_generated))
		con.commit()
		return payload
	except:
		return False
	finally:
		try:
			con.close()
		except:
			pass
예제 #4
0
def send_file(sender,unique_id,filename):
	try:
		additional1 = "DOWNLOAD-REPLY"
		con = sql.connect("info.db")
		con.row_factory = sql.Row
		cur = con.cursor()
		cur.execute('SELECT * FROM accounts')
		accounts = cur.fetchall()
		account = accounts[0]["identifier"]
		private_key_hex = accounts[0]["private_key_hex"]
		public_key_hex = accounts[0]["public_key_hex"]
		cur.execute('SELECT * FROM users WHERE identifier=?', (sender,))
		result = cur.fetchall()
		if len(result) == 1:
			key = result[0]["EncryptionKey"]
		else:
			requests.post("http://127.0.0.1:10001/user/search", data=sender)
			return "User is offline"
		return_data = requests.get("http://127.0.0.1:10001/active_directory/"+sender)
		path = return_data.content
		if os.path.isfile(os.path.join(path,"",filename)) == False:
			return False
		filesize = os.path.getsize(os.path.join(path,"",filename))
		filesize = float(filesize) / float(1048576)
		total_parts = int(math.ceil(filesize))
		if filesize <= 1 and filesize > 0:
			filename2 = encrypt.encryptWithRSAKey(key, filename)
			filename_details = encrypt.encryptWithRSAKey(key, "1/1")
			additional2 = unique_id + "|" + filename2 + "|" + filename_details
			with open(os.path.join(path,"",filename), "rb") as file_to_send:
				bytes = file_to_send.read(1048576)
			data = encrypt.encryptWithRSAKey(key, bytes)
			timestamp = str(int(time.time()))
			final = "TREESSH" + ":" + account + ":" + sender + ":" + timestamp + ":" + additional1 + ":" + additional2 + ":" + public_key_hex + ":" + data
			tx_hash = sha256(final.rstrip()).hexdigest()
			signature = messages.sign_message(private_key_hex, tx_hash)
			payload = "TREESSH" + "," + account + "," + sender + "," + timestamp + "," + additional1 + "," + additional2 + "," + public_key_hex + "," + data + "," + tx_hash + "," + signature.encode("hex")
			return_data = requests.post("http://127.0.0.1:10001/data/pool/new", data=payload)
			return True
		elif filesize > 1:
			filename2 = encrypt.encryptWithRSAKey(key, filename)
			starting = 1
			with open(os.path.join(path,"",filename), "rb") as file_to_send:
				filename_details = encrypt.encryptWithRSAKey(key, str(starting) + "/" + str(total_parts))
				additional2 = unique_id + "|" + filename2 + "|" + filename_details
				bytes = file_to_send.read(1048576)
				data = encrypt.encryptWithRSAKey(key, bytes)
				timestamp = str(int(time.time()))
				final = "TREESSH" + ":" + account + ":" + sender + ":" + timestamp + ":" + additional1 + ":" + additional2 + ":" + public_key_hex + ":" + data
				tx_hash = sha256(final.rstrip()).hexdigest()
				signature = messages.sign_message(private_key_hex, tx_hash)
				payload = "TREESSH" + "," + account + "," + sender + "," + timestamp + "," + additional1 + "," + additional2 + "," + public_key_hex + "," + data + "," + tx_hash + "," + signature.encode("hex")
				return_data = requests.post("http://127.0.0.1:10001/data/pool/new", data=payload)
				while bytes != "" and starting < total_parts:
					starting += 1
					filename_details = encrypt.encryptWithRSAKey(key, str(starting) + "/" + str(total_parts))
					additional2 = unique_id + "|" + filename2 + "|" + filename_details
					bytes = file_to_send.read(1048576)
					data = encrypt.encryptWithRSAKey(key, bytes)
					timestamp = str(int(time.time()))
					final = "TREESSH" + ":" + account + ":" + sender + ":" + timestamp + ":" + additional1 + ":" + additional2 + ":" + public_key_hex + ":" + data
					tx_hash = sha256(final.rstrip()).hexdigest()
					signature = messages.sign_message(private_key_hex, tx_hash)
					payload = "TREESSH" + "," + account + "," + sender + "," + timestamp + "," + additional1 + "," + additional2 + "," + public_key_hex + "," + data + "," + tx_hash + "," + signature.encode("hex")
					return_data = requests.post("http://127.0.0.1:10001/data/pool/new", data=payload)
			return True
		return False
	except:
		return False
	finally:
		try:
			con.close()
		except:
			pass
예제 #5
0
def tree_ssh(sender, receiver, timestamp, additional1, additional2,
             additional3, data, tx_hash, signature):
    try:
        con = sql.connect("info.db")
        con.row_factory = sql.Row
        con.text_factory = str
        cur = con.cursor()
    except:
        pass
    try:
        if additional1 == "CONNECT":
            if additional2 != "None":
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
            else:
                return
            data = decrypt.decryptWithRSAKey(EncryptionKey, data)
            if data == False:
                return
            if data != "None":
                return
            try:
                config = ConfigParser.RawConfigParser()
                config.read("treesshc")
                allowed_users_setting = config.get('Configuration',
                                                   'AllowedUsers')
                allowed_users = allowed_users_setting.split(",")
            except:
                allowed_users = []
            if sender not in allowed_users:
                return
            cur.execute('SELECT * FROM connected_to_us')
            result = cur.fetchall()
            if len(result) == 0:
                time_connected = str(int(time.time()))
                cur.execute(
                    'INSERT INTO connected_to_us (user_connected,time_connected) VALUES (?,?)',
                    (sender, time_connected))
                con.commit()
                user = getpass.getuser()
                hostname = socket.gethostname()
                command_line = user + "@" + hostname + ":~$"
                connect_reply.send_reply(sender, command_line)
                return True
            else:
                OK.send_OK(sender)
        elif additional1 == "CONNECT-REPLY":
            cur.execute('SELECT * FROM now_connected WHERE connected_to=?',
                        (sender, ))
            result = cur.fetchall()
            if len(result) == 0:
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
            else:
                return
            data = decrypt.decryptWithRSAKey(EncryptionKey, data)
            if data == False:
                return
            cur.execute(
                'UPDATE now_connected SET command_line=? WHERE connected_to=?',
                (data, sender))
            con.commit()
        elif additional1 == "DISCONNECT":
            if additional2 != "None":
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
            else:
                return
            data = decrypt.decryptWithRSAKey(EncryptionKey, data)
            if data == False:
                return
            if data != "None":
                return
            cur.execute('SELECT * FROM connected_to_us WHERE user_connected=?',
                        (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                cur.execute('DELETE FROM connected_to_us')
                con.commit()
                return True
            cur.execute('SELECT * FROM now_connected WHERE connected_to=?',
                        (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                cur.execute('DELETE FROM now_connected')
                con.commit()
                return True
        elif additional1 == "COMMAND":
            if len(additional2) != 64:
                return
            cur.execute('SELECT * FROM connected_to_us WHERE user_connected=?',
                        (sender, ))
            result = cur.fetchall()
            if len(result) == 0:
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
            else:
                return
            data = decrypt.decryptWithRSAKey(EncryptionKey, data)
            if data == False:
                return
            while data[0] == " ":
                data = data[1:]
            return_data = requests.get(
                "http://127.0.0.1:10001/active_directory/" + sender)
            path = return_data.content
            return_data = requests.get(
                "http://127.0.0.1:10001/active_directory")
            starting_folder = return_data.content
            try:
                if data == "cd":
                    current_path = "/"
                    while path != current_path:
                        current_path = path
                        path = os.path.abspath(os.path.join(path, os.pardir))
                    requests.post("http://127.0.0.1:10001/active_directory/" +
                                  sender + "/change",
                                  data=path)
                    result = "Directory changed."
                elif data == "cd .." or data == "cd..":
                    path = os.path.abspath(os.path.join(path, os.pardir))
                    requests.post("http://127.0.0.1:10001/active_directory/" +
                                  sender + "/change",
                                  data=path)
                    result = "Directory changed."
                elif "cd" in data:
                    details = data.split(" ")
                    directory = details[1]
                    if os.path.isdir(os.path.join(path, "",
                                                  directory)) == True:
                        path = os.path.join(path, "", directory)
                        requests.post(
                            "http://127.0.0.1:10001/active_directory/" +
                            sender + "/change",
                            data=path)
                        result = "Directory changed."
                    else:
                        result = "Directory doesn't exist."
                elif data == "ls" or data == "dir":
                    if path != starting_folder:
                        result = os.popen(data + " " + path).read()
                    else:
                        result = os.popen(data).read()
                    result = result[0:-1]
                else:
                    result = os.popen(data).read()
                    result = result[0:-1]
            except:
                result = "Command not found."
            command_reply.send_reply(sender, additional2, result)
            return True
        elif additional1 == "COMMAND-REPLY":
            if len(additional2) != 64:
                return
            cur.execute(
                'SELECT * FROM commands WHERE sender=? AND unique_id=? AND response=?',
                (sender, additional2, "None"))
            result = cur.fetchall()
            if len(result) == 0:
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
            else:
                return
            data = decrypt.decryptWithRSAKey(EncryptionKey, data)
            if data == False:
                return
            cur.execute(
                'UPDATE commands SET response=? WHERE sender=? AND unique_id=?',
                (data, sender, additional2))
            con.commit()
            OK.send_OK(sender)
            return True
        elif additional1 == "UPLOAD":
            cur.execute('SELECT * FROM connected_to_us WHERE user_connected=?',
                        (sender, ))
            result = cur.fetchall()
            if len(result) == 0:
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
            else:
                return
            try:
                details = additional2.split("|")
                filename = details[0]
                original_filename = filename
                filename_details = details[1]
            except:
                return
            filename = decrypt.decryptWithRSAKey(EncryptionKey, filename)
            original_filename = filename
            if filename == False:
                return
            filename_details = decrypt.decryptWithRSAKey(
                EncryptionKey, filename_details)
            if filename_details == False:
                return
            try:
                filename_details_details = filename_details.split("/")
                current_part = filename_details_details[0]
                total_parts = filename_details_details[1]
            except:
                return
            try:
                current_part = str(int(current_part))
                total_parts = str(int(total_parts))
            except:
                return
            if int(current_part) <= 0 or int(current_part) > int(
                    total_parts) or int(total_parts) <= 0:
                return
            data = decrypt.decryptWithRSAKey(EncryptionKey, data)
            if data == False:
                return
            return_data = requests.get(
                "http://127.0.0.1:10001/active_directory/" + sender)
            path = return_data.content
            if os.path.isfile(
                    os.path.join(path, "",
                                 filename + "_" + current_part)) == True:
                os.remove(os.path.join(path, "",
                                       filename + "_" + current_part))
            with open(os.path.join(path, "", filename + "_" + current_part),
                      "wb") as dest:
                dest.write(data)
            completed_file = True
            for i in range(1, int(total_parts) + 1):
                if os.path.isfile(
                        os.path.join(path, "",
                                     filename + "_" + str(i))) == False:
                    completed_file = False
                    break
            if completed_file == True:
                file_already_exists = False
                if os.path.isfile(os.path.join(path, "", filename)) == True:
                    file_already_exists = True
                if file_already_exists == True:
                    starting = 1
                    found = False
                    while found == False:
                        os.path.join(path, "",
                                     "(" + str(starting) + ")" + filename)
                        if os.path.isfile("(" + str(starting) + ")" +
                                          filename) == False:
                            filename = "(" + str(starting) + ")" + filename
                            found = True
                        starting += 1
                final_file = open(os.path.join(path, "", filename), "wb")
                for i in range(1, int(total_parts) + 1):
                    with open(
                            os.path.join(path, "",
                                         original_filename + "_" + str(i)),
                            'r') as current_file:
                        content = current_file.read()
                        final_file.write(content)
                        os.remove(
                            os.path.join(path, "",
                                         original_filename + "_" + str(i)))
                final_file.close()
            OK.send_OK(sender)
        elif additional1 == "DOWNLOAD":
            if len(additional2) != 64:
                return
            cur.execute('SELECT * FROM connected_to_us WHERE user_connected=?',
                        (sender, ))
            result = cur.fetchall()
            if len(result) == 0:
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
            else:
                return
            filename = decrypt.decryptWithRSAKey(EncryptionKey, data)
            if filename == False:
                return
            download_reply.send_file(sender, additional2, filename)
        elif additional1 == "DOWNLOAD-REPLY":
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
            else:
                return
            additional2_details = additional2.split("|")
            unique_id = additional2_details[0]
            cur.execute(
                'SELECT * FROM downloads WHERE sender=? AND unique_id=?',
                (sender, unique_id))
            result = cur.fetchall()
            if len(result) == 0:
                return
            filename = additional2_details[1]
            filename_details = additional2_details[2]
            filename = decrypt.decryptWithRSAKey(EncryptionKey, filename)
            if filename == False:
                return
            original_filename = filename
            cur.execute(
                'SELECT * FROM downloads WHERE sender=? AND unique_id=? AND filename=?',
                (sender, unique_id, filename))
            result = cur.fetchall()
            if len(result) == 0:
                return
            filename_details = decrypt.decryptWithRSAKey(
                EncryptionKey, filename_details)
            if filename_details == False:
                return
            try:
                filename_details_details = filename_details.split("/")
                current_part = filename_details_details[0]
                total_parts = filename_details_details[1]
            except:
                return
            try:
                current_part = str(int(current_part))
                total_parts = str(int(total_parts))
            except:
                return
            if int(current_part) <= 0 or int(current_part) > int(
                    total_parts) or int(total_parts) <= 0:
                return
            data = decrypt.decryptWithRSAKey(EncryptionKey, data)
            if data == False:
                return
            if os.path.isfile(
                    os.path.join("downloads", "",
                                 filename + "_" + current_part)) == True:
                os.remove(
                    os.path.join("downloads", "",
                                 filename + "_" + current_part))
            with open(
                    os.path.join("downloads", "",
                                 filename + "_" + current_part), "wb") as dest:
                dest.write(data)
            completed_file = True
            for i in range(1, int(total_parts) + 1):
                if os.path.isfile(
                        os.path.join("downloads", "",
                                     filename + "_" + str(i))) == False:
                    completed_file = False
                    break
            if completed_file == True:
                file_already_exists = False
                if os.path.isfile(os.path.join("downloads", "",
                                               filename)) == True:
                    file_already_exists = True
                if file_already_exists == True:
                    starting = 1
                    found = False
                    while found == False:
                        if os.path.isfile(
                                os.path.join(
                                    "downloads", "", "(" + str(starting) +
                                    ")" + filename)) == False:
                            filename = "(" + str(starting) + ")" + filename
                            found = True
                        starting += 1
                final_file = open(os.path.join("downloads", "", filename),
                                  "wb")
                for i in range(1, int(total_parts) + 1):
                    with open(
                            os.path.join("downloads", "",
                                         filename + "_" + str(i)),
                            'r') as current_file:
                        content = current_file.read()
                        final_file.write(content)
                        os.remove(
                            os.path.join("downloads", "",
                                         filename + "_" + str(i)))
                final_file.close()
                cur.execute(
                    'DELETE FROM downloads WHERE sender=? AND unique_id=? AND filename=?',
                    (sender, unique_id, filename))
                con.commit()
            OK.send_OK(sender)
        elif additional1 == "ENCRYPT":
            try:
                config = ConfigParser.RawConfigParser()
                config.read("treesshc")
                allowed_users_setting = config.get('Configuration',
                                                   'AllowedUsers')
                allowed_users = allowed_users_setting.split(",")
            except:
                allowed_users = []
            if sender not in allowed_users:
                return
            if additional2 != "None":
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 0:
                EncryptionKey = decrypt.decryptfromPubKey(data)
                if EncryptionKey == False:
                    return
                try:
                    testEncryptionKey = EncryptionKey.decode("hex")
                except:
                    return
                result = encrypt.encryptWithRSAKey(EncryptionKey, "test")
                if result == False:
                    return
                test_result = decrypt.decryptWithRSAKey(EncryptionKey, result)
                if test_result == False:
                    return
                if test_result != "test":
                    return
                time_created = str(int(time.time()))
                cur.execute(
                    'INSERT INTO users (identifier,EncryptionKey,NewEncryptionKey,time_generated,encryption) VALUES (?,?,?,?,?)',
                    (sender, EncryptionKey, EncryptionKey, time_created,
                     "INCOMING"))
                con.commit()
                result = encryption_reply.send_reply(sender, EncryptionKey)
                if result == True:
                    return True
            elif len(result) == 1:
                time_generated = result[0]["time_generated"]
                encryption_type = result[0]["encryption"]
                if encryption_type == "INCOMING":
                    if time.time() - float(time_generated) > 600:
                        EncryptionKey = decrypt.decryptfromPubKey(data)
                        if EncryptionKey == False:
                            return
                        try:
                            testEncryptionKey = EncryptionKey.decode("hex")
                        except:
                            return
                        Result = encrypt.encryptWithRSAKey(
                            EncryptionKey, "test")
                        if Result == False:
                            return
                        test_result = decrypt.decryptWithRSAKey(
                            EncryptionKey, Result)
                        if test_result == False:
                            return
                        if test_result != "test":
                            return
                        oldEncryptionKey = result[0]["EncryptionKey"]
                        time_created = str(int(time.time()))
                        cur.execute(
                            'UPDATE users SET EncryptionKey=?,NewEncryptionKey=?,time_generated=? WHERE identifier=?',
                            (EncryptionKey, oldEncryptionKey, time_created,
                             sender))
                        con.commit()
                        result = encryption_reply.send_reply(
                            sender, EncryptionKey)
                        if result == True:
                            return True
            else:
                return
        elif additional1 == "ENCRYPT-REPLY":
            if additional2 != "None":
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["NewEncryptionKey"]
                encryption = result[0]["encryption"]
            else:
                return
            if encryption != "OUTGOING":
                return
            data = decrypt.decryptWithRSAKey(EncryptionKey, data)
            if data == False:
                return
            if data == EncryptionKey:
                cur.execute(
                    'UPDATE users SET EncryptionKey=? WHERE identifier=?',
                    (data, sender))
                con.commit()
                OK.send_OK(sender)
        elif additional1 == "OK":
            requests.get("http://127.0.0.1:10001/received/" + sender + "/OK")
            return True
        else:
            return
    except:
        return
    finally:
        try:
            con.close()
        except:
            pass
예제 #6
0
def ask_memory(account, peer):
    try:
        original_account = account
        con = sql.connect("info.db", check_same_thread=False)
        con.row_factory = sql.Row
        cur = con.cursor()
        cur.execute('SELECT * FROM peers WHERE peer=?', (peer, ))
        result = cur.fetchall()
        if len(result) == 1:
            user = result[0]["identifier"]
            cur.execute('SELECT * FROM fakeAccounts WHERE identifier=?',
                        (user, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
            else:
                return
        else:
            return
        cur.execute('SELECT * FROM fake_account')
        accounts = cur.fetchall()
        Account = accounts[0]["fakeidentifier"]
        fake_private_key_hex = accounts[0]["fake_private_key_hex"]
        fake_public_key_hex = accounts[0]["fake_public_key_hex"]
        fake_Address = address.keyToAddr2(fake_public_key_hex, Account)
        timestamp = str(int(time.time()))
        signature = messages.sign_message(fake_private_key_hex,
                                          fake_Address + ":" + timestamp)
        fake_signature = signature.encode("hex")
        cur.execute('SELECT * FROM accounts WHERE identifier=?', (account, ))
        accounts = cur.fetchall()
        private_key_hex = accounts[0]["private_key_hex"]
        public_key_hex = accounts[0]["public_key_hex"]
        signature = messages.sign_message(private_key_hex,
                                          account + ":" + timestamp)
        signature = signature.encode("hex")
        account = encrypt.encryptWithRSAKey(EncryptionKey, account)
        public_key_hex = encrypt.encryptWithRSAKey(EncryptionKey,
                                                   public_key_hex)
        signature = encrypt.encryptWithRSAKey(EncryptionKey, signature)
        if account == False or public_key_hex == False or signature == False:
            return
        ip_result = whatis(peer)
        if ip_result == False:
            return
        if ip_result == "4":
            return_data = requests.get("http://" + peer +
                                       ":12995/memory/search/" + Account +
                                       "/" + fake_public_key_hex + "/" +
                                       timestamp + "/" + fake_signature + "/" +
                                       account + "/" + public_key_hex + "/" +
                                       signature)
        else:
            return_data = requests.get("http://[" + peer +
                                       "]:12995/memory/search/" + Account +
                                       "/" + fake_public_key_hex + "/" +
                                       timestamp + "/" + fake_signature + "/" +
                                       account + "/" + public_key_hex + "/" +
                                       signature)
        if return_data.content != "None" and return_data.status_code == 200:
            payload = decrypt.decryptWithRSAKey(EncryptionKey,
                                                return_data.content)
            if payload == False:
                return
            result = memory_new(original_account, payload)
    except:
        pass
    finally:
        try:
            con.close()
        except:
            pass
예제 #7
0
def create_payload(payment_type, product_hash, times, address_type, sender,
                   unique_id):
    try:
        additional1 = "ASK"
        additional2 = unique_id
        times = str(times)
        con = sql.connect("info.db")
        con.row_factory = sql.Row
        cur = con.cursor()
        cur.execute('SELECT * FROM accounts')
        accounts = cur.fetchall()
        account = accounts[0]["identifier"]
        private_key_hex = accounts[0]["private_key_hex"]
        public_key_hex = accounts[0]["public_key_hex"]
        cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
        result = cur.fetchall()
        if len(result) == 1:
            key = result[0]["EncryptionKey"]
        else:
            requests.post("http://127.0.0.1:10000/user/search", data=sender)
            return "User is offline"
        if payment_type == "PURCHASE":
            data = payment_type + "," + product_hash + "," + times + "," + address_type
        elif payment_type == "DONATE":
            data = payment_type + "," + address_type
        else:
            return False
        data = encrypt.encryptWithRSAKey(key, data)
        timestamp = str(int(time.time()))
        final = "TREEPAY" + ":" + account + ":" + sender + ":" + timestamp + ":" + additional1 + ":" + additional2 + ":" + public_key_hex + ":" + data
        tx_hash = sha256(final.rstrip()).hexdigest()
        signature = messages.sign_message(private_key_hex, tx_hash)
        payload = "TREEPAY" + "," + account + "," + sender + "," + timestamp + "," + additional1 + "," + additional2 + "," + public_key_hex + "," + data + "," + tx_hash + "," + signature.encode(
            "hex")
        cur.execute('SELECT * FROM requests WHERE unique_id=?', (unique_id, ))
        result = cur.fetchall()
        if len(result) == 0:
            time_generated = str(int(time.time()))
            if payment_type == "PURCHASE":
                cur.execute(
                    'INSERT INTO requests (type,identifier,ticker,time_generated,unique_id,transaction_id,transaction_id_times) VALUES (?,?,?,?,?,?,?)',
                    (payment_type, sender, address_type, time_generated,
                     unique_id, product_hash, times))
                con.commit()
                cur.execute(
                    'INSERT INTO messages (type,sender,times,refers_to,unique_id,time_generated) VALUES (?,?,?,?,?,?)',
                    (payment_type, sender, times, product_hash, unique_id,
                     time_generated))
                con.commit()
            else:
                cur.execute(
                    'INSERT INTO requests (type,identifier,ticker,time_generated,unique_id,transaction_id,transaction_id_times) VALUES (?,?,?,?,?,?,?)',
                    (payment_type, sender, address_type, time_generated,
                     unique_id, "None", "None"))
                con.commit()
                cur.execute(
                    'INSERT INTO messages (type,sender,times,refers_to,unique_id,time_generated) VALUES (?,?,?,?,?,?)',
                    (payment_type, sender, "None", "None", unique_id,
                     time_generated))
                con.commit()
        else:
            return False
        return payload
    except:
        return False
    finally:
        try:
            con.close()
        except:
            pass
예제 #8
0
def tree_pay(sender, receiver, timestamp, additional1, additional2,
             additional3, data, tx_hash, signature):
    try:
        con = sql.connect("info.db")
        con.row_factory = sql.Row
        cur = con.cursor()
    except:
        pass
    try:
        if additional1 == "ASK":
            if len(additional2) != 64:
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
                data = decrypt.decryptWithRSAKey(EncryptionKey, data)
                if data == False:
                    return
                data_details = data.split(",")
                payment_type = data_details[0]
                if payment_type == "PURCHASE":
                    product_hash = data_details[1]
                    cur.execute('SELECT * FROM items WHERE transaction_id=?',
                                (product_hash, ))
                    result = cur.fetchall()
                    if len(result) == 1:
                        Price = result[0]["price"]
                    else:
                        return
                    quantity = data_details[2]
                    try:
                        quantity = int(quantity)
                        if quantity < 1:
                            return
                    except:
                        return
                    total = float(Price) * quantity
                address_type = data_details[-1]
                if address_type == "BTC":
                    try:
                        address = getbtcaddress.get_address()
                    except:
                        OK.send_OK(sender)
                        return
                    if payment_type == "PURCHASE":
                        amount = price.get_price("BTC", total)
                        if amount != False:
                            reply.send_reply(sender, payment_type,
                                             address_type, address, amount,
                                             additional2, product_hash,
                                             quantity)
                    else:
                        product_hash = "Donation"
                        quantity = "Donation"
                        reply.send_reply(sender, payment_type, address_type,
                                         address, "0", additional2,
                                         product_hash, quantity)
                    OK.send_OK(sender)
                    return True
                elif address_type == "LTC":
                    try:
                        address = getltcaddress.get_address()
                    except:
                        OK.send_OK(sender)
                        return
                    if payment_type == "PURCHASE":
                        amount = price.get_price("LTC", total)
                        if amount != False:
                            reply.send_reply(sender, payment_type,
                                             address_type, address, amount,
                                             additional2, product_hash,
                                             quantity)
                    else:
                        product_hash = "Donation"
                        quantity = "Donation"
                        reply.send_reply(sender, payment_type, address_type,
                                         address, "0", additional2,
                                         product_hash, quantity)
                    OK.send_OK(sender)
                    return True
                else:
                    return
            else:
                return
        elif additional1 == "REPLY":
            if len(additional2) != 64:
                return
            cur.execute(
                'SELECT * FROM requests WHERE unique_id=? AND identifier=?',
                (additional2, sender))
            result = cur.fetchall()
            if len(result) != 1:
                return
            payment_type = result[0]["type"]
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["EncryptionKey"]
                data = decrypt.decryptWithRSAKey(EncryptionKey, data)
                if data == False:
                    return
                data_details = data.split(",")
                if len(data_details) != 3:
                    return
                address = data_details[0]
                if payment_type == "DONATE":
                    amount = data_details[1]
                    if amount != "0":
                        return
                else:
                    try:
                        amount = data_details[1]
                        amount = float(amount)
                        amount = "%.8f" % amount
                        if float(amount) <= 0:
                            return
                    except:
                        return
                transaction_on_success = data_details[2]
                if len(transaction_on_success) != 64:
                    return
                time_generated = str(int(time.time()))
                if payment_type != "DONATE":
                    cur.execute(
                        'UPDATE requests SET address=?,amount=?,time_generated=?,transaction_on_success=? WHERE unique_id=?',
                        (address, amount, time_generated,
                         transaction_on_success, additional2))
                    con.commit()
                else:
                    cur.execute(
                        'UPDATE requests SET address=?,amount=?,time_generated=?,transaction_on_success=? WHERE unique_id=?',
                        (address, "You can donate any amount of money!",
                         time_generated, transaction_on_success, additional2))
                    con.commit()
                cur.execute(
                    'UPDATE messages SET address=?,transaction_on_success=? WHERE unique_id=?',
                    (address, transaction_on_success, additional2))
                con.commit()
                OK.send_OK(sender)
                return True
            else:
                return
        elif additional1 == "ENCRYPT":
            if additional2 != "None":
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 0:
                EncryptionKey = decrypt.decryptfromPubKey(data)
                if EncryptionKey == False:
                    return
                try:
                    testEncryptionKey = EncryptionKey.decode("hex")
                except:
                    return
                result = encrypt.encryptWithRSAKey(EncryptionKey, "test")
                if result == False:
                    return
                test_result = decrypt.decryptWithRSAKey(EncryptionKey, result)
                if test_result == False:
                    return
                if test_result != "test":
                    return
                time_created = str(int(time.time()))
                cur.execute(
                    'INSERT INTO users (identifier,EncryptionKey,NewEncryptionKey,time_generated,encryption) VALUES (?,?,?,?,?)',
                    (sender, EncryptionKey, EncryptionKey, time_created,
                     "INCOMING"))
                con.commit()
                result = encryption_reply.send_reply(sender, EncryptionKey)
                if result == True:
                    return True
            elif len(result) == 1:
                time_generated = result[0]["time_generated"]
                encryption_type = result[0]["encryption"]
                if encryption_type == "INCOMING":
                    if time.time() - float(time_generated) > 600:
                        EncryptionKey = decrypt.decryptfromPubKey(data)
                        if EncryptionKey == False:
                            return
                        try:
                            testEncryptionKey = EncryptionKey.decode("hex")
                        except:
                            return
                        Result = encrypt.encryptWithRSAKey(
                            EncryptionKey, "test")
                        if Result == False:
                            return
                        test_result = decrypt.decryptWithRSAKey(
                            EncryptionKey, Result)
                        if test_result == False:
                            return
                        if test_result != "test":
                            return
                        oldEncryptionKey = result[0]["EncryptionKey"]
                        time_created = str(int(time.time()))
                        cur.execute(
                            'UPDATE users SET EncryptionKey=?,NewEncryptionKey=?,time_generated=? WHERE identifier=?',
                            (EncryptionKey, oldEncryptionKey, time_created,
                             sender))
                        con.commit()
                        result = encryption_reply.send_reply(
                            sender, EncryptionKey)
                        if result == True:
                            return True
            else:
                return
        elif additional1 == "ENCRYPT-REPLY":
            if additional2 != "None":
                return
            cur.execute('SELECT * FROM users WHERE identifier=?', (sender, ))
            result = cur.fetchall()
            if len(result) == 1:
                EncryptionKey = result[0]["NewEncryptionKey"]
                encryption = result[0]["encryption"]
            else:
                return
            if encryption != "OUTGOING":
                return
            data = decrypt.decryptWithRSAKey(EncryptionKey, data)
            if data == False:
                return
            if data == EncryptionKey:
                cur.execute(
                    'UPDATE users SET EncryptionKey=? WHERE identifier=?',
                    (data, sender))
                con.commit()
                OK.send_OK(sender)
        elif additional1 == "MESSAGE":
            if len(additional2) != 64:
                return
            cur.execute(
                'SELECT * FROM messages WHERE sender=? AND transaction_on_success=? AND message!=?',
                (sender, additional2, "None"))
            result = cur.fetchall()
            if len(result) == 1:
                data = decrypt.decryptWithRSAKey(EncryptionKey, data)
                if data == False:
                    return
                cur.execute(
                    'UPDATE messages SET message=? WHERE sender=? AND transaction_on_success=?',
                    (data, sender, additional2))
                con.commit()
                OK.send_OK(sender)
                return True
            else:
                return
        elif additional1 == "OK":
            requests.get("http://127.0.0.1:10000/received/" + sender + "/OK")
            return True
        else:
            return
    except:
        return
    finally:
        try:
            con.close()
        except:
            pass