当前位置:网站首页>智能合约安全——私有数据访问
智能合约安全——私有数据访问
2022-07-30 14:15:00 【fingernft】
这次我们将了解如何访问合约中的私有数据(private 数据)。目标合约话不多说,直接上代码

这次我们的目标合约是部署在 Ropsten 上的一个合约。
合约地址:0x3505a02BCDFbb225988161a95528bfDb279faD6b链接:https://ropsten.etherscan.io/address/0x3505a02BCDFbb225988161a95528bfDb279faD6b#code
漏洞分析由上面的合约代码我们可以看到,Vault 合约将用户的用户名和密码这样的敏感数据记录在了合约中,我们知道合约中修饰变量的关键字仅限制其调用范围,这也就间接证明了合约中的数据均是公开的,可任意读取的,将敏感数据记录在合约中是不安全的。
读取数据
首先,让我们来学习一下solidity的 storage存储方式:1)storage 中的数据被永久存储。其以键值对的形式存储在 slot 插槽中。2)storage在插槽中数据从右向左排列,空间不足时,打包当前插槽,开启下一个插槽存储数据;存储定长数组(长度固定)时,数组中每一个数据占据一个插槽。3)存储变长数组(长度随元素的数量而改变)比较特殊,在遇到变长数组时,会先启用一个新的插槽 slotA 用来存储数组的长度,其数据存储在另外的编号为 slotV 的插槽中。slotA 表示变长数组声明的位置,同时也存储着变长数组的长度length:length = sload(slotA)用slotV表示变长数组数据存储的位置(即key),index 表示 value 对应的索引下标:slotV = keccak256(slotA) + index用 value 表示变长数组某个数据的值:value = sload(slotV)下面我们就带大家来读取这个合约中的数据。首先我们先看 slot0 中的数据:
由合约中可以看到 slot0 中只存储了一个 uint 类型的数据,我们读取出来看一下:我这里使用 Web3.py 取得数据,首先写好程序
运行结果:

“7b”是16进制数,转换成10进制数就是123。这里我们就成功的去到了合约中的第一个插槽 slot0 中存储的 uint 类型的变量 count=123 ,下面我们继续:
slot1 中存储三个变量:u16, isTrue, owner
运行结果:
从右往左依次为owner = f36467c4e023c355026066b8dc51456e7b791d99isTrue = 01 = trueu16 = 1f = 31
slot2 中就存储着私有变量 password 我们读取看看
运行结果:

slot 3, 4, 5 中存储着定长数组中的三个元素
运行结果:

slot6 中存储着变长数组的长度
运行结果:

返回的结果显示变长数组的长度为3。我们从合约代码中可以看到用户的 id 和 password 是由键值对的形式存储的,下面我们来读取两个用户的 id 和 password:user1
运行结果:

user2
运行结果:
这样我们就成功的将合约中的所有数据读取完成。由此可见,合约中的私有数据也是可以读取的。总结大家可以看到,合约中的私有数据也是可以读取的,所以一定不要将任何敏感数据存放在合约中哦。
如果想了解更多的智能合约和区块链知识,欢迎到区块链交流社区CHAINPIP社区,一起交流学习~社区地址:https://www.chainpip.com/
边栏推荐
- Conversion between pytorch and keras (the code takes LeNet-5 as an example)
- cookie模拟登录「建议收藏」
- Get the Google Advertising ID as a unique identifier
- 我为何从开发人员转做测试,3年软件测试工程师,带你聊聊这其中的秘辛
- 3年软件测试经验面试要求月薪22K,明显感觉他背了很多面试题...
- Eight years of testing experience, why was the leader criticized: the test documents you wrote are not as good as those of fresh graduates
- Understand the Chisel language. 29. Chisel advanced communication state machine (1) - communication state machine: take the flash as an example
- Understand Chisel language. 28. Chisel advanced finite state machine (2) - Mealy state machine and comparison with Moore state machine
- 华为再发「天才少年」召集令!曾放弃360万年薪的他也来首秀
- Redis6.0 source code learning (5) ziplist
猜你喜欢

LoRaWAN网关源码分析(V1.0.2)

Flask Framework - Sijax

关于容器的小案例

3 years of software testing experience, the interview requires a monthly salary of 22K, obviously he has memorized a lot of interview questions...

Eight years of testing experience, why was the leader criticized: the test documents you wrote are not as good as those of fresh graduates

元宇宙邮局AI航天主题系列数字藏品 将于7月30日10:00点上线“元邮数藏”

自动化办公|办公软件和亿图脑图MindMaster快捷键

43.【list的简单属性】

时间序列的数据分析(四):STL分解

Flask框架——Sijax
随机推荐
Understand the Chisel language. 29. Chisel advanced communication state machine (1) - communication state machine: take the flash as an example
NFTScan 与 PANews 联合发布多链 NFT 数据分析报告
How awesome is the "12306" architecture?
数字信号处理课程实验报告(数字信号处理需要什么基础)
关于String的一些思考
Recommended open source tools: MegPeak, a high-performance computing tool
CS内网横向移动 模拟渗透实操 超详细
双碳目标下:农田温室气体排放模拟
为什么做软件测试一定要学自动化?谈谈我眼中自动化测试的价值
00 testers of seasoning after nearly a year, whether to change careers or to learn the software testing students summarized the following heart advice
3 years of software testing experience, the interview requires a monthly salary of 22K, obviously he has memorized a lot of interview questions...
自动化测试之数据驱动DDT详细篇
获取Google Advertising ID作为唯一识别码
Hello, World
kubernate部署服务
开源工具推荐:高性能计算辅助工具MegPeak
【Vue.js 3.0源码】KeepAlive 组件:如何让组件在内存中缓存和调度?
CF603E Pastoral Oddities
Get the Google Advertising ID as a unique identifier
ECCV 2022 | 通往数据高效的Transformer目标检测器