您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. \documentclass [10pt]{article}
  2. \usepackage{latexsym}
  3. \usepackage{amssymb}
  4. \usepackage{epsfig}
  5. \usepackage{fullpage}
  6. \usepackage{enumerate}
  7. \usepackage{enumitem}
  8. \usepackage{xspace}
  9. \usepackage{todonotes}
  10. \usepackage{listings}
  11. \usepackage{url}
  12. \usepackage[ruled,linesnumbered]{algorithm2e} % Enables the writing of pseudo code.
  13. \usepackage{float}% http://ctan.org/pkg/float
  14. \newcommand{\true}{true}
  15. \newcommand{\false}{false}
  16. \pagestyle{plain}
  17. \bibliographystyle{plain}
  18. \title{192.127 Seminar in Software Engineering (Smart Contracts) \\
  19. SWC-124: Write to Arbitrary Storage Location \\
  20. Report Exploits }
  21. \author{Exercises}
  22. \date{WT 2023/24}
  23. \author{\textbf{Ivanov, Ivaylo (11777707) \& Millauer, Peter (01350868)}}
  24. \newtheorem{theorem}{Theorem}
  25. \newtheorem{lemma}[theorem]{Lemma}
  26. \newtheorem{corollary}[theorem]{Corollary}
  27. \newtheorem{proposition}[theorem]{Proposition}
  28. \newtheorem{conjecture}[theorem]{Conjecture}
  29. \newtheorem{definition}[theorem]{Definition}
  30. \newtheorem{example}[theorem]{Example}
  31. \newtheorem{remark}[theorem]{Remark}
  32. \newtheorem{exercise}[theorem]{Exercise}
  33. \renewcommand{\labelenumi}{(\alph{enumi})}
  34. \usepackage{xcolor}
  35. \definecolor{codegreen}{rgb}{0,0.6,0}
  36. \definecolor{codegray}{rgb}{0.5,0.5,0.5}
  37. \definecolor{codepurple}{rgb}{0.58,0,0.82}
  38. \definecolor{backcolour}{rgb}{0.95,0.95,0.92}
  39. \definecolor{verylightgray}{rgb}{.97,.97,.97}
  40. \lstdefinelanguage{Solidity}{
  41. 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
  42. keywordstyle=[1]\color{blue}\bfseries,
  43. 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
  44. keywordstyle=[2]\color{teal}\bfseries,
  45. keywords=[3]{block, blockhash, coinbase, difficulty, gaslimit, number, timestamp, msg, data, gas, sender, sig, value, now, tx, gasprice, origin}, % environment variables
  46. keywordstyle=[3]\color{violet}\bfseries,
  47. identifierstyle=\color{black},
  48. sensitive=true,
  49. comment=[l]{//},
  50. morecomment=[s]{/*}{*/},
  51. commentstyle=\color{gray}\ttfamily,
  52. stringstyle=\color{red}\ttfamily,
  53. morestring=[b]',
  54. morestring=[b]"
  55. }
  56. \lstset{
  57. language=Solidity,
  58. backgroundcolor=\color{verylightgray},
  59. extendedchars=true,
  60. basicstyle=\footnotesize\ttfamily,
  61. showstringspaces=false,
  62. showspaces=false,
  63. numbers=left,
  64. numberstyle=\footnotesize,
  65. numbersep=9pt,
  66. tabsize=2,
  67. breaklines=true,
  68. showtabs=false,
  69. captionpos=b
  70. }
  71. \lstdefinestyle{mystyle}{
  72. backgroundcolor=\color{backcolour},
  73. commentstyle=\color{codegreen},
  74. keywordstyle=\color{magenta},
  75. numberstyle=\tiny\color{codegray},
  76. stringstyle=\color{codepurple},
  77. basicstyle=\ttfamily\footnotesize,
  78. breakatwhitespace=false,
  79. breaklines=true,
  80. captionpos=b,
  81. keepspaces=true,
  82. numbers=left,
  83. numbersep=5pt,
  84. showspaces=false,
  85. showstringspaces=false,
  86. showtabs=false,
  87. tabsize=2
  88. }
  89. \begin{document}
  90. \maketitle
  91. %\begin{abstract}
  92. % This paper outlines different forms of the common smart contract weakness with the SWC number 124, commonly referred to as "Write to Arbitrary Storage Location". While this paper focuses on applications within the context of Ethereum's EVM and higher-level language Solidity, we will also briefly touch on other research that deals with the Hyperledger Fabric environment. We will begin with a gentle introduction to the Solidity storage layout design that allows this weakness to occur, followed by common forms of exploit, alongside their associated consequences. Finally, we will outline the code characteristics that are detectable by automated tools as well as an exploit sketch.
  93. %\end{abstract}
  94. \section{Introduction}
  95. \subsection{Paper introduction}
  96. The aim of this report is to present an analysis of the SWC-124 weakness and its detection. First, we will decribe the setup we did for performing the analysis.
  97. Then, in section~\ref{sec:exploit-creation} we will give a brief overview of the weakness and present heuristics for detecting it. Afterwards, in section~\ref{sec:results} we will present the results
  98. of the security analysis of the assigned contracts\footnote{\url{https://git.logic.at/ethereum/vulnerabilities_23w/-/tree/swc-124?ref_type=heads}}
  99. and finally, in section~\ref{sec:discussion} we will wrap up, give an overview about what we learned and provide future research questions.
  100. \subsection{Setup}
  101. We attempted to implement an automatic weakness detection pipeline by using a multitude of tools. The software used includes:
  102. \begin{itemize}
  103. \item \textbf{solc}: Using \textbf{solc-select} we select the correct solc version and compile the associated *.sol file. This way we gather compiler hints and warnings.
  104. \item \textbf{Mythril}: A tool used for analysis of EVM bytecode, one of the de-facto standards for such work. It is unfortunately not able to detect SWC-124, but there is an existing feature request\footnote{\url{https://github.com/Consensys/mythril/issues/861}} for support available.
  105. \item \textbf{teEther}: A tool we found in the last research step of this seminar, teether is a dynamic analysis tool for smart contracts. It is unfortunately hardly documented and has been unmaintained for several years. We were unable to generate usable results with it.
  106. \item \textbf{Securify v2.0}: Another tool we found that could theoretically detect SWC-124. Unfortunately, it works only with old solidity versions and we were not able to start it.
  107. \item \textbf{Slither}: A highly useful tool that offers a large static analysis toolkit for solidity, it not only allows the extraction of contract data like storage layouts but also automatic scanning for common weaknesses. Although it did not seem to be able to detect SWC-124, the storage layout functionality was used extensively by our team.
  108. \end{itemize}
  109. \section{Exploit Creation}\label{sec:exploit-creation}
  110. \subsection{Short recap of weakness definitions}
  111. SWC-124 attempts to use the underlying mechanisms that govern dynamic array allocation to deterministically overwrite arbitrary data in smart contracts. An analogous weakness can be created by employing unchecked assembly instructions, although this is a less common attack vector due to its unusual structure.
  112. \subsection{Exploit heuristics}
  113. The workflow of determining the vulnerability of a given contract is straightforward and follows the general approach of automatic detection mechanisms from our last paper. Since SWC-124 requires the presence of very specific code, it is relatively easy to develop heuristics to exclude non-vulnerable contracts:
  114. \begin{enumerate}[label=\arabic*.]
  115. \item any contract without dynamic arrays (or mappings with integer keys) or raw assembly including a SSTORE instruction can immediately be considered non-vulnerable;
  116. \item if heuristic 1 does not hold and there is a dynamic array or mapping with an integer key available, we can then apply a second heuristic and namely: 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. As such, we have the following "sub-heuristic":
  117. \begin{enumerate}
  118. \item if the version of the contract is higher than 0.8.0, we examine whether unchecked arithmetic\footnote{\url{https://docs.soliditylang.org/en/v0.8.0/control-structures.html#checked-or-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.
  119. \end{enumerate}
  120. \end{enumerate}
  121. The presence of dynamic arrays can be determined using \textbf{slither --print variable-order}. A sample output looks as follows:
  122. \begin{verbatim}
  123. $slither cans.sol --print variable-order
  124. Cans:
  125. +----------------------------+----------------------------------------------+------+--------+
  126. | Name | Type | Slot | Offset |
  127. +----------------------------+----------------------------------------------+------+--------+
  128. | ERC1155._balances | mapping(address => uint32[7]) | 0 | 0 |
  129. | ERC1155.tokens | uint256[] | 1 | 0 |
  130. | ERC1155._operatorApprovals | mapping(address => mapping(address => bool)) | 2 | 0 |
  131. | ERC1155._uri | string | 3 | 0 |
  132. | Ownable._owner | address | 4 | 0 |
  133. | Functional._reentryKey | bool | 4 | 20 |
  134. | Cans.START_TIME | uint256 | 5 | 0 |
  135. | Cans.END_TIME | uint256 | 6 | 0 |
  136. | Cans.amountClaimed | uint8[9998] | 7 | 0 |
  137. | Cans.CLAIM_ENABLED | bool | 320 | 0 |
  138. | Cans.soda | SODAContract | 320 | 1 |
  139. | Cans.SODA_CONTRACT | address | 321 | 0 |
  140. | Cans.baseURI | string | 322 | 0 |
  141. +----------------------------+----------------------------------------------+------+--------+
  142. \end{verbatim}
  143. In this example the \textbf{ERC1155.tokens} array is the only potential weakess present. We would then look for the presence of what-where writes to this array in order to confirm the potential presence of SWC-124. What-where writes are of the form
  144. \begin{verbatim}
  145. someArray[where] = what;
  146. \end{verbatim}
  147. \noindent and are a necessary code snippet for SWC-124. If such an instruction is present, we then attempt to reverse engineer a sequence of inputs to trigger the exploit, or formulate a reason why we believe this not to be possible.
  148. \section{Results}\label{sec:results}
  149. Applying the heuristics, mentioned in the previous section, we gathered the following data about the contracts.
  150. \subsection{Vulnerable contracts}
  151. Using the heuristics above, we were not able to find a contract that is vulnerable to SWC-124.
  152. \subsection{Non-exploitable contracts}
  153. \noindent Solidity files that contained no contracts, just libraries, that would not introduce SWC-124 to the contracts using them as per the heuristics:
  154. \begin{itemize}
  155. \item AuctionLib.sol
  156. \item LibRegion.sol
  157. \item LToken.sol
  158. \end{itemize}
  159. \noindent Contracts that were discarded due to the heuristics holding:
  160. \begin{itemize}
  161. \item DCU.sol
  162. \item ERC20\_Asset\_Pool.sol
  163. \item FacelessNFT.sol
  164. \item GElasticTokenManager.sol
  165. \item GoldToken.sol
  166. \item GovernmentAlpha.sol
  167. \item HedgeSwap.sol
  168. \item HermesImplementation.sol
  169. \item IMETACoin223Token\_13.sol - had this contract been compiled with solidity under 0.8.0, it would have been vulnerable.
  170. \item UniswapV3PoolAdapter.sol
  171. \item UserDeposit.sol
  172. \item WPCMainnetBridge.sol
  173. \end{itemize}
  174. \section{Discussion}\label{sec:discussion}
  175. \subsection{Conclusions}
  176. We have proposed initial heuristics which can show us whether a contract is vulnerable to SWC-124. They are easy to understand and apply even in large contracts.
  177. We have demonstrated an example workflow that uses the tool Slither as a data-gathering aid and applies the heuristics. As a result of this workflow, we were unable to find
  178. vulnerable specimen from the examples provided.
  179. \subsection{Lessons learned: what works, what doesn't}
  180. During the analysis that we performed, we were able to learn that none of the state-of-the-art tools have support for SWC-124 as of the time of writing.
  181. Because of this, currently the only way of detecting this vulnerability is the application of the heuristics from section~\ref{sec:exploit-creation}.
  182. The easiest way to protect yourself from this vulnerability is to use solidity 0.8.0+ and its built-in under- and overflow protection whenever using dynamic arrays (and in general),
  183. to avoid using assembly for trivial tasks and, if the need arises, to make sure that the assembly calls that modify the storage are formally correct and non-exploitable.
  184. \subsection{Open challenges}
  185. The initial heuristics are easy to add to a static code analysis tool, such as Slither.
  186. Afterwards, it can be used to develop a dataset of vulnerable or non-vulnerable samples, which, alongside with manually verified contracts, can be used to improve or expand the heuristics.
  187. \bibliography{exercise.bib}
  188. \end{document}