<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tevž Bijol on Medium]]></title>
        <description><![CDATA[Stories by Tevž Bijol on Medium]]></description>
        <link>https://medium.com/@tevz.bijol?source=rss-1eef0c397804------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fjCpQFArAyEoLFp2fXx16g.jpeg</url>
            <title>Stories by Tevž Bijol on Medium</title>
            <link>https://medium.com/@tevz.bijol?source=rss-1eef0c397804------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 12:05:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tevz.bijol/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Every Attack Is an Anomaly. Not Every Anomaly Is an Attack.]]></title>
            <link>https://medium.com/@tevz.bijol/i-rebuilt-an-ai-that-spots-crypto-heists-before-they-happen-then-i-took-away-its-answers-d792f46a5964?source=rss-1eef0c397804------2</link>
            <guid isPermaLink="false">https://medium.com/p/d792f46a5964</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[defi]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Tevž Bijol]]></dc:creator>
            <pubDate>Sun, 01 Nov 2026 19:33:16 GMT</pubDate>
            <atom:updated>2026-11-01T20:03:49.054Z</atom:updated>
            <content:encoded><![CDATA[<p><em>I Rebuilt an AI That Spots Crypto Heists Before They Happen.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yX5mA6ZNcGiGNheC7c0CUg.jpeg" /><figcaption>Photo by Leftfield Corn on Unsplash</figcaption></figure><p><strong>TL;DR.</strong> I replicated LookAhead, a machine-learning model that flags DeFi attack contracts at deployment, before any money moves. On the authors’ public data, my XGBoost version caught 64 of 75 attack contracts in the test set with a 0.6% false-alarm rate, close to the paper’s numbers. An Isolation Forest trained only on normal contracts caught just 14 at a similar alarm rate, and the biggest thing I learned is why: attack contracts are unusual, but so are thousands of perfectly normal ones.</p><p>Code and results on GitHub: <strong>github.com/tevzbijol-ops/lookahead-replication</strong></p><p>A quick word about who’s writing this. I’m Tevž Bijol, a 16-year-old from Bled, Slovenia. I’m in my first year at an economics high school, and this year I’m part of TKS Innovate, where I’m spending the year going deep on one problem: using AI to catch attacks on DeFi protocols before they happen. I got into this through finance. Markets were my thing long before crypto security was, and the more I read about DeFi, the more one question bugged me: how can a protocol lose millions in a few seconds, and could anyone actually stop it? This Build is the second step of that project. The first was my Learn article, <a href="https://medium.com/@tevz.bijol/can-unsupervised-anomaly-detection-flag-flash-loan-oracle-attacks-before-they-execute-ff9bd0548b1a">Can Unsupervised Anomaly Detection Flag Flash-Loan Oracle Attacks Before They Execute?</a>, where I argued that the best moment to catch these attacks is when the attacker deploys their contract, not when they strike. If you work on DeFi security or on-chain monitoring, I’d love to hear from you on <a href="https://www.linkedin.com/in/tev%C5%BE-bijol-73b405397/">LinkedIn</a>.</p><h3>The robbery takes seconds. The getaway car is parked outside for hours.</h3><p>Most flash-loan and oracle attacks on DeFi protocols happen inside a single transaction. The attacker borrows millions, bends a price, drains a pool and pays the loan back before the block is even finished. More and more of these transactions are sent through private channels, so nobody sees them coming. By the time a monitoring tool notices the attack transaction, the money is already gone.</p><p>But there’s a step before that. To run the attack, the attacker has to deploy a smart contract that holds the attack logic, and that deployment is public. This is the idea behind LookAhead: stop watching the vault and start watching the parking lot.</p><p>It isn’t only theory. In 2022, Forta reported that its machine-learning model flagged the attack contract behind the Team Finance exploit about an hour before $15.8 million was taken. My Learn article suggested that this deployment window offers the most promising opportunity to detect such attacks. This Build aimed to test that claim using actual data.</p><h3>What I replicated, and the question I added</h3><p>LookAhead was published at FSE 2025 by Ren, He, Tu, Wu, Liu, Ren and Chen. The authors collected 375 attack contracts from Ethereum and BNB Chain, deployed between 2020 and mid-2024, and compared them with normal contracts deployed on Ethereum between 2022 and 2024. For every contract, they extracted features you can read straight off the chain: where the deployer’s money came from, whether the source code is published, how many functions it has, whether it contains flash-loan callback functions, and how often it calls token functions like swap, sync or getReserves.</p><p>Their system stacks two models, an XGBoost classifier on those features and a transformer that reads decompiled code. I replicated the XGBoost part.</p><p>Then I added my own question. XGBoost is <em>supervised</em>: it learns from examples labelled “attack”. But tomorrow’s attack might not look like yesterday’s, and a brand-new attack style has no labels. So I trained an <strong>Isolation Forest</strong>, a model that only sees normal contracts and flags anything that looks out of place. If it worked nearly as well, you could catch attacks nobody has catalogued yet.</p><h3>How I did the <strong>Technical Implementation</strong>?</h3><p>The tools were Python, pandas, scikit-learn and XGBoost. The pipeline is like this:</p><p><strong>new contract → features (funding source, verified code, flash-loan callbacks, token calls) → model → risk score → alert above a threshold</strong></p><p>I downloaded the authors’ dataset (a 1.8 GB archive of SQLite databases) and wrote a small script that turns it into one table. I applied the same filters as their code, which removes token contracts and upgradeable proxies. That left 375 attack contracts and 15,721 normal ones.</p><p>The most important design choice was the split. I sorted contracts by deployment time, trained on the oldest 80% and tested on the newest 20%, exactly like the paper. A random split would let the model learn from attacks that happened <em>after</em> the ones it’s tested on, which flatters the results. A time split asks the only question that matters: trained on the past, would it have caught the next attack?</p><p>For the Isolation Forest, I set the alarm threshold using normal contracts only. A contract is flagged if it looks stranger than 99% of the normal contracts in the training set. No attack labels touch that model at any point.</p><h3>Attack contracts really do give themselves away</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*AKeyILDurF-D4LeXAiL54w.png" /></figure><p>Figure 1. Three signals in the LookAhead data, for attack and normal contracts.</p><p>Before training anything, I looked at the raw data, and the gap is huge. 62% of attack contracts contain a flash-loan callback function, compared with 2% of normal ones. 72% were funded from an anonymous source like a mixer, compared with 7%. And only 2% of attack contracts have verified source code, while 64% of normal contracts do. Attackers don’t publish their code. That makes sense, but seeing it in numbers made the whole idea feel a lot more real to me.</p><h3>Results: the replication works, but the shortcut doesn’t</h3><p>On the 75 attack contracts and 3,145 normal contracts in the test set, my XGBoost model caught 64 attacks and missed 11. It raised 19 false alarms, a 0.6% false-positive rate. That gives a precision of 0.77, a recall of 0.85 and an F1 score of 0.81.</p><p>The paper reports a precision of 0.83, a recall of 0.88 and an F1 of 0.86 for XGBoost alone, with a 0.41% false-positive rate. So my version lands a few points below theirs, which is about what I’d expect given that I didn’t tune anything. Their full system, with the transformer added, reaches an F1 of 0.90 and a false-positive rate of 0.16%.</p><p>The Isolation Forest was a different story. At its threshold, it caught only 14 of the 75 attacks, and two out of three of its alerts were false alarms. Averaged over five random seeds, it got a precision of about 0.32 and a recall of about 0.18.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*FqEs9TmTEEDRC7zndubieg.png" /></figure><p>Figure 2. Precision vs. recall for both models on the test set. The dashed line is random guessing.</p><p>The chart shows the whole trade-off. XGBoost (blue) keeps a precision above 0.8 until it has caught about 80% of attacks. The Isolation Forest (orange) never gets above 0.4 precision.</p><h3>Why the label-free model failed, and why that is the interesting part</h3><p>Here’s what surprised me. If you only ask the Isolation Forest to <em>rank</em> contracts from most to least suspicious, it’s actually decent. Pick a random attack contract and a random normal contract, and it ranks the attack as stranger 92.5% of the time.</p><p>The problem is the base rate. In the test set there are 42 normal contracts for every attack. If even 1% of normal contracts look weird, that’s around 30 false alarms competing with 75 real attacks. And plenty of normal contracts are weird: one-off deployment scripts, unusual DeFi integrations, experimental code. Unusual isn’t the same as malicious.</p><blockquote><strong><em>Attack contracts are anomalies. But in DeFi, not every anomaly is an attack, and that difference is the whole problem.</em></strong></blockquote><p>Supervised XGBoost doesn’t have this problem, because it learns which kind of unusual matters, such as flash-loan callbacks combined with anonymous funding. That’s exactly the knowledge you lose when you remove the labels.</p><h3>Why I ignored accuracy</h3><p>With 42 normal contracts for every attack, accuracy says almost nothing. A model that calls every contract normal would score 97.7% and still catch zero attacks. That&#39;s why every result here is precision, recall or false-positive rate.</p><h3>Where these results stop</h3><p>This replication covers the XGBoost half of LookAhead. The paper also stacks a transformer that reads decompiled contract code, and that half is next on my list. I worked from the authors&#39; precomputed features, and their data runs to mid-2024, so the next real test is a contract deployed this month.</p><p>There&#39;s also a catch hidden in the data itself. Every normal contract in the dataset is from Ethereum, while 233 of the 375 attacks are from BNB Chain. When I kept only the Ethereum attacks, recall dropped from 0.85 to 0.55 and precision rose to 0.89. So part of the headline number may come from the difference between the two chains rather than from how attack contracts behave. Separating those two effects is something I want to test properly.</p><h3>What I learned</h3><p>The most surprising thing was how good the label-free model was at ranking and how useless it was at alerting. Before this project I thought “can it tell attacks apart?” was the question. Now I think the real question is “can it tell attacks apart <em>at a false-alarm rate a protocol would actually accept?</em>” Those are very different bars.</p><p>If I did it again, I’d start by checking for chain differences before running any model, not after.</p><h3>What’s next</h3><p>For my Iterate project, I want to run the detector against real historical attacks and measure how much warning it gives: the time between the alert at deployment and the moment money leaves. If that window is minutes rather than seconds, an automatic response, like pausing borrowing against the targeted token, becomes realistic.</p><p>If you work on DeFi security or on-chain monitoring, I’d really value your feedback on this, especially on the chain issue.</p><h3>Key takeaways</h3><p>• You can catch many DeFi attacks <strong>before</strong> they execute by scoring contracts at deployment. My replication caught 85% of test attacks at a 0.6% false-alarm rate.<br> • The strongest signals are simple: flash-loan callbacks, anonymous funding and unverified code.<br> • Removing attack labels breaks it. An unsupervised model ranks attacks as unusual, but normal DeFi is full of unusual contracts.<br> • Check your dataset for shortcuts. Here, chain differences may inflate the numbers.</p><h3>References</h3><p>1. Ren, S., He, L., Tu, T., Wu, D., Liu, J., Ren, K., Chen, C. LookAhead: Preventing DeFi Attacks via Unveiling Adversarial Contracts. <em>Proceedings of the ACM on Software Engineering</em> 2 (FSE), 2025. <a href="https://arxiv.org/abs/2401.07261">https://arxiv.org/abs/2401.07261</a><br> 2. Liu, F. T., Ting, K. M., Zhou, Z.-H. Isolation Forest. <em>IEEE ICDM</em>, 2008.<br> 3. Qin, K., Zhou, L., Livshits, B., Gervais, A. Attacking the DeFi Ecosystem with Flash Loans for Fun and Profit. <em>Financial Cryptography</em>, 2021. <a href="https://arxiv.org/abs/2003.03810">https://arxiv.org/abs/2003.03810</a><br> 4. Forta. How Forta’s Predictive ML Models Detect Attacks Before Exploitation, 2022. <a href="https://www.forta.org/blog/how-fortas-predictive-ml-models-detect-attacks-before-exploitation">https://www.forta.org/blog/how-fortas-predictive-ml-models-detect-attacks-before-exploitation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d792f46a5964" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Can AI Catch a Crypto Heist Before It Happens?]]></title>
            <link>https://medium.com/@tevz.bijol/can-unsupervised-anomaly-detection-flag-flash-loan-oracle-attacks-before-they-execute-ff9bd0548b1a?source=rss-1eef0c397804------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff9bd0548b1a</guid>
            <category><![CDATA[loans]]></category>
            <category><![CDATA[finance]]></category>
            <category><![CDATA[ai-in-blockchain]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Tevž Bijol]]></dc:creator>
            <pubDate>Mon, 12 Oct 2026 11:47:24 GMT</pubDate>
            <atom:updated>2026-10-31T12:00:37.474Z</atom:updated>
            <content:encoded><![CDATA[<h4>Can unsupervised anomaly detection flag flash-loan oracle attacks before they execute?</h4><figure><img alt="An Ethereum coin next to a small padlock, representing cryptocurrency security" src="https://cdn-images-1.medium.com/max/1024/0*WATtfZCK5RcDsiUy.jpg" /><figcaption>DeFi apps hold billions of dollars, and their safety depends on the prices they trust. Photo: <a href="https://commons.wikimedia.org/wiki/File:Ethereum_Cryptocurrency_Security.jpg">Edwin.images, Wikimedia Commons</a> (CC BY-SA 4.0)</figcaption></figure><p>On March 13, 2023, an alarm went off.</p><p>About ten minutes before the hack, a monitoring network called Forta noticed a brand-new crypto wallet getting money from Tornado Cash, a service people use to hide where their coins come from. A few minutes later the same wallet put a new program on the blockchain, and Forta’s model marked it as malicious. Three minutes before any money moved, a third alert even named the likely target [1].</p><p>Then <a href="https://www.forta.org/blog/detecting-a-197-million-hack-before-exploitation-euler-finance-hack-retrospective">$197 million left Euler Finance</a> anyway [1][16].</p><p>Euler’s code had been <a href="https://cointelegraph.com/news/euler-finance-hacked-despite-10-audits-in-2-years-says-ceo">audited ten times by six firms</a>, and the alerts were right [1][16]. Nobody managed to act on them in time.</p><figure><img alt="Forta’s timeline of the Euler Finance hack, with alerts appearing minutes before the attacker’s first move" src="https://cdn-images-1.medium.com/proxy/1*M3ah_zRHtyfWOeoq-ncygg.png" /><figcaption>Forta’s timeline of the Euler hack. White dots are alerts, red dots are the attacker’s moves. Image: <a href="https://www.forta.org/blog/detecting-a-197-million-hack-before-exploitation-euler-finance-hack-retrospective">Forta</a></figcaption></figure><p>A few weeks ago I didn’t know what a flash loan was. This story left me with a question:</p><p><strong>Can an AI that has never been shown an attack flag a flash-loan oracle attack before it executes?</strong></p><p><strong>What you’ll get from this article:</strong> an explanation of what a flash loan and a price oracle are, and how an attacker combines them to borrow real money against a fake price, with a worked example. You’ll see how unsupervised anomaly detection works and how BlockGPT, a model trained on 68 million transaction traces, uses it to score how unusual a transaction is. You’ll learn why detecting an attack “before it executes” is hard: private transaction channels, the speed of the attack and the Euler Finance case. Then we look at what LookAhead’s data shows about the attack contract that is deployed before the attack, and how its features differ from normal contracts. The article ends with the limits of this kind of detection: false alarms, response time and better price oracles as an alternative.</p><p>A few words you’ll need first</p><ul><li><strong>DeFi</strong> (decentralized finance) is lending, borrowing and trading that runs on code on a blockchain, without a bank in the middle.</li><li>A <strong>smart contract</strong> is the program behind a DeFi app. Anyone can read it and use it, attackers included.</li><li>A <strong>lending protocol</strong> works like a pawnshop. You leave something valuable as collateral and borrow less than it’s worth.</li><li>A <strong>flash loan</strong> is a loan of almost any size, with no collateral, that has to be paid back within the same transaction, so within seconds.</li><li>A <strong>price oracle</strong> tells a smart contract how much something is worth. If you can fool it, the pawnshop will lend you a lot of money against junk.</li></ul><p>In one sentence, the attack is: borrow a huge amount for a few seconds, use it to push a price up, and borrow real money against the fake price.</p><h3>How big the problem is</h3><p>Chainalysis counted <a href="https://www.chainalysis.com/blog/oracle-manipulation-attacks-rising/">$403.2 million lost in 41 attacks like this</a> in 2022 alone [3]. TRM Labs counted <a href="https://www.trmlabs.com/resources/blog/number-of-price-manipulation-attacks-hits-all-time-high-as-usd-75-million-is-stolen-from-tectonic">32 price-manipulation attacks in 2026 by the end of August</a>, more than in any full year before [4].</p><figure><img alt="Bar chart of price-manipulation attacks per year according to TRM Labs, with 2026 the highest so far" src="https://cdn-images-1.medium.com/proxy/1*W4ekLWhniAsk7WzKEMNRUg.png" /><figcaption>Price-manipulation attacks per year according to TRM Labs. They count differently from Chainalysis, so 2022 shows 10, not 41. Image: <a href="https://www.trmlabs.com/resources/blog/number-of-price-manipulation-attacks-hits-all-time-high-as-usd-75-million-is-stolen-from-tectonic">TRM Labs</a></figcaption></figure><h3>How a flash loan works</h3><p>On Ethereum, a transaction is all or nothing. You can pack dozens of steps into one, but if a single step fails, the whole thing is cancelled.</p><p>Flash loans are built on that rule. Aave, for example, will <a href="https://aave.com/docs/developers/flash-loans">lend you any amount the pool holds, without collateral</a>, as long as you pay it back plus a small fee inside the same transaction [8]. If you don’t, everything is cancelled, so the lender can’t lose.</p><p>Most people use flash loans for normal trading. The problem is that for a few seconds, anyone can have the money of a hedge fund. The <a href="https://arxiv.org/abs/2003.03810">first academic study of these attacks</a> found returns of over 500,000% [2].</p><h3>The weak spot: the price oracle</h3><p>Smart contracts <a href="https://ethereum.org/en/developers/docs/oracles/">can’t see anything outside the blockchain</a>, so a lending protocol has to ask a price oracle what your collateral is worth [7].</p><p>This was the part that surprised me most. In my economics program and in my own investing, you just trust the market price. In DeFi, that price is exactly what attackers go after.</p><p>The cheapest kind of oracle reads the price from a <strong>liquidity pool</strong>, a contract that holds two tokens and lets anyone swap between them. Think of an exchange office with no staff, where the rate depends only on how much of each currency is left inside. Most pools follow one rule: the two amounts multiplied together have to stay the same (x · y = k), which is why one big trade can move the price so much.</p><h4>A worked example</h4><p>Say a pool has 1,000,000 TOKEN and 1,000,000 USDC (a token that’s always worth $1). Multiplied, that’s 1 trillion, and it has to stay that way. One TOKEN costs $1.</p><ol><li><strong>Borrow.</strong> Take a flash loan of 1,000,000 USDC.</li><li><strong>Pump the price.</strong> Use all of it to buy TOKEN. The pool now has 2,000,000 USDC, so it can only keep 500,000 TOKEN. You get the other 500,000, and the price is now $4.</li><li><strong>Fool the oracle.</strong> The lending protocol checks the pool and thinks TOKEN is worth $4.</li><li><strong>Borrow against the fake price.</strong> Deposit 250,000 TOKEN, which now looks like $1,000,000. The protocol lends 75% of that, so you get 750,000 real USDC.</li><li><strong>Sell the rest.</strong> Sell your other 250,000 TOKEN back into the pool for about 666,667 USDC.</li><li><strong>Repay</strong> the 1,000,000 USDC plus the fee.</li></ol><figure><img alt="Diagram of the six steps of the worked flash-loan attack, from borrowing 1,000,000 USDC to repaying the loan" src="https://cdn-images-1.medium.com/proxy/1*fa8K5SDsnjI7Lwodl2T3Mw.png" /><figcaption>My example, step by step.</figcaption></figure><p>Before fees, you end up with about $416,000. With a 0.3% pool fee on both trades and Aave’s 0.05% flash-loan fee [8], it’s about $412,000. The protocol is left with a $750,000 loan backed by TOKEN now worth about $444,000, and other users’ deposits cover the difference. It all happens in one transaction, so nobody can step in halfway.</p><p>In June 2024, <a href="https://rekt.news/uwulend-rekt">UwU Lend lost about $19.4 million</a> this way, even though its code had been audited [5][18]. The attacker used flash loans to move the pools its oracle relied on, so sUSDe, a staked, <a href="https://finance-pillar.wharton.upenn.edu/wharton-crypto-insights/ethenas-synthetic-dollar-token/">yield-bearing version of Ethena’s dollar token</a> whose price isn’t fixed at $1 [19], was priced at about 0.99 when they borrowed and about 1.03 when they liquidated other users [5][18]. Only a 4% difference, but on a big enough position that’s millions.</p><figure><img alt="Chart of sUSDe’s price in UwU Lend’s oracle: about 0.988 when the attacker borrowed and 1.032 when they liquidated" src="https://cdn-images-1.medium.com/proxy/1*6iW2NFsoiuk4FpCC4lf59w.png" /><figcaption>The real UwU Lend attack: about 0.988 when borrowing, about 1.032 when liquidating. Image: <a href="https://rekt.news/uwulend-rekt">Rekt</a></figcaption></figure><h3>Detecting attacks nobody has seen before</h3><p>Think about how your bank spots card fraud. It has never seen the thief, but it knows how you normally spend. If you usually buy coffee in Bled and suddenly someone buys a laptop abroad at 3 a.m., the bank blocks the card.</p><p>Unsupervised anomaly detection works the same way. The model learns what normal activity looks like and flags anything that doesn’t fit.</p><p>The alternative is <strong>supervised</strong> learning, like a spam filter that learns from labelled examples. For DeFi that’s tricky. There are only a few hundred well-documented attacks (<a href="https://arxiv.org/abs/2208.13035">one major survey</a> analysed 181 [20]), and most big hacks use a new trick, so a model trained on old ones is always one step behind.</p><h4>BlockGPT: scoring how surprising a transaction is</h4><p>Every transaction leaves an <strong>execution trace</strong>, a receipt of which contract called which and in what order. <a href="https://arxiv.org/abs/2304.12749">BlockGPT</a> trained a GPT-style model on 68 million of these receipts without telling it which were attacks [10]. A normal swap looks like a sentence it has seen a million times, an exploit like one with broken grammar.</p><p>BlockGPT cuts each receipt into small pieces called tokens: who called whom, which function, what amounts, and what was read or written along the way. Numbers are rounded to their first two digits, so 1,254 becomes 1,300. The model has a vocabulary of 100,000 pieces, including the most common contract addresses and functions, and it reads the first 512 pieces of each receipt through 8 layers [10]. Going through them one by one, it guesses at every step how likely the next piece is, then adds those guesses up into one score. A high score means the transaction looks like millions it has seen before. A low score means something in it doesn’t fit, which is the broken grammar in numbers.</p><p>To test it, the researchers took each of the 124 apps that were later hacked and ranked every transaction it had ever received, from most to least unusual. For 49 of the 124 attacks (42%), the attack was in the top three. For 20, it was number one. But they only tested apps that did get hacked, and for more than half of them, fewer than one in five alerts was a real attack [10].</p><h3>Why “before it executes” is hard</h3><p>A trace only exists once a transaction has run, so “before it executes” really means catching it in the <strong>mempool</strong> (the public waiting room for transactions), simulating it, and reacting, all within seconds.</p><p>Most attacks now skip the waiting room. Services like <a href="https://docs.flashbots.net/flashbots-protect/overview">Flashbots Protect</a> send a transaction straight to whoever builds the next block, so nobody else sees it first [9]. In the <a href="https://arxiv.org/abs/2401.07261">LookAhead paper</a>, 80 of 142 DeFi attacks (56.3%) went through a private channel like this, and the share grew from 44.4% in early 2022 to 77.8% by late 2023 [11].</p><p>If you only watch the attack transaction, the honest answer to my question is mostly no.</p><h3>Most attackers deploy before they strike</h3><p>What changed my mind is that an attack like this usually has too many steps to do by hand. Most attackers first write a smart contract with the whole plan in it and <strong>deploy</strong> it to the blockchain, and only then does a second transaction start the attack.</p><figure><img alt="Diagram of an attack in two steps: deploying the attack contract, then triggering it" src="https://cdn-images-1.medium.com/max/1024/0*tBBRrEZm6T5XMpDa.png" /><figcaption>An attack like this happens in two steps: deploy the contract, then trigger it. Image: <a href="https://www.forta.org/blog/how-fortas-predictive-ml-models-detect-attacks-before-exploitation">Forta</a></figcaption></figure><p>The second transaction can be hidden. The deployment can’t, because every new contract is public, and there’s usually a gap between the two. In LookAhead’s data, more than 60% of attacks had at least 100 seconds between deployment and attack [11]. In the <a href="https://www.theblock.co/news/regulation/2023-07-21-conic-finance-security-attack-240917">$3.2 million Conic Finance hack</a>, the attack itself was private, but the attack contract sat in public for <a href="https://arxiv.org/pdf/2401.07261v6">36 seconds<em> </em></a>first, with the victim’s address right there in the code [11][17].</p><p>It’s a bit like a bank robbery. The robbery takes two minutes, but the getaway car has been parked outside for a while. <strong>If you want to catch it early, watch the parking lot, not the vault.</strong></p><h4>Signals that give attack contracts away</h4><p>LookAhead compared 375 attack contracts with more than 210,000 normal ones [11]:</p><ul><li>More than 70% of attack contracts got their money from an anonymous source like a mixer, compared to about 10% of normal ones.</li><li>Only 1.6% published readable source code, which honest developers usually do. For normal contracts it’s more than 85%.</li><li>More than 60% had the function you need to receive a flash loan. Fewer than 1% of normal contracts have it.</li></ul><figure><img alt="Chart comparing attack contracts with normal contracts on funding source, verified code and flash-loan callbacks" src="https://cdn-images-1.medium.com/proxy/1*UFv2anxaaOAww1ozdlDpHQ.png" /><figcaption>Attack contracts vs. normal contracts. Data: <a href="https://arxiv.org/abs/2401.07261">LookAhead</a> (Ren et al., 2025)</figcaption></figure><p>This doesn’t cover every attack: 7.3% happened in the same transaction that deployed the contract, and 5.7% didn’t use a contract at all [11].</p><h3>Today’s early detectors rely on labels</h3><p><a href="https://arxiv.org/abs/2304.12749">BlockGPT</a> doesn’t need examples of attacks, but it only sees the attack transaction. <a href="https://www.forta.org/blog/how-fortas-predictive-ml-models-detect-attacks-before-exploitation">Forta</a> and <a href="https://arxiv.org/abs/2401.07261">LookAhead</a> check contracts at deployment, so they’re early, but both were trained on labelled attacks [11][13]. <a href="https://arxiv.org/abs/2502.03718">SmartCAT</a> uses rules written by experts, and in live use it found 616 attack contracts, only 19 of which had ever been reported [12].</p><p>I couldn’t find a published detector that needs no labelled attacks and still works at deployment time.</p><h3>Limits: false alarms, speed and oracles</h3><p><strong>Unusual doesn’t mean malicious.</strong> When LookAhead ran live, it flagged 129 of 23,463 new contracts, and only 26 were real attacks, so about four in five alerts were false [11]. Most false alarms were arbitrage bots, which also use flash loans and look a lot like attackers. If one passenger in a million carries a bomb, even a scanner that wrongly flags only 0.1% of innocent passengers (a 0.1% false-positive rate) stops about 1,000 innocent people for every real threat.</p><p><strong>An alert doesn’t stop anything by itself.</strong> <a href="https://www.forta.org/blog/detecting-a-197-million-hack-before-exploitation-euler-finance-hack-retrospective">Forta’s own review</a> says the Euler attack was too fast for a multisig, a wallet that needs several people to sign before anything happens [1]. UwU Lend paused about an hour after its hack [6], and when it reopened, the same attacker <a href="https://unchainedcrypto.com/uwu-lend-hacker-steals-another-3-7-million-from-protocol/">took another $3.7 million</a>. An automatic pause would be faster, but with four in five alerts false, it would mostly stop honest people.</p><p><strong>Isn’t a better price feed the simpler fix? </strong>Partly. <a href="https://docs.chain.link/data-feeds/data-sources">Chainlink combines prices from many exchanges</a>, and <a href="https://developers.uniswap.org/docs/protocols/v2/concepts/oracles">a TWAP averages a pool’s price</a> over a window such as 30 minutes, so a single flash loan can barely move either [21][22]. But protocols don’t always use them, and audits don’t always catch it: UwU Lend was audited, and its oracle still relied on pool prices that could be pushed inside one transaction [18]. Even a good oracle has limits. A TWAP lags behind real price moves, and a thinly traded token can still be pushed over several blocks. A hard-to-push oracle is the first line of defence, and detection is the second.</p><h3>My take</h3><p>Can unsupervised anomaly detection flag these attacks before they execute? Not on its own. It can only say something looks unusual, and DeFi is full of unusual but honest activity.</p><p>Where I think it helps is the parking lot: noticing the getaway car before anyone reaches the vault.</p><p>Every new contract would get an “unusualness” score from the signals LookAhead used. If one scores really high and mentions a lending protocol, it gets run on a copy of the blockchain [14], as Forta already <a href="https://www.forta.org/blog/attack-simulation">does</a>. If no money leaves in that test, it’s probably a strange bot, and only an alert goes out.</p><p>For the score itself, I’d start with an isolation forest. It asks lots of random yes-or-no questions about each contract and keeps splitting them into smaller groups. A normal contract looks like thousands of others, so it takes many splits before it ends up alone. A strange one gets cut off after just a few. Fewer splits means a more unusual contract, and no attack ever has to be labelled [23]. Its inputs would be LookAhead’s three signals: where the money came from, whether the code is published, and whether the contract can receive a flash loan. The score can’t do the job alone, though. With about one attack for every 900 new contracts [11], even a good score will mostly flag honest ones, so the simulation has to do the filtering. In my next project, I’ll test whether this works on LookAhead’s own data [15].</p><p>If money does leave in the test, the protocol should stop new loans against the token whose price can be pushed and keep everything else running, like a shop that finds a wrong price tag on one product and pulls that product until someone checks. After, say, 30 minutes the freeze ends by itself, unless the people running the protocol keep it.</p><p>In my worked example, the test would show 750,000 USDC leaving, loans against TOKEN would stop, and the attack would fail and cancel itself.</p><p>The hard part is speed. In the Conic hack there were only 36 seconds, so no person can be in the loop. It also isn’t free. Some honest users will get blocked, someone could trigger a freeze on purpose, and there’s still the question of who controls the switch in a system that’s supposed to have no one in charge.</p><p>The numbers above also show three gaps this design doesn’t close:</p><ul><li>7.3% of attacks happen in the same transaction that deploys the contract, and 5.7% don’t use a contract at all [11]. That’s about one attack in eight with no parking lot to watch.</li><li>Conic’s 36 seconds isn’t the worst case. Up to 40% of attacks leave less than 100 seconds between deployment and attack [11], and I don’t know yet whether the score, the test run and the freeze can all happen that fast.</li><li>A freeze on new loans doesn’t stop liquidations. Much of UwU Lend’s damage came from the attacker selling off other users’ collateral at the fake price [5][18]. To stop that, the freeze would have to pause liquidations too. But if the price is really falling, that leaves the protocol with loans nobody will repay.</li></ul><p>My answer to the question is still no. As the trigger for a small, short freeze, next to an oracle that’s hard to push, I think it’s worth building.</p><p>What would change my mind? Two things. If an isolation forest can’t tell attack contracts from honest bots on LookAhead’s data, the idea doesn’t work. And if most attackers start deploying and attacking in the same transaction, there’s no parking lot left to watch.</p><h3>Summary</h3><p>Most of these attacks now arrive through private channels, so watching the attack itself is too late. Watching the moment the attack contract is deployed, anomaly detection could set off a small freeze before any money moves.</p><h3>Sources</h3><ol><li>Forta, <a href="https://www.forta.org/blog/detecting-a-197-million-hack-before-exploitation-euler-finance-hack-retrospective">Detecting a $197 Million Hack Before Exploitation: Euler Finance Hack Retrospective</a> (2023)</li><li>Qin et al., <a href="https://arxiv.org/abs/2003.03810">Attacking the DeFi Ecosystem with Flash Loans for Fun and Profit</a> (2021)</li><li>Chainalysis, <a href="https://www.chainalysis.com/blog/oracle-manipulation-attacks-rising/">Oracle Manipulation Attacks are Rising</a> (2023)</li><li>TRM Labs, <a href="https://www.trmlabs.com/resources/blog/number-of-price-manipulation-attacks-hits-all-time-high-as-usd-75-million-is-stolen-from-tectonic">Number of Price Manipulation Attacks Hits All-Time High</a> (2026)</li><li>Rekt, <a href="https://rekt.news/uwulend-rekt">UwuLend — Rekt</a> (2024)</li><li>Unchained, <a href="https://unchainedcrypto.com/uwu-lend-hacker-steals-another-3-7-million-from-protocol/">UwU Lend Hacker Steals Another $3.7 Million From Protocol</a> (2024)</li><li>ethereum.org, <a href="https://ethereum.org/en/developers/docs/oracles/">Oracles</a></li><li>Aave, <a href="https://aave.com/docs/developers/flash-loans">Flash Loans</a></li><li>Flashbots, <a href="https://docs.flashbots.net/flashbots-protect/overview">Flashbots Protect overview</a></li><li>Gai et al., <a href="https://arxiv.org/abs/2304.12749">Blockchain Large Language Models</a> (BlockGPT, 2023)</li><li>Ren et al., <a href="https://arxiv.org/abs/2401.07261">LookAhead: Preventing DeFi Attacks via Unveiling Adversarial Contracts</a> (FSE 2025)</li><li>Zhang et al., <a href="https://arxiv.org/abs/2502.03718">Following Devils’ Footprint: Towards Real-time Detection of Price Manipulation Attacks</a> (SmartCAT, USENIX Security 2025)</li><li>Forta, <a href="https://www.forta.org/blog/how-fortas-predictive-ml-models-detect-attacks-before-exploitation">How Forta’s Predictive ML Models Detect Attacks Before Exploitation</a></li><li>Forta, <a href="https://www.forta.org/blog/attack-simulation">Detecting Exploits Before Funds Are Lost Using Attack Simulation</a></li><li>LookAhead <a href="https://github.com/zju-abclab/LookAhead">dataset and source code</a> (GitHub)</li><li>Cointelegraph, <a href="https://cointelegraph.com/news/euler-finance-hacked-despite-10-audits-in-2-years-says-ceo">Euler Finance hacked despite 10 audits in 2 years, says CEO</a> (2023)</li><li>The Block, <a href="https://www.theblock.co/news/regulation/2023-07-21-conic-finance-security-attack-240917">Conic Finance drained of $3.2 million in security attack</a> (2023)</li><li>SlowMist, <a href="https://slowmist.medium.com/analysis-of-the-uwu-lend-hack-9502b2c06dbe">Analysis of the UwU Lend Hack</a> (2024)</li><li>Wharton Crypto Insights, <a href="https://finance-pillar.wharton.upenn.edu/wharton-crypto-insights/ethenas-synthetic-dollar-token/">Ethena’s Synthetic Dollar Token</a></li><li>Zhou et al., <a href="https://arxiv.org/abs/2208.13035">SoK: Decentralized Finance (DeFi) Attacks</a> (2023)</li><li>Chainlink, <a href="https://docs.chain.link/data-feeds/data-sources">Data Feeds: Data Sources</a></li><li>Uniswap, <a href="https://developers.uniswap.org/docs/protocols/v2/concepts/oracles">Oracles (Uniswap v2 documentation)</a></li><li>Liu, Ting &amp; Zhou, <a href="https://doi.org/10.1109/ICDM.2008.17">Isolation Forest</a> (IEEE ICDM 2008)</li></ol><h3>About the author</h3><p>I’m Tevž Bijol, a high-school student from Bled, Slovenia, and an Innovator at The Knowledge Society (TKS). This year I’m exploring AI × blockchain security: how machine learning could spot attacks on DeFi protocols before any money moves. When I’m not reading attack post-mortems, I’m at music school playing accordion and piano. If you work in DeFi security and think I got something wrong, I’d like to hear it on <a href="https://www.linkedin.com/in/tev%C5%BE-bijol-73b405397/">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff9bd0548b1a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>