Smart Contract Security Through Damn Vulnerable DeFi v4
DeFi Security Summit 2024 · 7 Nov 2024 · 1:00:40
Tincho of The Red Guild works through challenges from Damn Vulnerable DeFi v4 in a one-hour hands-on session at DSS 101 2024, running exercises in Foundry and using flash loans to uncover vulnerabilities. He presents the wargame as a practical way into smart contract security and describes The Red Guild's grant-funded security education work for the Ethereum ecosystem.
Speakers
Topics
From DeFi Security Summit 2024, hosted by DeFi Security Summit.
Transcript
hello hello hello are you hear me yes perfect okay nice to see you all I know it's early I have a green tea from 7-Eleven if you're jet lag or whatever you I recommend it no sugar I think is nice you should get me through the whole hour we have one hour uh to talk about themi um thank you mly thank you for you and thank you for all the organization for the honor of opening this new DSS and one1 super excited to be here um so yeah we have one hour to just go through themable defi of course we going through everything of it but we will be showing some cool
features about it at some point that should be Shar in screen and you should be start seeing stop there because I'm only seeing that there and that perfect thank you very much uh so yeah for those who don't know me I'm Tino I'm a security researcher I've been doing security stuff on ethereum for the past five years or more I don't know I L count um I work at the red Guild a small team from latam that I co-founded and I will be telling you a little bit more about the red Guild very soon the idea is to just give you a quick very quick intro and then we I will just be opening Terminals and V code and those kind of things that you n people like um so yeah for today is the m defi um if you want to follow along to some extent you will be able
to um but I have only one hour and the MiFi is huge um and I want to show you many things and cool features that it has so I don't know to what extent you will be able to follow along if you're only starting out in smart contract security so perhaps you you just can I don't know listen to me and see the things that I will be that we will be able to endi and hopefully this will be a trigger for you to get excited about it to understand that in my humble opinion not because I created it but but because some other people have told me so that it's one of the best resources to get started in smart contact Security in a practical way um so hopefully you will find it so as well so
if you want to you can go to that page deify XYZ uh there's a repository that I will be showing you and you only need to have found so which is this development framework for smart contract um so that should be it in any case I will be showing you later about it so a little bit about the red very quick I know you're are not here to listen to me much about what we do and why we do it but and those kind of things uh but we do security and education for the public benefit of the of the theum ecosystem right so we are a very small team from Lam we are supported by theum foundation with grants of the theum foundation we don't sell products we don't sell Services we don't sell audits we don't sell tooling we don't have freemum we don't have Lo like nothing right so we just
work for the community in service of the community we try to complement the other security firms in the space that are kind of more selling commercial services and products we just do this for the community for free and everything we do is it's just for you everything it's just contributing public goods to eum security right I think we are only one of the few teams that are actually doing this and then M defi is actually part of that right um these days we are kind of positioned in this way and I wouldn't say we're a web three team I wouldn't say we're a web two team H we're kind of trying to understand that security goes well beyond what you call Web three and sometimes gets uh into the realm of Web Two And even those differences are very very difficult to pinpoint
sometimes U so we are just providing security for ethereum right um many of the initiatives that we have uh it's about education we have gamified learning platform such ASI and some others I will be telling you about because we also announcing some stuff at Decon too uh we do security research and M hunting that means participating in context in contest participating in back hunties doing spot checks uh we do security awareness campaigns we won't be doing this uh right now at DSS but for instance last year in eum Argentina we did an undercover security campaign in which we did some fishing and scams and everything just to raise awareness of security in the community we do tooling we participate as members of the security Alliance scene uh we do community engagement and and many more things if you want to read more about us you have the website and that's all for
the introduction of the red G so for today it's what is the defy we will do the quick local setup and I will just be hacking challenges right there should be okay no I I won't go into that later um so why you should play thei okay so we have realistic up to dat scenarios that essentially meanso um that in the you will find actual stuff that happening on minet okay um I think that these are one of the most upto-date challenges that you will find you will find uh security flows and code patterns and latest version of the libraries and we are integrating with many U external protocols and you will find that hopefully if you play themi you will be quite prepared to then go
into contests for example or start participating in back hunting because you will have already seen many of the practices and bad coding patterns that we see every day on myet so no [ __ ] right you just play no logins no premium no nothing you download something to your machine and you run it okay you don't need anything to play in the only Foundry and you wanting to play that's it um and these are many many of the things I don't know if you actually see all of that but we have many stuff right so flash price oracles governance nfts dexes Ling P Mar contract time BS met transactions and more and more more stuff many of the things that again you will find on real contracts on Main I already included in the Moi for you it's all very very Val coded very broken we are very proud
of that poly written code so please take a look at it and hopefully you will uh I know have fun with it and learned a lot um yeah okay the workshop um spoiler alert so I will be solving challenges okay so that means that if you don't want to see the solutions you should probably be leaving now um but don't worry because I will be solving like the introductory ones H the ones that you will probably solve anyways uh um but just yeah just make sure that if you really don't want to see any of the solutions I don't know don't listen to me much other than that I don't think there's much introduction um yeah I should go to themify I will do the overview and the setup um I should
also have mentioned yeah okay so if you speak with many I don't know known security researchers or popular security researchers on Twitter they will probably tell you that the MiFi is one of the resources that they started their career with so again um don't overlook them mul I think is very very powerful um and I would suggest you also that if you start playing it share something on Twitter uh share your right UPS share your Solutions um that in that way you will also help us to spread the word of themi I think this is actually the first first Workshop we ever do on of Dei like live um and de defi is already four years old I guess and I I don't know it's very popular in
the community just word of mouth so if you like it juste spread the word um okay so if let's just get it started right so there's not much to tell um if you go to the website it's just this very simple nothing too fancy uh some interactions we will see all the challenges is here H we'll just be playing two with probably Thruster and side entrance if we have more time I could do kind of a quick walk through some others but I think that we two will already be quite covered um but we'll see what happens uh they are not necessarily ordered by difficulty perhaps they are I don't know because probably the starting ones are to some extent easier and then anyway this is um version four
uh so the last challenges that you see there are the newest ones uh I don't know if there are already public writeups about them uh perhaps one or two that I have seen uh so if you are like feeling adventurous you can actually start playing the last ones too and start publishing write apps that would be quite cool because not many people are actually H solving them or being able to solve them so how to play all instructions the repository uh so this is essentially it right um so if I open truster for example which is going to be the first level that we we will tackle um you will usually find some context for the challenge and then um you can actually see the contracts and we'll see how that looks like so we'll be doing this and we will be doing also this one which is
called side entrance um and then we'll see if we have time to do more probably we W uh what else okay so um Thruster is about two things mainly Flash loones and arbitrary calls uh so we will be diving in a few minutes into what a flash Lo is what does it look like uh and why having arbitrary calls from your contracts to any other contract on myet is not the right thing to do and uh then we will have side entrance which is about um reentrancy but not the usual one that you will find on places and some B counting of handling Nether and also
how do we mix that with flash Loom right um and I could go on right uh we have more levels and stuff so if we have time we can go about them or after this Workshop you can just you know come to me and we can solve something together or whatever you want um so yeah the repository is uh here get ./ the redgill them vulnerable defi uh if you go to a website actually and you click this link you will go straight to repository um and then you can just as what repository just clone it to your machine and that's it if you have Foundry installed already uh that's that's perfect otherwise you can use a Dev container if you're using visual studio code if you want to start
local dependencies on your host machine which is a nice practice tomorrow we also have one workshop and we will be talking about Dev containers and why they matter and why you shouldn't be install independencies on your host machine uh so come listen to us too um so yes I have already downloaded things and close the repository so I have H things here um so that's the repository I unloaded I already run for build uh um so it's already compiling everything if you haven't done so please do it download the repository clonet and run Forge build that will actually download all the dependencies to your machine uh it's just downloading all GitHub repositories that we need
to do this um such as many of these right I don't know to what extent you can see this but um those are the dependencies that we use right so we have contracts from unop B3 Unis 2 unit absorbs already sales smart contracts permit tools open seing contracts multic calls and many many more stuff um but today it's going to be simpler than that so we just open Visual Studio code um I will trust the authors if you come to our Workshop tomorrow you will find many surprises about this so please come I will open this in a de container so that means I will be hacking inside my Dev container which is nice as I was telling you so um here we are
I will do let me see if I can do this again here for build nothing to compile and you should yeah but if I do this I don't see anything okay hopefully you can see something um I need to check out the latest version uh which is 41 so there we are just make sure we are there perfect so yeah that's it okay challenges um the MiFi it's about each challenge is about a set of contracts and a test okay um so if we want to play for example Thruster we go to a Thruster folder we have a rmy that has has um the the context that you need for the challenge and it says in
this case more and more lending pools are offering flash loans in this case a new pool has launched that is offering flash loans for of DVT tokens for free I'm about to explain you what DVT tokens are the pool holds a million DBT tokens and you have nothing you as the player right so to pass the challenge you should Rescue all funds in the pool executing a single transaction deposit the funds into the designated recovery account okay many things to take into account one is that we have a DBT token the DBT token is the them valuable token that we use in them bu defi it's just a near C20 token so if you go to it the valuable token which is right here you will see that it's just a usually rc20 token right so nothing too fancy everything's doing is just minting to the
deployer of the token and the maximum amount of tokens um this shouldn't be broken and the tokens in the M if should be actually be safe if you broke this probably you have bigger problems in the ecosystem um but yeah this this should be fine okay so in our challenge uh you as a player won't have anything but we we're about to see a contract that has a million of these tokens assigned and we should be able to rescue them and notice that I see rescue and I don't I don't say steel or whatever other work word okay um Demi is particularly uh focused on rescuing funds from contracts and that's some big difference and a very important mentality that you should have uh
what we are learning here is about rescuing funds and depositing them into in this case designated recovery accounts following what for example seal and the their safe Hardware are pushing these days uh we are not keeping funds we always depositing them into the recovery accounts that let's imagine that these recovery accounts belong to a protocol okay um so that's probably important to notice in Thena you will learn how to exploit a smart contracts right and in the way of explo in them is that you can actually rescue funds from them and deposited them into this recovery accounts so please whatever you learn in them more defi when you go to real life uh make sure that you do things right make sure that you to participate in Bounty programs
and do things the right way okay and then Defy is just an educational platform that you should take advantage of to them participate in the ecosystem as a white hat or as a security auditor or security research or whatever disclaimers made perfect so truster again we will have a lending pool that is offering flash loans uh we need to somehow rescue funds from it perfect um we have a single contract in this case which is the Thruster lender pool um in the source folder we usually have the challenges with the contracts and then we have the test folder this is just a regular foundary project okay H in the test folder we have a this similar folders as the other ones but we have a truster folder in which we have the foundary test so in thei you have to
find vulnerabilities in the contracts and code your solutions for the challenges in the tests in this case we have a Foundry test that I will just walk you through it um but all test in Demi follow a similar structure you will have Accounts at the beginning and these are the accounts that you should be using um that sorry that the scenario is using uh you will only be using the player one and and the the challenge make sure that you only use use the player one but actually we have deployer and the recovery account okay the deployer is the one that actually deploys contracts and the recovery account is obviously the one where you have to deposit funds um we said that the tokens in the pool are a million tokens and this is the VAR the variable that defines
that and usually then you will find after the constants the the the contracts that are involved in the challenge okay in this case we have only two contracts we have the Dem bable token and the pool perfect H you shouldn't pay attention to this modifier this is just an internal thing that we use to make sure that you actually use the player account and then in the M def you have the setup you have the how we test for initial conditions just to make sure that everything is okay you should be you just be taking a look at this but not actually touching this this is just for you to have context on on what the challenge look like looks like and then you can actually code Your solution here and then we check off the post conditions where you have actually solved the challenge or not um so on top of having like the context on the fre that I showed you earlier
you this is actually what you have to do okay so we have to only execute one transaction um at the end of The Challenge the pool shouldn't have any balance of tokens and at the end of the challenge you shouldn't have any token balance but the recovery account should have all tokens in Balance so somehow we should write stuff here such that we solved this challenge um such that moving from these initial conditions we go to this uh post conditions okay let's go to the cool stuff I told you that there's a single contract in this challenge remember this this is one of the inory challenges as you go as you progress into the Mula you find more more contracts and
more lines of solidity code and more we code in this case we have something simpler just because this is a 101 and I don't want you to go home all scared and and not want to come back ever to solute smart contracts so um okay I won't do line by line but to some extent let's let's see how it goes um we have this lender Pool A lender pool in this case will be a just a smart contract that has tokens on it and in this case is trying to mimic what a defi contract would do that is offering flash loan if you don't know what a flash loan is which might be the case essentially is a way in which contracts can give you money as long as you return that money in the
same transaction flash Al can be of ether of or tokens any asset um should be flash probably in this case it's using tokens so in this case the lender pool is actually offering flash loans of them valuable tokens those million Dem tokens that we s on the test are being offer for a flash Lo so uh to Trier a flashl we should be calling this flash Lo function that accepts different parameters right so the amount that you want to take who's taking that and then um in Flash loans what we usually do is that H you get um execution control so that means that the contract that has the funds will send you funds and will actually execute your code such that you can do something
with those funds and then return them so the usual flow for any flash that you ever see regardless of more or less complexity is usually checking the balance before executing a flash Lo which is done in this case the balance before transferring the funds executing the receiver and H then once the receiver is done in executing stuff um checking that the receiver has actually returned the money okay and that's done in this case so three steps uh check the balance before transfer the funds and execute the the receiver and then check that the the receiver has actually repay the flash down before we end the function okay so we have like I don't know 15 lines of solidity code and this
is badly broken somehow um with very little you can do uh very bad stuff on solidity um so if you want to see this another way you can actually see this this way I have some little nice diagrams for you today um so iny usually you will find lots of complexity not in this case but in other challenges so I just want to tell you that usually drawing stuff in diagrams could be useful H so in this case we have the player we have to do something in one transaction calling the truster ler pool such that we can move this million tokens from the trust lender pool to the recover address and then we also have the 10 valuable token okay but as we saw
earlier like on the Dem token there's no much we can do it's just anc20 so we should we should be doing something on the Thruster lender B um okay perfect so live coding uh session starts now you know how this things go the go of demos should be with us today um so we have a single entry point usually a security researchers is good of thinking of entry points that means what can I on this contract right H you should be able to execute something in this case we have a single function so somehow we have to execute this single flashl function on this contract uh one good practice that I would suggest you do for them vulnerab IFI is actually coding your
own contracts to solve the challenges you might be able to in some challenges not code your own contracts and just go Your solution straight here uh that means I will just do some uh SE code right but just do flash on whatever here but usually it's a good practice to actually go your own contract so let's call it h player Thruster um my contract I will just be defining this above in a second player Thruster let's do this um and you can actually just go your contact here okay um then if we want to just execute the stuff we do this with force test we use this flag to
say Force which contract we want to execute and in this case you have Thruster and Thruster Sol this will obviously fail but just to uh get it started okay perfect uh everything is failing and but we already are deploying an Mt contract um then what we can do is actually Define a function to start executing stuff and um we'll do is like the Constructor the the Tidy way we don't want to be too messy um so what I'm doing is I'm deploying a contract that I will be using to to solve this challenge uh but first I want to set it up right so I want
to have a reference to the pool for example and to a token as well um as then we I will show you why this is not strictly necessary actually to solve the challenge but uh I just want you to take it to take you step step by step through it um okay so we are um passing the pool reference to the Constructor so what I will should be doing is actually putting that here okay and then we have a run function that we will be calling okay so um probably that should be it for the for this part probably I don't know if then we should be including something but let's keep it that way um um I could pass the token as well in case we need it
in the meantime if you're following along you can start seeing what the actual vulnerability is Right token okay so we have reference to the mo token and we have reference to thr L the pool perfect um in run let's try this uh we should be able to call the flashl function somehow right um and to call the flashion function I'm just doing this because I'm thinking like that's the only function that I can execute right so somehow we should be doing something with this um and if you want to start thinking like an attacker to solve these challenges you need to understand that you have four parameters so you should be passing something in those parameters such that you can
actually take FS out in such a way that then you don't have to return them right because in this case we have a flash Lo function that is actually always checking that we return something so if you somehow are taking funds out through this function you should be able to re I mean not not that you should be able you must return them because code is checking that um so we should find a way to be able to at some point take a million tokens out of this contract without necessarily returning them um we have a couple of ways to do that but I want to show you first that this how this would work um in the right way so we should be specifying how much tokens we want to take out who's taking those
tokens out who's the Target and what data we're calling on that Target so if we want to take all tokens um we do this right so that's the amount that we want to take let's use name parameters because we are fancy and we are uh we like to do things the right way who's the borrower let's say that our contract is taking those those funds um who's the target uh us because we want to be executing some code after they give us the fun so this means that uh at this point when the contract calls something it will call us in that Target and then H we need to pass some data
and if you're if you're only getting started with solidity this will be a little bit harder to understand so bear with me um we need to specify which function we want the vulnerable contract uh to call on this contract so for example I could have uh another function here let's Call it Whatever execute external and we won't do mostly anything with it I will just do a console uh that says it's very difficult to write um that says hello world hello DSS so in the data we can specify actually uh this is why how solidity works we can actually specify in this data the function selector that so see yeah the function they have coded function that we want to
execute on our contract so there's a fancy way to do this and um if you're not again familiar with solidity you won't probably understand this but this is just how we do this um so we specify here execute okay so we actually taking a flash pH of a million D tokens um when we are actually saying the contract that they should be executing the execute function let's see if this works uh I should be passing another parameter workshops should have like background music such like lowy you know so we get into the mood and no oh sorry I'm writing what we should pass the token here
okay okay okay uh different error in this case it says can you read it yeah we can read it um it says rep repay failed so that means that we are actually taking some funds the thing is we are not returning them how do I know that we can actually use the beauty of Forge and in this case we can see the log hello DSS and we can actually see the whole Trace uh which is probably very difficult to understand if you're only starting now sorry but but with me so we have we are running the Run
function of our contract that's executing a flash loan we are getting the transfer you see here the transfer event so that means that our H contract is receiving that and then um that contract is executing our execute function here and below that we are seeing the console Lo hello DSS so the usual flow is okay and we're being able to execute a flash Lo and get a million tokens the problem is as I was telling you that we are getting the million tokens but the thing is we want to keep them and then send them to a recovery account right we don't want to return them because we know that somehow this is vulnerable so we should think better of which kind of data we should put in into this par such that we can actually do what we want to do the
thing is we have a single function here we have a flashl function and as I was showing you earlier in the diagram it's not that we have many other options we only have the D valuable token that we could code that's the other contract on the scenario um that do that I mean it does have other functions that we can actually start exploring and see what we can do with them because again flash Lo seems to be just a function on the thrust the pool but perhaps we cannot do much with it because it seems that from the from the execution that we see there's not much to to to do with it also we couldn't even recall it if you wanted to because it has this non reenter modifier so we are kind of lost we are receiving a million tokens but there's not
much we can do so let's go to the invaluable token uh that I told you that it's a near C20 so if it's a near C20 it has other functions that we can execute on it right it has an approv function it has a transfer function a transfer from function it has a permit function uh and probably that's it so um perhaps you say you R transfer and you say okay what if I call the transfer function of the them valuable token these kind of questions of what if are like very very important for security researchers okay so always think about what if what if I just call this as long as you're not testing things on myet thinking of what if and trying things out is okay
so what if I call the transfer function I don't think it would make much sense um because at the end of the day what we would be doing is kind of replicating this right we would be calling the transfer function of the token contract to take funds out but at this point in which we would be doing that we would we would already have the token so it doesn't make much sense to call transfer but but and we are getting to a gist of it what if I call another function which is called approve suspense um I could do something with that because what I would be doing is I would be
executing the approv function on the on the Dem valuable token but from the vulnerable contract so I I'm forcing the vulnerable contract to execute a function on the on the token and that's probably why arbitrary calls this is a this what this is what we call an arbitrary call are very very dangerous because in this way this function is allowing anyone to specify and anyone I mean an attacker to specify a Target and data that this contract will call in many contexts that might be okay I don't know in which ones but perhaps somebody finds a some way of doing that securely but usually it's not because again you're allowing it's like remote execution in web
2 I don't know the parallel is hard but in any case we can actually execute whatever we want we can force this contract to ex execute whatever we want for example the approve function so if you if you execute the approve function what we can do is actually Force an approval of on this contract to whatever address for example our address keep that approval not moving funds out in the flash loan function because we don't want to return them so we will call the flash function passing zero as an amount but actually three the approval that will make the contract approve our contract and after the flash function is done we will have we will still have the approval and that means that we will be able to transfer funds out of the contract because we have been approved okay so that's the
gist of the challenge and we can actually try to solve this now the target we said that would be the token okay so the token goes here actually the amount and the borrower in this case it doesn't even make sense so we can do we can put whatever stuff we want let's just put zero and data in this case we want to call the approv function of the token so if we go to the signature of the approv function it has two parameters an address and a 256 which is the amount so we should specify that here approve an address and new in 256 and then uh in this case because the function has parameters we should pass the parameters here
so we want uh us to be approved and we want the contract we could we could either do this uh how do you write this in Sol like this yes uh you could do this or you could [Music] do this uh like whatever we are just in this case we are getting what we call an infinite approval okay um so let's see in this case of execute function we don't need that anymore so let's see what happens if I just execute this okay nice we're ex we are calling the flash fun sorry we are calling the Run function which is
calling the flash Lo function we are passing some data um here's the transfer of the tokens which nothing's happening this is the event and then check this out we are calling the appro function of the then valuable token from the vulnerable contract and we are emitting the approval event so that means that as as a spender the player truster contract is getting an infinite approval from the vulnerable contract and the flashion function is finishing uh perfectly and we don't even need to return funds because we are not taking funds out which is cool so now that we have an infinite approval um we can do more stuff for example we can move the tokens from the
pool two recovery accounts that I need a reference to recovery okay so we pass the
[Applause] challenge I'm paying the guys the front so they clap and then you all clap um so let's see the let's see the trace such that you can actually understand what's going on again run function flash Lo function uh transfer nothing call approve um get the infinite approval and execute then transfer from on the token from the vulnerable pool to a recovery address and worth probably noticing us as players never touched the funds okay we move them from the vulnerable contract to a recovery
account that some protocol gave us and we never touch the funds very important um I'm not saying that you will always be able to do that in real life if you're if you're a white hat but finding ways to do that is good and there's something else that we can do we still have time okay there's something else that we can do because um we haven't really really really solve the challenge because the challenge says that we should actually be doing things in a single transaction and um because of how Force Works um if I really wanted to solve this challenge right I think I should use this weird flag isol it let me check okay so forche is telling us that instead of
using one transaction we're using two and for the challenge we should be using one so in this case when I run the test and using this isolate flag and that's because how Force works it says that we are executing more than one transaction it says that we are executing actually two transactions why because we are actually deploying a contract and deploying a contract is one transaction in real life and then we are calling a function on that contract and that's the second transaction that we're executing so what you can do and this is really like this is true for mostly yeah all challenges the M if I'm telling that whenever I tell you that you should be executing things on a single Transaction what you should be doing is executing things from the contract
Constructor so we can simplify this um little exploit and move everything to a Constructor of the contract in this case this will mean that we will execute a single transaction um and in real life for example that would mean that I don't know you couldn't be from run and and many other things anyway so it's quite simple we move let me move this to rec recovery go here and we will just be doing this and then deleting this and let's check if this works now okay and if I run with is
it okay perfect uh so yep that's level one Thruster we have some time left so I will be going over a second challenge that is somewhat similar uh but not that much but it also has a flash Lo so we should be able to solve it a little bit faster because we already know what a flash Lo is perfect so one challenge solved let's move to the next one then vulner and we will be doing side entrance okay if you ever wanted to know a little bit about reentrancy this contract or this challenge might help you so side entrance in this case it says
that we have a surprising surprisingly simp sorry simple pool that allows anyone to deposit ether and withdraw it at any point in time okay okay so in this challenge we are not handling tokens we're handling ether it's somewhat different we we about to see the difference so it says that it has a thousand ether in balance and it's offering free flash loans using The posited Ether to promote the system we start with one ether in this Cas in the follow in the earlier case we didn't have any funds in this case we do have we have one ether perhaps that's useful perhaps that's unnecessary we'll see what happens and we should pass the challenge rescuing O Okay so the pool is vulnerable somehow and we should rescue oer and again deposit the funds into designated recovery
account let's go here sance sance T okay we have this and the other one that we will be opening is the lender po perfect remember contracts in the source folder tests for the challenges in the test folder shouldn't be that difficult so uh in the test script as I was telling you earlier right so very similar structure this is just done so such that it's easier for you to get into the challenge quickly we are the players we have the recovery accounts we have the deployer we
have the constants that Define the amounts that usually players have or or pools have and so on we only have one contract in this case which is the pool so pretty simple challenge we have the setup H in which the employer is deploying the lender pool it's depositing funds and that's it and we're checking at the beginning that the pool has enough balance and that we as players have the balance that we should have and at the end of The Challenge what we should do is actually um not have any balance in the pool and the recovery account should have all balance uh available same as before we should be going something on this test uh to be able to pass this challenge so let's go to the side entrance lender pool in this case I tell I told you we are
not using tokens we are using ether so you will see some differences in this contract we have a mapping that is actually tracking balances for each account that deposit stuff we will have some positive entry on this mapping then we have an error some event and three functions if you think like a security man guy like a three entry points that you can touch to do something on this contract a deposit one withdraw one and Flash loan the deposit one is essentially just taking some ether from the color and tracking that depositing that into the big pool of ether that has the contact and tracking the accounting into the mapping okay so you deposit ether there's nothing you can do here on top of just depositing ether and
Ting an event um so should be okay then we have the withdraw function that is actually reading from the mapping reading whatever you have deposited earlier here it's actually taking that here then it's h clearing up the mapping and then it's transferring out the funds that read using a function this is like like just doing a regular transfer offer but it say it's safe so it should be safe um so deposit and withdraw you deposit money and then the contract tracks that and then you can call withdraw and withdraw the money that's it and then we have the flash loan that again it's pretty similar in the structure um related to to the earlier one that we saw in the previous challenge in this case you have the checking the balance then it's executing something
and then it's repaying it and sorry checking that it's been repaid now that in this case uh we are not using anymore so the developer seems to have learned the lesson they are not using anymore an arbitrary code they are actually calling a specific execute function on the receiver okay so if you want to take a flashl out of this contract you should be ex implementing an execute function on your contract that is able to receive ether and then you can do something with that ether if we go to the little diagrams and that we have in this case I don't want to spoil you the surprise okay let's do this like this uh you have three entry points you have a player with one ether you have three entry points on the lender pool and you should be able to move the th000
ether to a recovery account okay so a single contract we should be executing some stuff on it um so as I told you before uh it's good to Let's do do this it's good to have your own contract sometimes we will pass the pool here oh no you shouldn't do
this um function let's call it run external go something on the pool perhaps we could call flash we could call deposit we could do something with that okay so let's uh let's try this because we
want to execute a flash Lo probably we should specify the amount uh for the amount we're going to actually let's just try taking all funds and see what happens with that um and we should be implementing an execute function that in this case will be the one that the contract will call on us to transfer the funds good fun this should work test
s okay see if I can yeah okay there you can see the ls got funds so that means that uh yeah we got the funds from the flashl so we are being able to take ether out of this contract by calling the flash function and then we also have the opposite and the W draw function so what I want to highlight in this challenge um before we wrap up we still we we have some minutes left so if you take a look at these functions on their own you will realize something none of these functions on their own look vulnerable the deposit function is just where you deposit funds the withdraw function is where you withdraw funds is actually using
the checks effects interaction pattern it's using like recommended libraries if you take a look at the flash Lo function seems to be doing things okay just checking the balance transferring funds out checking that the funds have been repaid so sometimes when you think about vulnerabilities and smart contracts and this challenge is pretty much about this sometimes functions on their own are not vulnerable the vulnerable ities comes up when you you start mixing and putting functions together and making these functions play badly with each other okay so I think usually that's kind of a difference between like Junior security researchers and more advanced people is that if when you're only starting out you only look
vulnerabilities for vulnerabilities in a single line of solidity code and sometimes you won't find them that way sometimes what you need to do is thinking of ways in which you can actually mix functions and call them and call them twice and three times or call this and then this and then this like finding your ways into a contract and trying to trigger dynamics that perhaps developers didn't expect so this challenge is H for you to learn about this again if you look at these functions none of them are vulnerable but when we start mixing them uh we can actually Trier some cool stuff so what I have now in the test is I'm able to take funds out on the execute function we have received funds uh here so what can we do with those funds well we should be able to we we should be able to
repay we we have to repay somehow but if you how would we repay well we need to send the ether back to the vulnerable contract but if you take a look at it and this is kind of a a way to make the challenge a little bit easier it this it doesn't have a fullback or a receive function it's not that it has a receive function and blah blah blah so there's a singing way in which you can actually repay your funds and it's actually calling the deposit function so if I call the deposit fun I took all funds out so if I call the deposit function now that I have the funds those funds will be assigned to me as the Coler but the contract will believe that the funds have been repayed
because the funds got into a contract the thing is they got assigned in a mapping such that then I could call the withdraw function and take those ones out so let's try that and that's why it's kind of a re-entrancy thing because you take the flash loan you get the funds you deposit you call the contract back but not on the same function but actually on deposit you deposit them those funds get assign to you then you call withdraw and you take them out so you are able to bypass the flash loan protections this and by recalling on deposit and then taking the funds out via withdraw so we are actually combining the three functions to execute this attack knowing that not if you look at them alone none of them are are vulnerable on their own right um so let's call pull
deposit um I don't think we should be yeah okay perfect uh value just message that uh value so what we are receiving we are uh depositing it back and then H once the flashl is done you can actually call pull withdraw I will put a receip function here such that we are able to receip funds and withraw we shouldn't be calling anything okay so let's see what happens
now okay so it says that we are just missing putting fans in the record account that I will be doing in a second uh yeah let's do that now before we finish and so this this and I should post the record address bam okay so we solve another challenge um [Applause] woo if you want to take
a closer look at it again you can uh use uh everb mode and we can actually see the trace [Music] so uh we are actually taking the flash FL it's being the contract the vulnerable contract is calling our contract um then we are depositing the funds then we are withdrawing the funds and we are moving those funds into the recovery account so that's pretty much it for today uh it's that I have two minutes left and I will use them to wrap this up perfect so what we have seen is two levels of Dem vulnerable defi this probably good that the red Gil has contributed to the
to the security community in ethereum you have many many more challenges that you can play we have 18 challenges I just show you two but if you want to take a bigger look at it you have token as B flash Services meta transactions multi calls uh and you can go on and on and on and on and on and on and on and on and on and on so yeah h a lot of things that you can do okay uh go save them all go practice them M I think is one of the best resources that you will find ever to learn about smart contract security you can ask your favorite security researcher in the ecosystem they have probably play them M at some point in the time H remember this is worth of mouth um this is actually the first Workshop we ever do on them M Lei hopefully it was nice uh but it's it's
a thing that has been going around the ecosystem forever for more than four years already uh so I think it's it's one of the best resources that you will find um if you're are going to Devcon the red Gill is doing a capture the flag and esav your hand uh so please sign up go to cf. theed games.com the sign up is already there so if you want to participate on a capture of the flag that is not a strictly smart contract security related but is very much related to web 3 but not on vulnerabilities of a smart contract you should really sign up to the CF and tomorrow uh past noon at 1:10 p.m. with Mata my colleague here from the red gild also we are they in a workshop here on the DSs out hardening Dev enironment against back door
attacks and you will be very paranoid after going coming to our Workshop because we will be telling you how attackers can actually try to back door repositories in many many many ways and how you should be able to defend from that and that's why for example I've been using a Dev container today so with 30 seconds left on the clock I will just say thank you very much uh I'm tincho I work at the red Guild and hopefully you will be learning a lot from the vulnerab fight that's it [Applause]