{"id":10271,"date":"2022-11-12T10:20:00","date_gmt":"2022-11-12T10:20:00","guid":{"rendered":"https:\/\/www.trustrecipe.in\/?p=10271"},"modified":"2023-12-01T11:29:07","modified_gmt":"2023-12-01T11:29:07","slug":"dfx-finance-hack-nov-10-2022-detailed-analysis","status":"publish","type":"post","link":"https:\/\/immunebytes.com\/blog\/dfx-finance-hack-nov-10-2022-detailed-analysis\/","title":{"rendered":"DFX Finance Hack\u00e2\u20ac\u201dNov 10, 2022\u00e2\u20ac\u201dDetailed Analysis"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Overview<\/h3>\n\n\n\n<p>On the 10th of November, 2022, DFX Finance, operating on an Ethereum-based protocol with the attack manifesting on the Polygon network, fell victim to a security breach involving a <a href=\"https:\/\/www.trustrecipe.in\/what-is-a-flash-loan-attack-and-how-to-prevent-it\/\" title=\"\">flash loan attack<\/a> that exploited missing reentrancy protection. <\/p>\n\n\n\n<p>The assailant executed a sophisticated scheme, siphoning off assets exceeding $7 million in value. Of this sum, the perpetrator directly appropriated $4.3 million while an opportunistic MEV bot <a href=\"https:\/\/www.trustrecipe.in\/front-running-attacks-in-blockchain-the-complete-guide\/\" title=\"\">front-ran<\/a> the transaction, intercepting around $3.2 million. <\/p>\n\n\n\n<p>The exploit underscores a critical vulnerability within the smart contract&#8217;s design, emphasizing the necessity for rigorous security measures in decentralized finance protocols.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>About DFX Finance<\/strong><\/h3>\n\n\n\n<p>DFX Finance is a Decentralized Exchange (DEX) protocol that operates on the Ethereum blockchain, catering specifically to fiat-backed stablecoins and leveraging real-world FX price feeds to optimize trades. <\/p>\n\n\n\n<p>It utilizes dynamically tuned bonding curves to facilitate trading, ensuring price efficiency for stablecoin transactions. While specific statistics and notable clients were not provided, DFX has established a reputation in the cryptocurrency space for enabling efficient stablecoin trading.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Root Cause of the Hack<\/h3>\n\n\n\n<p>The primary reason for the hack was a missing <a href=\"https:\/\/www.trustrecipe.in\/shield-your-smart-contracts-with-a-robust-reentrancy-guard\/\" title=\"\">reentrancy guard<\/a> in the flash loan function. Due to this vulnerability, the contract incorrectly recognized the flash loan as repaid when, in fact, the attacker had only redeposited borrowed funds.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Attack Flow<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Initiation of the Attack<\/h4>\n\n\n\n<p>The attack on DFX Finance commenced with the exploiter leveraging the protocol\u00e2\u20ac\u2122s flash loan function. This maneuver involved borrowing USDC and XIDR stablecoins and then re-depositing them into the liquidity pools, thereby manipulating the protocol into falsely acknowledging the loan as repaid.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Execution of the Flash Loan Exploit<\/h4>\n\n\n\n<p>Calling the viewDeposit() Function: The attacker began by interacting with the \u00e2\u20ac\u0153usdc-xidr\u00e2\u20ac\u009d pair contract\u00e2\u20ac\u2122s viewDeposit() function, which calculates the required amount of USDC and XIDR tokens and mints corresponding lptokens.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"875\" height=\"191\" src=\"https:\/\/www.trustrecipe.in\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-1.png\" alt=\"\" class=\"wp-image-10277\" srcset=\"https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-1.png 875w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-1-300x65.png 300w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-1-768x168.png 768w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-1-585x128.png 585w\" sizes=\"auto, (max-width: 875px) 100vw, 875px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Borrowing and Redepositing<\/strong><\/h5>\n\n\n\n<p>Enough USDC and XIDR were borrowed and then redeposited back into the contract. This was facilitated through the deposit() method, which then invoked the ProportionalLiquidity.proportionalDeposit() function, registering the attacker\u00e2\u20ac\u2122s LP token and placing the borrowed funds back into the pool.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"688\" height=\"286\" src=\"https:\/\/www.trustrecipe.in\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-2.png\" alt=\"\" class=\"wp-image-10278\" srcset=\"https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-2.png 688w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-2-300x125.png 300w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-2-585x243.png 585w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Bypassing Flash Loan Checks<\/h5>\n\n\n\n<p>The contract was tricked into believing the loan was settled as there was no outstanding amount under the attacker&#8217;s address, allowing the bypass of transaction pair checks.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Withdrawal of Funds<\/h5>\n\n\n\n<p>The attacker then executed the withdraw() function, destroying the lptoken and seizing the USDC and XIDR tokens.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"714\" height=\"167\" src=\"https:\/\/www.trustrecipe.in\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-3.png\" alt=\"\" class=\"wp-image-10279\" srcset=\"https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-3.png 714w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-3-300x70.png 300w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-3-585x137.png 585w\" sizes=\"auto, (max-width: 714px) 100vw, 714px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Re-entrancy and Emergency Withdrawal<\/h3>\n\n\n\n<p>The withdraw function\u00e2\u20ac\u2122s re-entrancy protection was ineffective since the flash loan appeared complete, yet the attacker\u00e2\u20ac\u2122s lptokens remained in the lending contract.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"769\" height=\"248\" src=\"https:\/\/www.trustrecipe.in\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-4.png\" alt=\"\" class=\"wp-image-10280\" srcset=\"https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-4.png 769w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-4-300x97.png 300w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-4-585x189.png 585w\" sizes=\"auto, (max-width: 769px) 100vw, 769px\" \/><\/figure>\n\n\n\n<p>This allowed the attacker to repeatedly call the emergencyWithdraw() function, withdrawing all deposited tokens.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"827\" height=\"211\" src=\"https:\/\/www.trustrecipe.in\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-5.png\" alt=\"\" class=\"wp-image-10281\" srcset=\"https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-5.png 827w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-5-300x77.png 300w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-5-768x196.png 768w, https:\/\/immunebytes.com\/blog\/wp-content\/uploads\/2023\/11\/dfx-finance-hack-5-585x149.png 585w\" sizes=\"auto, (max-width: 827px) 100vw, 827px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">MEV Bot Intervention<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Front-Running Transaction<\/h4>\n\n\n\n<p>An MEV bot intervened during the attack, executing a front-running transaction and siphoning approximately $3.2 million from the transaction flow. This unexpected development resulted in the attacker losing a significant portion of the stolen funds.<\/p>\n\n\n\n<p>In an interesting turn, DFX Finance reached out to the MEV bot owner, requesting the return of the front-run funds.<\/p>\n\n\n\n<p><strong>Attack Txn: <\/strong>https:\/\/etherscan.io\/tx\/0x390def749b71f516d8bf4329a4cb07bb3568a3627c25e607556621182a17f1f9<br><br><strong>Attacker\u00e2\u20ac\u2122s Address:<\/strong> https:\/\/etherscan.io\/address\/0x14c19962e4a899f29b3dd9ff52ebfb5e4cb9a067<\/p>\n\n\n\n<p><strong>Attacker Contract:<\/strong> https:\/\/etherscan.io\/address\/0x6cfa86a352339e766ff1ca119c8c40824f41f22d#code<br><br><strong>MEV Bot Address &amp; transaction<\/strong>:<br>https:\/\/etherscan.io\/address\/0x6c6b87d44d239b3750bf9badce26a9a0a3d2364e<br><br><strong>MEV Bot\u00e2\u20ac\u2122s wallet address:<\/strong> https:\/\/etherscan.io\/address\/0xfde0d1575ed8e06fbf36256bcdfa1f359281455a<br><br><strong>Unconventional ordering transaction Block:<\/strong> https:\/\/etherscan.io\/block\/15941904<br><br><strong>MEV Bot &amp; wallet transaction address analysis: <\/strong>https:\/\/etherscan.io\/address\/0xfde0d1575ed8e06fbf36256bcdfa1f359281455a<\/p>\n\n\n\n<p><strong>DFX Finance Code: <\/strong>https:\/\/etherscan.io\/token\/0x888888435fde8e7d4c54cab67f206e4199454c60#code<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Stolen Fund Details<\/h3>\n\n\n\n<p>The attacker could only transfer $4.3 million worth of assets into their wallet. The remaining portion\u00e2\u20ac\u201cabout $3.2 million\u00e2\u20ac\u201c was extracted by an MEV bot in a front-running transaction, also called a <a href=\"https:\/\/www.trustrecipe.in\/what-are-sandwich-attacks-in-blockchain\/\" title=\"\">sandwich attack<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Hack Aftermath<\/h3>\n\n\n\n<p>DFX Finance had confirmed the breach, noting that they were alerted to unusual activities within 20\u00e2\u20ac\u201c30 minutes after the initial transaction. In response, they swiftly suspended all DFX contracts minutes after validating the attack.<\/p>\n\n\n\n<p>Furthermore, the DFX team has acknowledged the involvement of an MEV bot in the incident. This bot managed to divert a substantial amount of funds from the hacker. The team is currently seeking communication with the bot&#8217;s owner.<\/p>\n\n\n\n<p>In addition to these developments, the team had raised concerns about the <a href=\"https:\/\/immunebytes.com\/blog\/polygon-smart-contract-audit\/\" title=\"\">vulnerability of Polygon contracts<\/a> to similar exploits. Consequently, they are proceeding with an urgent shutdown of these pools and plan to publish a detailed analysis of the attack in an upcoming postmortem report.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mitigation Steps to Avoid Such Hacks<\/h3>\n\n\n\n<p>To avoid such re-entrancy attacks, the following measures could have been implemented:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Mutex implementation to prevent multiple accesses.<\/li><li>Reentrancy guard modifiers like those provided by OpenZeppelin.<\/li><li>State changes should precede external calls (check-effect-interaction pattern).<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>The DFX Finance hack highlights the importance of thorough, <a href=\"https:\/\/immunebytes.com\/blog\/smart-contract-audit\/\" title=\"\">smart contract auditing<\/a> and the implementation of robust security practices such as reentrancy guards. A detailed and comprehensive smart contract auditing can potentially prevent such vulnerabilities and secure the DeFi ecosystem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview On the 10th of November, 2022, DFX Finance, operating on an Ethereum-based protocol with the attack manifesting on the&hellip;<\/p>\n","protected":false},"author":2,"featured_media":10662,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[6,679],"tags":[],"class_list":["post-10271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-crypto-hacks-exploits","category-web3-security"],"_links":{"self":[{"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/posts\/10271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/comments?post=10271"}],"version-history":[{"count":5,"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/posts\/10271\/revisions"}],"predecessor-version":[{"id":10282,"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/posts\/10271\/revisions\/10282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/media\/10662"}],"wp:attachment":[{"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/media?parent=10271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/categories?post=10271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/immunebytes.com\/blog\/wp-json\/wp\/v2\/tags?post=10271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}