【ERC20安全审计】——22、错误使用随机数漏洞

文章前言

智能合约是区块链技术的重要应用之一,它可以在不需要第三方信任机构的情况下实现自动化的合约执行,然而智能合约的安全性一直是人们关注的焦点之一,本文将介绍智能合约中常见的错误使用随机数的问题,探讨其产生的原因以及如何避免这些问题,希望本文能够对智能合约的安全性提高大家的警惕性

基本介绍

智能合约中的随机数是指在智能合约中生成的、具有随机性质的数字,在智能合约中的随机数常用于以下场景:

  • 抽奖:智能合约可以使用随机数来实现公平的抽奖功能
  • 游戏:智能合约可以使用随机数来模拟游戏中的随机事件,例如:掷骰子、抽卡等
  • 安全性:智能合约可以使用随机数来增加安全性,如在密码学中使用随机数来生成密钥或签名

智能合约中的随机数常常会使用一些区块参数来作为随机数,导致随机数出现可被预测或计算出的风险,常见的区块参数如下:

  • keccak256:哈希函数
  • block.timestamp:当前区块的时间戳
  • block.difficulty:当前区块的难度
  • block.number:当前区块的高度
  • msg.sender:当前交易的发送者地址
  • block.difficulty (uint): 当前区块难度
  • block.number (uint): 当前区块号
  • block.gaslimit (uint): 当前区块gas限额
  • block.coinbase (address): 挖出当前区块的矿工地址
漏洞演示

下面给出一个随机数生成代码示例:


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用JS来转账ERC20的USDT,需要使用相应的Web3库。以下是一个简单的示例代码: ```javascript const Web3 = require('web3'); const EthereumTx = require('ethereumjs-tx').Transaction; const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/<YOUR_INFURA_PROJECT_ID>')); // 以下是需要设置的参数 const privateKey = '<YOUR_PRIVATE_KEY>'; const toAddress = '<RECEIVER_ADDRESS>'; const amount = '<AMOUNT_IN_WEI>'; const contractAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7'; const decimals = 6; const gasPrice = '1'; const gasLimit = '100000'; // 获取USDT合约对象 const usdtContract = new web3.eth.Contract([ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "_owner", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "balance", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" } ], contractAddress); // 获取nonce值 web3.eth.getTransactionCount(web3.eth.accounts.privateKeyToAccount(privateKey).address, 'pending').then(nonce => { // 构造交易对象 const txParams = { from: web3.eth.accounts.privateKeyToAccount(privateKey).address, nonce: web3.utils.toHex(nonce), gasPrice: web3.utils.toHex(gasPrice * 1e9), gasLimit: web3.utils.toHex(gasLimit), to: contractAddress, value: '0x0', data: usdtContract.methods.transfer(toAddress, amount).encodeABI() }; // 签名交易 const tx = new EthereumTx(txParams, {chain: 'mainnet'}); tx.sign(Buffer.from(privateKey, 'hex')); // 发送交易 const serializedTx = tx.serialize(); web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')) .on('transactionHash', txHash => { console.log(`Transaction hash: ${txHash}`); }) .on('receipt', receipt => { console.log(`Transaction receipt: ${JSON.stringify(receipt, null, 2)}`); }) .on('error', error => { console.error(`Transaction error: ${error}`); }); }); ``` 在上述代码中,需要设置以下参数: - `privateKey`: 发送USDT的账户的私钥 - `toAddress`: 接收USDT的账户的地址 - `amount`: 发送的USDT数量,单位为wei - `contractAddress`: USDT合约地址 - `decimals`: USDT的小数位数 - `gasPrice`: 交易的gas价格,单位为Gwei - `gasLimit`: 交易的gas限制 该示例代码使用Infura提供的节点来与以太坊网络进行交互,也可以使用自己的节点。注意,发送USDT需要支付一定的手续费,需要确保发送账户有足够的ETH用于支付手续费。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FLy_鹏程万里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值