DSS 2026 — Smart Contract Invariants Protect Against Cybercriminals Sofia Bobadilla · Martin Monperrus Source video: https://www.youtube.com/watch?v=lutbmp7QlD8 Automatic captions — not human-reviewed. Complete spoken-audio coverage is unverified. Source language tag: en (English). Source export SHA256: e9e099e08051f9754b3b52fe87253317e04daf451dc513d3756e2482e102d2ae [0:01] Thank you. So my name is Sophia Varia [0:04] and I'm a thirdy year PhD student at KTH [0:07] the Insty and my PhD topic is around [0:10] smart contract security. Uh previously I [0:12] work a bit on like patching smart [0:14] contracts and exploit generation and [0:16] today we we're looking at most effective [0:19] protections that one can have in their [0:20] code. But before our talk uh a bit of [0:23] promotion we are coming from uh Sweden [0:26] where we have this research group uh on [0:29] blockchain security and cyber supply [0:31] chain where our group leader it's here [0:33] with me Martin who is a professor at [0:36] KTH. So we will be switching between us. [0:40] So uh yes smart contracting variance [0:44] protects against cyber criminal. That's [0:46] our claim and hope we convince you by [0:47] the end of the talk. [0:50] So before we continue, let's make like a [0:53] clarification of the terminology we [0:56] utilize throughout the talk and through [0:57] the paper. So we often discuss about [1:01] invariance, right? Like auditing varants [1:03] and uh how to use them for auditing [1:06] protocols and developing protocols. Uh [1:08] here we define them as the specification [1:11] of your code. So the overall logic that [1:14] you want to apply on your protocol. For [1:16] example, if it's a flash loan that [1:18] you're trying to implement, one [1:20] invariant will be money must come back [1:23] at the end of the transaction. Now, if [1:26] you have this specification, right, you [1:27] have to be uh to you have to write it in [1:30] the code otherwise it's just a [1:32] documentation uh a natural language [1:34] description and it's never enforced in [1:36] your code. So to make this apply in your [1:38] code, you have a programming variant in [1:40] which you write this in your code in the [1:42] state of a in a require statement or a [1:46] modifier. And what this program will do [1:49] is that if well defined every time [1:52] someone interacts with your contract on [1:54] chain on the transaction there will be a [1:56] runtime check. So your required [1:58] statement will be executed and will [2:00] ensure that your condition is valid. In [2:03] the case of flash loan, it it means that [2:06] at the end of the transaction, there [2:07] will be a piece of bytes that will [2:09] ensure that the money came back to the [2:10] protocol. [2:12] So now way to see it for us is invarian [2:15] is the overall rule. The perm invariant [2:17] is the code condition that you write and [2:19] the ring check will be the one that will [2:21] revert when someone is trying to hack [2:23] you in simple words. [2:27] So building on this um as I said in our [2:30] team we are a few PhD students and [2:32] master students and Martin and we often [2:35] go over the exploits of the week and we [2:39] read the entire blog thread and Twitter [2:41] and we're like okay what happened in [2:42] this smart contract hack and here for [2:44] scoping we're talking about smart [2:46] contract hacks not general hacks so it's [2:48] not about the compromised wallet it's [2:50] about the code that was uh with a [2:53] vulnerability [2:54] and then we came to this hypothesis that [2:57] a real attack or like a a smart contract [3:00] attack came from a property that was [3:04] declared from the protocol but never [3:05] really enforced. [3:08] So our belief is if we find those [3:10] programming varants that should have [3:12] been written we could have prevented [3:14] this attack. [3:16] So our expectation is the expected [3:18] protocol property was missing a runtime [3:20] check and that's what the hackers found [3:22] and that's how they got the money. [3:26] So we are software engineer research. So [3:29] how do we apply science to evaluate [3:31] this? So we started from our very good [3:34] uh repository defi hacklabs which is a [3:37] community contributions of trying to [3:40] replicate exploits in the form of proof [3:42] concepts and uh we have uh rich [3:45] resources to understand the exploits and [3:47] to trace which protocols which contracts [3:50] on the protocol were involved and so on [3:52] and then we also utilize uh the onchain [3:55] evidence. So it's a real exploit. We [3:58] have transactions to track. We have [3:59] transactions that we know that are [4:01] malicious and transactions that we know [4:02] that were normal usage. [4:04] And utilizing this we proposed the [4:08] hardened version of the contract. So we [4:09] kind of asked the question what would [4:11] have happened if the missing variant was [4:13] in the code and how that hardened [4:16] contract would have survived against the [4:18] PC against the attack transaction and [4:21] how it it behaves on the onchain uh [4:24] traffic. [4:26] So we have two oracles here. The first [4:28] one is the PC by the fihack labs and the [4:32] other one is the uh onchain transaction [4:34] replay which is we collect transactions [4:37] and we try to reproduce them on the [4:39] harder version that we very carefully uh [4:42] wrote. [4:44] So what does these oracles will tell us? [4:46] So first the hacklabs will tell us and [4:49] then the P will tell us whether the [4:51] attack is being prevented but then the [4:53] onchain transaction replay will tell us [4:55] if we can prevent the malicious [4:58] transactions and we can't uh preserve [5:00] the normal traffic of the contract. [5:04] So how how we managed to do this? So we [5:07] had to build our own uh transaction [5:09] replay tool and uh we built on sources [5:12] of truth. So this is attacks already [5:15] happened and we have all the resources [5:16] that occur before. So here we hence we [5:18] have a onchain trace that's the onchain [5:20] trace that happened. So what we want is [5:22] that for normal usage the onchain trace [5:26] on the harden version has to stay the [5:28] same and the uh the trace for the [5:31] exploit has to defer with the um in [5:36] harden version. So we collect the [5:38] onchain trace which is O. And then since [5:40] we are we are modifying the bite code of [5:43] the contract. We want to ensure that our [5:45] replay mechanis is sound. So we [5:47] recompile the source code uh in the [5:49] setup that we will do with the harden [5:51] version. Just ensure that we are [5:53] recompiling the correct way which if [5:54] you're in this room you probably know [5:56] that is not an easy task. And so if we [5:58] can have some replay fidelity in our [6:00] tool, then we go and we check our harder [6:04] hardened version of the contract which [6:05] is the contract but plus the missing [6:07] environment. And here this included [6:10] modification. It's targeted to this [6:12] required statement. So we don't change [6:14] the logic uh or the way the functions [6:16] operate. It's mostly like what [6:18] properties were the protocol saying that [6:21] should be there but they were not [6:22] enforced. So if R can replicate O then [6:27] we check whether the harder version can [6:30] have a similar trace as the recompile. [6:33] So from here we will uh expect and [6:35] analyze these two cases. So for the [6:38] onchain um for for the transaction [6:40] replay we will expect or we will look [6:43] for in which cases we store is unchanged [6:45] and here the hope is that we maintain [6:47] the normal traffic of the contract and [6:50] then we will look in which cases the [6:52] story is not preserved. So this [6:53] transaction that is it happened before [6:55] and it passed why is it not passing [6:57] right now and here we had two options. [7:00] So either our invariant is operating [7:02] correctly and that transaction was [7:04] indeed malicious or we have a over [7:06] restriction. So imagine for example it [7:08] was a price oracle manipulation and we [7:10] put a threshold that was too tight. And [7:13] now Martin the floor is yours. [7:18] >> Thank you Sophia. [7:20] >> Um who has ever submitted a an academic [7:23] paper in the room? [7:27] Who has ever gotten a paper rejected in [7:29] the room? Ah, you're a true scientist. [7:33] So, so you know, Sophia could stop here [7:36] because she has this the theory, right? [7:38] Can I go back? I can. So, that's the [7:39] theory. In theory, everything is [7:41] perfect. You write an environment and [7:44] the environment protects against cyber [7:46] criminals and you're happy, right? [7:47] There's nothing more to do. But then [7:49] comes a reviewer and the reviewer say, [7:51] "Yeah, Sophia, I can't accept your paper [7:53] because you have to validate that. you [7:56] have to show me for a year for real [7:57] attacks [7:59] that it actually works and this is where [8:01] you spend another six months of your PhD [8:03] or even a year you know just to walk and [8:05] more and more and more and more to [8:08] actually prove and that's what we show [8:09] here. Uh so we took real attacks in defy [8:13] hacklabs that's the environmental data [8:15] sets. So you know they they are real in [8:18] all possible sense in the sense that [8:19] they are onchain sure but also in the [8:21] sense that there are real money like [8:23] there were real funs of losses here. So [8:26] real attacks for which we identify the [8:29] missing environment. We put the missing [8:30] environments recompile and we check that [8:32] the attack is blocked. [8:38] That's perfect. In all cases, the attack [8:41] uh is blocked. The the attacker cannot [8:44] run with the funds. So far so good. So [8:47] here we demonstrate that the invariant [8:49] would block the attack. But you know the [8:51] environment could say revert always. [8:54] Then it also just freeze the contracts. [8:57] So all those those attacks are on real [9:01] contracts. So those real contracts are [9:02] real transactions. Past transactions [9:04] more than 100k. It's a lot of past [9:06] transactions. [9:08] So one by one we rerun all those [9:10] transactions [9:12] on the new contract with the new require [9:14] statement and we check whether we have [9:17] the same the same onchain semantics [9:19] whether we have the same behavior. Is [9:22] that the same? Is that the same? Yes. [9:23] Yes. Yes. Yes. Yes. No. H interesting. [9:27] Yes. Yes. 88 98% of of yes. So it's the [9:32] exact same semantics. The environment [9:34] doesn't break anything. No. Why do we [9:36] have slightly not a perfect fully [9:38] perfect solution? Uh this is where it [9:41] becomes very interesting. Uh reason [9:43] number one is uh is gas semantics. So [9:46] you know etherm is the only primary [9:48] language in the world where computation [9:51] may depend on execution performance [9:52] let's say and so there is semantics with [9:54] gas and see we change a bit the gas with [9:56] the environment. There are some obscure [9:59] changes happening here. [10:01] Sure. Uh but the most important [10:04] interesting is actually what comes next. [10:07] Uh so that's here a guy. It was one of [10:10] the million tokens uh which was pumped [10:13] and dumped at some point. Not even dump [10:15] because it was hacked during the pump [10:18] because of a missing authorization [10:20] check. So most authorization are done. [10:24] Yeah. So missing it's not here. That's [10:26] the original code. You don't see it. So [10:28] the high any any permissionless attacker [10:31] could call this obscure family uh [10:33] function here [10:36] the fix the missing environment because [10:38] smart contract environments protect [10:40] against cyber criminals is this required [10:43] statement here that it's a permission [10:45] method so only the owner can can call [10:48] this uh this method [10:50] good uh there was some pumping on this [10:53] token so we have you know a few [10:54] thousands of of past transactions to [10:56] look at so we delay the few thousand [10:58] pass transactions [11:01] pass pass pass pass pass pass pass pass [11:02] pass pass pass pass pass pass pass pass [11:02] pass pass pass pass pass pass pass pass [11:02] pass pass pass pass pass pass pass pass [11:02] pass pass pass pass pass pass pass pass [11:02] pass pass pass pass pass pass pass pass [11:03] pass pass pass pass pass pass pass pass [11:03] pass pass pass pass pass pass pass pass [11:03] pass pass pass pass pass pass pass pass [11:03] pass pass pass pass pass pass pass pass [11:03] pass pass pass pass pass pass pass pass [11:03] pass pass pass pass pass pass pass pass [11:03] pass pass pass pass pass pass pass pass [11:03] pass pass pass pass pass pass pass pass [11:03] pass pass pass pass pass pass pass pass [11:03] pass pass pass pass pass pass pass pass [11:04] pass pass pass pass pass pass pass pass [11:04] pass pass pass pass pass pass pass pass [11:04] pass pass pass pass pass pass pass pass [11:04] pass pass pass pass pass pass pass pass [11:04] pass pass pass pass pass pass pass pass [11:04] pass pass pass pass pass pass pass pass [11:04] pass pass pass pass pass pass it f f f f [11:05] f f f f f f f f f f f f f f f f f f f f [11:05] f f f f f f f f f f f f f f f fail [11:07] we look at it and it's not the [11:09] transaction that is in deflab it's a [11:11] transaction that happened you know six [11:12] hours before seven hours before so we [11:15] what we actually detected in this case [11:17] is that we we detected you know canary [11:18] testing of the attack some people who [11:20] tried the attack it was not the final [11:22] attack with all the funds but they [11:23] actually triggered the exact same [11:25] violation of the security property [11:28] onchain [11:30] uh and we could detect it. So this fully [11:32] validate the soundness of the approach [11:33] because not only we detect the one big [11:35] attack but you also detect all the [11:38] canary test before by definition because [11:40] they violate the same security [11:43] environment. [11:46] Um and that the beauty on on chain all [11:49] canary tests can be seen uh including uh [11:52] the Binance hack. you know there were [11:54] some carrying test 23 minutes before the [11:56] actual hack um [12:00] which means you know people should do [12:01] live monitoring uh but yeah I don't want [12:04] to uh to digress here um another example [12:08] is the why earn uh problem [12:12] um so it was it was also a missing [12:16] authorization check like this one and so [12:19] you you make the the method the the [12:22] function permission so So far so good. [12:24] It blocks the attack. We're happy. [12:26] Oracle number one is happy. Oracle [12:28] number two is replaying the historical [12:30] transactions. And we see, you know, [12:32] quite a few of reverts. They all look [12:34] the same [12:37] and they look very legitimate. Um, and [12:40] so in this case, it means the the the [12:42] the reverted transactions were about the [12:47] keeper transactions which are part of [12:48] the protocol. the protocol add some [12:50] permissionless methods to be called by [12:52] keepers and in our manual analysis uh we [12:55] missed that aspect of the protocol. So [12:57] we wrote an environment which was too [12:59] strict and by replaying on the [13:01] historical transactions we detected that [13:03] this environment was too strict. [13:07] So it also validates you know the [13:08] soundness of the full approach uh not in [13:11] yes we did a mistake uh but it does [13:13] validate the soundness of the approach. [13:17] So [13:19] to conclude, smart contracts [13:21] environments are your best friends for [13:23] sure. Uh and we know that in the [13:26] community, you know, solidity is is [13:28] perhaps the one language with the most [13:29] the highest density of environments and [13:31] that's good, but it's still still not [13:33] perfect. Um and we demonstrated here [13:36] that you know so that's many of of us [13:39] are auditors here. when when you get an [13:42] audit uh you know finding you might say [13:44] okay what's missing is I should actually [13:47] call if then else do something else not [13:50] necessarily you know fixing a security [13:52] problem doesn't mean necessarily being [13:53] imperative in your fix it mean just [13:56] restoring the security environment and [13:58] then whatever goes wrong before that's a [13:59] beauty environment whatever goes wrong [14:02] it will still be caught by the security [14:04] environment [14:05] so you have to as as developers and as [14:08] auditors we have to understand you know [14:10] the core security properties how they [14:12] are translated into into require [14:15] statements and a require statement [14:17] becomes something which is falsifiable. [14:19] we can get it falsified on the PC. uh we [14:22] can get it falsified on the real attack [14:26] and [14:28] when you have historical transactions [14:29] you can replay the historical [14:30] transactions [14:32] and this is where you can say hey Martin [14:34] yeah thank you very much uh for this [14:36] research but uh you know for a new [14:38] protocol we don't have historical [14:39] transactions [14:41] which is correct so this aspect of our [14:43] work of the historical transactions is [14:44] to validate the entire you know [14:46] framework concept framework um in [14:49] practice you don't have this but what we [14:51] want [14:52] to what we claim and what what [14:54] developers, auditors, all of us in the [14:56] security committee should do is that we [14:58] should be obsessed you know days and [15:00] night by identifying the missing [15:03] environments [15:04] uh with our you know poor brains and [15:08] also with super intelligence artificial [15:10] intelligence or I don't I don't know [15:12] exactly but yeah we can you can you can [15:13] use AI to find the missing environments [15:15] to improve the environments and uh and [15:17] to improve the overall security of of [15:19] our protocols. [15:21] Uh, thanks a lot Sophia. Thanks a lot [15:23] for your attention. We can take a few [15:25] questions. Thanks a lot. [15:30] Questions. [15:41] >> Thank you. uh the data and I just wanted [15:44] to ask if you identify some family of [15:48] varants that are commonly missed. [15:51] >> Yes. So in the paper we proposed like a [15:54] taxonomy h actually on this area there's [15:57] a few related work and actually the [15:59] first one was uh by the moit in storaa h [16:03] so there are like um your classics [16:06] access control your classes uh price um [16:10] pricely page um [16:14] there were approximately eight cases [16:16] that we identify and a few of course are [16:18] related to uh business logical designed [16:21] but most of them could fall into these [16:23] categories of uh admin roles, [16:26] permissionless design, uh price [16:28] manipulations and dependencies [16:32] and related questions of the varants you [16:37] if I correctly you went and manually [16:40] like fixed the [16:42] um how many of I forget the numbers that [16:46] you were like 18 28 [16:48] >> 28 [16:48] >> yeah 28 how many of those invariants you [16:52] wrote. Do you guess probably but how [16:56] many of those do you think the team that [16:58] wrote the code knew about the invariant [17:00] and uh never checked it and how many of [17:04] them are for example the invariants that [17:07] don't check for an authority are just [17:09] like they knew probably they just didn't [17:12] put it but there are some complicated [17:14] invariants that I think that's the main [17:16] challenge here as you as you say so of [17:19] those 28 How many would you classify as [17:22] trivial? Maybe like Yeah, I forgot to [17:24] put it. How many are actually complex [17:27] environments that are probably hard for [17:28] a human to think about? [17:32] >> Um so I will say that um [17:35] um we maybe it's not easy for me to [17:38] answer these questions because I got all [17:40] the resources right. I got sources [17:42] afterwards. So I was not in the mind of [17:44] the developer. H and it was not an easy [17:46] task for us to you know abstract. We [17:49] found some cases in which everything was [17:51] completely wrong like all the accounting [17:53] was completely wrong but then we were [17:54] thinking okay what property should they [17:57] have thought about and so for example [17:58] the one of them was like only uh money [18:01] from the account involved in this [18:03] operation should be modified and with [18:05] that you will have prevented uh I would [18:08] say that um [18:11] many of them 20 out of 28 were like uh [18:14] very tough to identify but we are from [18:17] the mind of the [18:18] >> postmortm. [18:23] >> Thanks.