def ask_memory(account,peer): try: 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.encryptAES(EncryptionKey,account) public_key_hex = encrypt.encryptAES(EncryptionKey,public_key_hex) signature = encrypt.encryptAES(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.decryptAES(EncryptionKey,return_data.content) if payload == False: return result = memory_new(user,payload) if result == "Added": print time.strftime("%d/%m/%Y %H:%M:%S", time.gmtime()) + " ["+account+"] <- received data from " + peer except: pass finally: try: con.close() except: pass
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
def create_payload(sender): try: additional1 = "OK" additional2 = "None" 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"] data = "None" 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") return payload except: return False finally: try: con.close() except: pass
def create_payload(account): try: additional2 = "TREEPAY".encode("hex") con = sql.connect("info.db") con.row_factory = sql.Row cur = con.cursor() 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"] cur.execute('SELECT * FROM keys WHERE identifier=? ORDER BY time_generated DESC LIMIT 1', (account,)) keys = cur.fetchall() public_key = keys[-1]["public_key"] data = public_key data = data.encode("hex") timestamp = str(int(time.time())) final = "OSP" + ":" + account + ":" + account + ":" + timestamp + ":" + "None" + ":" + additional2 + ":" + public_key_hex + ":" + data tx_hash = sha256(final.rstrip()).hexdigest() signature = messages.sign_message(private_key_hex, tx_hash) payload = "OSP" + "," + account + "," + account + "," + timestamp + "," + "None" + "," + additional2 + "," + public_key_hex + "," + data + "," + tx_hash + "," + signature.encode("hex") return payload except: return False finally: try: con.close() except: pass
def create_payload(sender,EncryptionKey): try: additional1 = "ENCRYPT" additional2 = "None" 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"] return_data = requests.get("http://127.0.0.1:10001/user/"+sender) if return_data.content != "None": pubKey = return_data.content.decode("hex") else: return False data = encrypt.encryptwithPubKey(pubKey, EncryptionKey) 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
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
def get_encryption(Identifier, peer, user, privkey, pubkey, ip): try: if ip == "4": return_data = requests.get("http://" + peer + ":12995/encrypt") else: return_data = requests.get("http://[" + peer + "]:12995/encrypt") userPublicKey = return_data.content ourEncryptionKey = (os.urandom(32)).encode("hex") payload = encrypt.encryptwithPubKey(userPublicKey, ourEncryptionKey) if payload == False: return timestamp = str(int(time.time())) message = user + ":" + timestamp signature = messages.sign_message(privkey, message) signature = signature.encode("hex") if ip == "4": encryption_post = requests.post( "http://" + peer + ":12995/encrypt/post/" + user + "/" + pubkey + "/" + timestamp + "/" + signature, data=payload) else: encryption_post = requests.post( "http://[" + peer + "]:12995/encrypt/post/" + user + "/" + pubkey + "/" + timestamp + "/" + signature, data=payload) response = encryption_post.content if response != "OK": return try: con = sql.connect("info.db", check_same_thread=False) con.row_factory = sql.Row cur = con.cursor() cur.execute('SELECT * FROM fakeAccounts WHERE identifier=?', (Identifier, )) result = cur.fetchall() if len(result) == 0: cur.execute( 'INSERT INTO fakeAccounts (identifier,EncryptionKey,time_generated) VALUES (?,?,?)', (Identifier, ourEncryptionKey, timestamp)) con.commit() else: cur.execute( 'UPDATE fakeAccounts SET EncryptionKey=?, time_generated=? WHERE identifier=?', (ourEncryptionKey, timestamp, Identifier)) con.commit() except: return finally: try: con.close() except: pass except: pass
def importprivkey(): private_key_hex = raw_input("Enter private key in hex format: ") public_key_hex, Address = address.details_from_private(private_key_hex) signature = messages.sign_message(private_key_hex, "test") prove_ownership = messages.verify_message(public_key_hex, signature.encode("hex"), "test") if prove_ownership == True: try: cur.execute( 'INSERT INTO accounts (identifier,private_key_hex,public_key_hex) VALUES (?,?,?)', (Address, private_key_hex, public_key_hex)) con.commit() print "[+] Account " + Address + " added" except Exception as e: print e con.close() else: print "This private key does not prove ownership of " + Address
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
def nodeinfo(): try: con = sql.connect("info.db", check_same_thread=False) con.row_factory = sql.Row cur = con.cursor() cur.execute('SELECT * FROM fake_account') result = cur.fetchall() identifier = result[0]["fakeidentifier"] public_key = result[0]["fake_public_key_hex"] private_key = result[0]["fake_private_key_hex"] timestamp = str(int(time.time())) signature = messages.sign_message(private_key,identifier+":"+timestamp) return str(identifier + "," + public_key + "," + signature.encode("hex") + "," + timestamp) except Exception as e: print e return "Something went wrong." finally: try: con.close() except: pass
def importfake(): private_key_hex = raw_input("Enter private key in hex format: ") public_key_hex, Address = address.details_from_private_fake( private_key_hex) signature = messages.sign_message(private_key_hex, "test") prove_ownership = messages.verify_message(public_key_hex, signature.encode("hex"), "test") if prove_ownership == True: cur.execute('SELECT * FROM fake_account') result = cur.fetchall() if len(result) == 0: try: cur.execute( 'INSERT INTO fake_account (fakeidentifier,fake_private_key_hex,fake_public_key_hex) VALUES (?,?,?)', (Address, private_key_hex, public_key_hex)) con.commit() print "[+] Account " + Address + " added" except Exception as e: print e else: print "Another fake account already exists. Exiting.." con.close() else: print "This private key does not prove ownership of " + Address
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
print " [+] New account " + Accountaddress + " created" GetFromSettings.update({Accountaddress:"ALL"}) PostToSettings.update({Accountaddress:"ALL"}) accounts.append(Accountaddress) else: for Account in Accounts: try: account = Account["identifier"] private_key_hex = Account["private_key_hex"] public_key_hex = Account["public_key_hex"] Accountaddress = address.keyToAddr(public_key_hex,account) if Accountaddress != account: cur.execute('UPDATE accounts SET identifier=? WHERE identifier=?', (Accountaddress,account)) con.commit() signature = messages.sign_message(private_key_hex,"test") if signature == False: print " [-] There was a problem with signature. Exiting.." sys.exit(1) prove_ownership = messages.verify_message(public_key_hex, signature.encode("hex"), "test") if prove_ownership == False: print " [-] The private key " + private_key_hex + " does not prove ownership of " + account cur.execute('DELETE FROM accounts WHERE identifier=?', (account,)) con.commit() else: print " [+] Account successfully loaded: " + account accounts.append(account) except: print " [-] Error with private key. Maybe wrong format (WIF)? Exiting.." sys.exit(1)
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