|
@@ -0,0 +1,251 @@
|
|
1
|
+\documentclass{beamer}
|
|
2
|
+%\documentclass[aspectratio=169]{beamer}
|
|
3
|
+
|
|
4
|
+% You have to install the theme first!
|
|
5
|
+
|
|
6
|
+% corporate design of TU Wien
|
|
7
|
+\usetheme[font=helvetica]{tuw}
|
|
8
|
+% background "TU main building" on title page
|
|
9
|
+%\usetheme[tuw_background]{tuw}
|
|
10
|
+% individual background on title page
|
|
11
|
+%\usetheme[tuw_image=TU_Background]{tuw}
|
|
12
|
+% white logo if you have a dark background image
|
|
13
|
+%\usetheme[tuw_image=TU_Background,tuw_whitelogo]{tuw}
|
|
14
|
+% sidebar (not in TU Wien CD! but nice for long presentations)
|
|
15
|
+% width of the sidebar can be changed with option: "width=2cm"
|
|
16
|
+%\usetheme[outer=sidebar]{tuw}
|
|
17
|
+% move frametitle up (beside logo)
|
|
18
|
+%\usetheme[tuw_frametitletotop]{tuw}
|
|
19
|
+
|
|
20
|
+% if you use german umlaute use T1 encoding:
|
|
21
|
+%\usepackage[T1]{fontenc}
|
|
22
|
+% default Latex fonts are not T1 supported -> bitmaps used, this is not nice on
|
|
23
|
+% screen; you can use the lmodern package instead
|
|
24
|
+%\usepackage{lmodern}
|
|
25
|
+\usepackage[utf8]{inputenc}
|
|
26
|
+\usepackage{listings}
|
|
27
|
+\usepackage{booktabs}
|
|
28
|
+\usepackage{url}
|
|
29
|
+\usepackage{xcolor}
|
|
30
|
+
|
|
31
|
+\definecolor{codegreen}{rgb}{0,0.6,0}
|
|
32
|
+\definecolor{codegray}{rgb}{0.5,0.5,0.5}
|
|
33
|
+\definecolor{codepurple}{rgb}{0.58,0,0.82}
|
|
34
|
+\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
|
|
35
|
+\definecolor{verylightgray}{rgb}{.97,.97,.97}
|
|
36
|
+
|
|
37
|
+\lstdefinelanguage{Solidity}{
|
|
38
|
+ keywords=[1]{anonymous, assembly, assert, balance, break, call, callcode, case, catch, class, constant, continue, constructor, contract, debugger, default, delegatecall, delete, do, else, emit, event, experimental, export, external, false, finally, for, function, gas, if, implements, import, in, indexed, instanceof, interface, internal, is, length, library, log0, log1, log2, log3, log4, memory, modifier, new, payable, pragma, private, protected, public, pure, push, require, return, returns, revert, selfdestruct, send, solidity, storage, struct, suicide, super, switch, then, this, throw, transfer, true, try, typeof, using, value, view, while, with, addmod, ecrecover, keccak256, mulmod, ripemd160, sha256, sha3}, % generic keywords including crypto operations
|
|
39
|
+ keywordstyle=[1]\color{blue}\bfseries,
|
|
40
|
+ keywords=[2]{address, bool, byte, bytes, bytes1, bytes2, bytes3, bytes4, bytes5, bytes6, bytes7, bytes8, bytes9, bytes10, bytes11, bytes12, bytes13, bytes14, bytes15, bytes16, bytes17, bytes18, bytes19, bytes20, bytes21, bytes22, bytes23, bytes24, bytes25, bytes26, bytes27, bytes28, bytes29, bytes30, bytes31, bytes32, enum, int, int8, int16, int24, int32, int40, int48, int56, int64, int72, int80, int88, int96, int104, int112, int120, int128, int136, int144, int152, int160, int168, int176, int184, int192, int200, int208, int216, int224, int232, int240, int248, int256, mapping, string, uint, uint8, uint16, uint24, uint32, uint40, uint48, uint56, uint64, uint72, uint80, uint88, uint96, uint104, uint112, uint120, uint128, uint136, uint144, uint152, uint160, uint168, uint176, uint184, uint192, uint200, uint208, uint216, uint224, uint232, uint240, uint248, uint256, var, void, ether, finney, szabo, wei, days, hours, minutes, seconds, weeks, years}, % types; money and time units
|
|
41
|
+ keywordstyle=[2]\color{teal}\bfseries,
|
|
42
|
+ keywords=[3]{block, blockhash, coinbase, difficulty, gaslimit, number, timestamp, msg, data, gas, sender, sig, value, now, tx, gasprice, origin}, % environment variables
|
|
43
|
+ keywordstyle=[3]\color{violet}\bfseries,
|
|
44
|
+ identifierstyle=\color{black},
|
|
45
|
+ sensitive=true,
|
|
46
|
+ comment=[l]{//},
|
|
47
|
+ morecomment=[s]{/*}{*/},
|
|
48
|
+ commentstyle=\color{gray}\ttfamily,
|
|
49
|
+ stringstyle=\color{red}\ttfamily,
|
|
50
|
+ morestring=[b]',
|
|
51
|
+ morestring=[b]"
|
|
52
|
+}
|
|
53
|
+
|
|
54
|
+\lstset{
|
|
55
|
+ language=Solidity,
|
|
56
|
+ backgroundcolor=\color{verylightgray},
|
|
57
|
+ extendedchars=true,
|
|
58
|
+ basicstyle=\footnotesize\ttfamily,
|
|
59
|
+ showstringspaces=false,
|
|
60
|
+ showspaces=false,
|
|
61
|
+ numbers=left,
|
|
62
|
+ numberstyle=\footnotesize,
|
|
63
|
+ numbersep=9pt,
|
|
64
|
+ tabsize=2,
|
|
65
|
+ breaklines=true,
|
|
66
|
+ showtabs=false,
|
|
67
|
+ captionpos=b
|
|
68
|
+}
|
|
69
|
+
|
|
70
|
+%%% title page settings
|
|
71
|
+\title[SWC-124]{%
|
|
72
|
+ SWC-124: Write to Arbitrary Storage Location
|
|
73
|
+}
|
|
74
|
+\subtitle{192.127 Seminar in Software Engineering (Smart Contracts)}
|
|
75
|
+\author{Ivaylo Ivanov \& Peter Millauer}
|
|
76
|
+\date{\today}
|
|
77
|
+
|
|
78
|
+%%% slides start here
|
|
79
|
+\begin{document}
|
|
80
|
+
|
|
81
|
+% first frame must include the title page!
|
|
82
|
+\begin{frame}
|
|
83
|
+ \titlepage
|
|
84
|
+\end{frame}
|
|
85
|
+
|
|
86
|
+% table of contents if you have a long presentation (uses 'part' and 'section'
|
|
87
|
+% elements)
|
|
88
|
+\begin{frame}{Outline}
|
|
89
|
+ \tableofcontents
|
|
90
|
+\end{frame}
|
|
91
|
+
|
|
92
|
+\section{Introduction}
|
|
93
|
+
|
|
94
|
+\subsection[SWC-124: Weakness Outline]{SWC-124: Weakness Outline}
|
|
95
|
+\begin{frame}[fragile]
|
|
96
|
+ \frametitle{SWC-124: Weakness Outline}
|
|
97
|
+ SWC-124 is a weakness that allows attackers to write to places in the storage where they should not be able to.
|
|
98
|
+ It can be used to gain unauthorized access, overwrite data, steal funds etc.
|
|
99
|
+\end{frame}
|
|
100
|
+
|
|
101
|
+\begin{frame}[fragile]
|
|
102
|
+ \frametitle{SWC-124: Weakness Outline}
|
|
103
|
+ We generally differentiate three types of SWC-124:
|
|
104
|
+\begin{itemize}
|
|
105
|
+ \item unchecked array write
|
|
106
|
+ \item incorrect array length check
|
|
107
|
+ \item unchecked assembly code
|
|
108
|
+\end{itemize}
|
|
109
|
+
|
|
110
|
+ Examples follow, use in production at your own risk ;)
|
|
111
|
+\end{frame}
|
|
112
|
+
|
|
113
|
+\subsection[Examples]{Examples}
|
|
114
|
+\begin{frame}[fragile]
|
|
115
|
+ \frametitle{Unchecked Array Write}
|
|
116
|
+ \begin{lstlisting}[language=Solidity]
|
|
117
|
+pragma solidity 0.4.25;
|
|
118
|
+
|
|
119
|
+contract MyContract {
|
|
120
|
+ uint[] private arr;
|
|
121
|
+
|
|
122
|
+ constructor() public {
|
|
123
|
+ arr = new uint[](0);
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ function write(unit index, uint value) {
|
|
127
|
+ arr[index] = value;
|
|
128
|
+ }
|
|
129
|
+}
|
|
130
|
+ \end{lstlisting}
|
|
131
|
+\end{frame}
|
|
132
|
+\begin{frame}[fragile]
|
|
133
|
+ \frametitle{Incorrect Array Length Check}
|
|
134
|
+ \begin{lstlisting}[language=Solidity, basicstyle=\tiny\ttfamily, numberstyle=\tiny]
|
|
135
|
+pragma solidity 0.4.25;
|
|
136
|
+
|
|
137
|
+contract MyContract {
|
|
138
|
+ uint[] private arr;
|
|
139
|
+
|
|
140
|
+ constructor() public {
|
|
141
|
+ arr = new uint[](0);
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ function push(value) {
|
|
145
|
+ arr[arr.length] = value;
|
|
146
|
+ arr.length++;
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ function pop() {
|
|
150
|
+ require(arr.length >= 0);
|
|
151
|
+ arr.length--;
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ function update(unit index, uint value) {
|
|
155
|
+ require(index < arr.length);
|
|
156
|
+ arr[index] = value;
|
|
157
|
+ }
|
|
158
|
+}
|
|
159
|
+ \end{lstlisting}
|
|
160
|
+\end{frame}
|
|
161
|
+\begin{frame}[fragile]
|
|
162
|
+ \frametitle{Unchecked Assembly}
|
|
163
|
+ \begin{lstlisting}[language=Solidity, basicstyle=\tiny\ttfamily, numberstyle=\tiny, breaklines=true]
|
|
164
|
+pragma solidity 0.4.25;
|
|
165
|
+
|
|
166
|
+contract MyContract {
|
|
167
|
+ address private owner;
|
|
168
|
+ mapping(address => bool) public managers;
|
|
169
|
+
|
|
170
|
+ constructor() public {
|
|
171
|
+ owner = msg.sender;
|
|
172
|
+ setNextUserRole(msg.sender);
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ function setNextManager(address next) internal {
|
|
176
|
+ uint256 slot;
|
|
177
|
+ assembly {
|
|
178
|
+ slot := managers.slot
|
|
179
|
+ sstore(slot, next)
|
|
180
|
+ }
|
|
181
|
+ bytes32 location = keccak256(abi.encode(160, uint256(slot)));
|
|
182
|
+ assembly {
|
|
183
|
+ sstore(location, true)
|
|
184
|
+ }
|
|
185
|
+ }
|
|
186
|
+
|
|
187
|
+ function registerUser(address user) {
|
|
188
|
+ require(msg.sender == owner);
|
|
189
|
+ setNextManager(user);
|
|
190
|
+ }
|
|
191
|
+}
|
|
192
|
+ \end{lstlisting}
|
|
193
|
+\end{frame}
|
|
194
|
+
|
|
195
|
+\section{Detecting and Exploiting}
|
|
196
|
+\subsection[Detecting SWC-124]{Detecting SWC-124}
|
|
197
|
+\begin{frame}[fragile]
|
|
198
|
+ \frametitle{SWC-124: Detection Heuristics 1}
|
|
199
|
+ Any contract without dynamic arrays (or mappings with integer keys) or raw assembly including a SSTORE instruction can immediately be considered non-vulnerable.
|
|
200
|
+\end{frame}
|
|
201
|
+
|
|
202
|
+\begin{frame}[fragile]
|
|
203
|
+ \frametitle{SWC-124: Detection Heuristics 2}
|
|
204
|
+ If heuristic 1 does not hold, we can then apply a second heuristic: checking the solidity compiler version, specified at the top of the contract. Solidity version 0.8.0+ introduced integer under- and overflow protection, which are enabled per default and require extra work to be disabled.
|
|
205
|
+\end{frame}
|
|
206
|
+
|
|
207
|
+\begin{frame}[fragile]
|
|
208
|
+ \frametitle{SWC-124: Detection Heuristics 2.1}
|
|
209
|
+ If the version of the contract is higher than 0.8.0, we examine whether unchecked arithmetic has been used for modifying the arrays. If this is not the case, which it is not most of the time, we can then determine that the contract is non-vulnerable. Applying this heuristic, we found a contract that could have been vulnerable had it been compiled with a lower solidity version.
|
|
210
|
+
|
|
211
|
+ \begin{block}{Note on assembly}
|
|
212
|
+ Due to the nature of the examples given, we could not find reliable heuristics for unchecked assembly.
|
|
213
|
+ \end{block}
|
|
214
|
+\end{frame}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+\begin{frame}[fragile]
|
|
218
|
+ \frametitle{SWC-124: Detection Tools}
|
|
219
|
+ \begin{itemize}
|
|
220
|
+ \item existing static analysis tools were useless - most of them had no support for SWC-124
|
|
221
|
+ \item \texttt{solc-select} - for changing solidity compiler versions
|
|
222
|
+ \item \texttt{slither} - for printing contract variable layout
|
|
223
|
+ \end{itemize}
|
|
224
|
+\end{frame}
|
|
225
|
+
|
|
226
|
+\subsection[Exploiting SWC-124]{Exploiting SWC-124}
|
|
227
|
+
|
|
228
|
+% TODO: Exploitation is trivial, should we just go back and explain or should we go into memory layout? Can we handle it in time?
|
|
229
|
+
|
|
230
|
+\begin{frame}[fragile]
|
|
231
|
+ \frametitle{Vulnerable Examples}
|
|
232
|
+ Using the previously mentioned heuristics, we could not find a vulnerable contract from the dataset.
|
|
233
|
+\end{frame}
|
|
234
|
+
|
|
235
|
+\section{Future Work}
|
|
236
|
+\begin{frame}[fragile]
|
|
237
|
+ \frametitle{Future Work}
|
|
238
|
+\begin{itemize}
|
|
239
|
+ \item add heuristics to static analysis tool like \texttt{slither} or \texttt{mythril}
|
|
240
|
+ \item develop additional vulnerable and non-vulnerable contracts and test against heuristics
|
|
241
|
+ \item train a model against the resulting dataset
|
|
242
|
+ \item fine-tune heuristics
|
|
243
|
+\end{itemize}
|
|
244
|
+\end{frame}
|
|
245
|
+\section{Conclusion}
|
|
246
|
+\begin{frame}[fragile]
|
|
247
|
+ Questions?
|
|
248
|
+\end{frame}
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+\end{document}
|