Security Summit 🦺 Live Audit with Cyfrin
ETHGlobal·Wed, Oct 9, 2024, 12:00 AM
Join Cyfrin Team for their Security Summit Live Audit. This Summit is part of ETHGlobal's flagship online event: ETHOnline 2023, one of the most exciting events on our calendar, anticipated by hackers all over the world. ETHOnline is a three week long online hackathon with over $200,000 USD in prizes as well as a series of curated summits, celebrating some of the most significant happenings & learnings from the past 12 months in web3. ----------------- Follow us: https://x.com/ETHGlobal?s=20 Check us out: https://ethglobal.com/ Email us at: hello@ethglobal.com
Transcript
[Music] we're going to be auditing a codebase called Lottery friends we've heard a lot about security we've heard a lot about all this stuff now let's get down to Brass tax how do we actually do security what does it take what does it look like what does it feel like from and what are all the tools at our disposal to make sure we are successful uh for those of you who do not know uh some of my uh information is here some of my socials are here my name is Patrick Collins I'm a smart contract engineer security researcher educator uh there's my Twitter and my YouTube uh I am the co-founder of cipon we are a smart contract security uh platform and we also run the code Hawks competitive audits platform and as I mentioned before we have created some of the largest uh and most watched smart contract developer educational videos on the planet uh the Youtube videos we have number one and number two for the most watch solidity courses ever created so we've been doing this a very long time here is the agenda for what we're going to be talking about here number one welcome in resources so I'm going to talk about some resources here we're going to go over the audit process from a high level then we're going to jump into the audit so I need to finish number one and number two in the next five minutes so we have plenty of time to actually go over the audit this is going to be the code base that we're going to be auditing or doing a Security review of here's the code base um I'll have a QR code and I'll show this link again in a minute it looks like I'm cutting off the bottom just a little bit let's fix that okay lovely much better I already have it queued up in my vs code great okay anyways um so some resources so number one for those of you who are looking to begin your security Journeys definitely be sure to sign up for code Hawks we are running an awesome program right now called first flights which are incredibly which are much easier fake code bases that we have created to give you a chance to find some bugs and get familiar get used to auditing and doing Security reviews right give you some confidence get because a lot of these professional codebases can be very daunting it can be very hard to chalk up a win you can be working nothing and these first flights are meant to be hey there's a lot of easy bugs to pick up in here to give you that confidence and show you you know how to get better so be sure to sign up for code Hawks and then finally be sure to sign up uh subscribe to my YouTube go to web3 education. deev as well subscribe for alerts as well because we are H we are working on the ultimate solidity security smart contract evm defi assembly course uh ever created and I have given out some Alpha that it will be done in the next 30 days um if you see me on Twitter tell me to get off Twitter and get back to making the course okay uh again here is the code base uh that we're actually going to be working with and uh before we actually get into doing this Security review or this audit um and then Al uh we want to talk about kind of what we're looking to do uh there's a slight different mindset if we're doing this for a competitive platform like code Hawks versus doing kind of a more traditional Security review in the competitive landscape we want to um optimize for time to find as many high impact bugs as possible normally in a private audit we have a lot more time uh and we can actually focus more on okay what what do we need to do to make this codebase safer and we give a lot better uh consultation advice uh for the live audit here we're going to assume that you're doing either or uh we're going to focus more on this kind of being a traditional private audit where we are going to actually create a report uh at the end of this process with all the bugs that we have found uh next so the audit process itself it's really at a high level these three steps uh get context uh use tools do a manual review and then write the report report that's really it at the end of the day it's these three steps the first step is actually the most important step most bugs are actually a result of some business logic being messed up right so it's a lot of times people think oh you need to be like this cryptography wizard person but it's really uh just understanding the business requirements of the protocol is oftentimes more important than being a super you know evm Savant right so getting context what is the protocol supposed to do and are there any scenarios where it won't do what it's supposed to do uh so getting context is absolutely crucial for the first step of any codebase right read the documentation right read the docs number two tools and manual review we'll go over some tools uh and then write the report and again I have video on my YouTube how to audit ens where actually legendary security researcher tincho goes over um his exact process for auditing a protocol and it's actually really interesting is we record that video of him like Hey like I'm going through ens like here's here's the approach that I'm taking and two weeks later he gets paid out $100,000 for a a a critical that he found right so literally we record that video he's like here I'm working on ens let me let me show you what I'm doing and then he got paid out for some massive bug you found absolutely insane one more thing um one more thing there's like three more things uh one more thing uh I have you might have heard me say like audit and Security review kind of flip-flopped a couple of times here as security researchers we want to take the more correct terminology here and we want to say Security review uh instead of audit uh Security review is a much better um uh representation of what we're actually doing right an audit isn't a very good term uh I can explain that later if there's questions about it but as security researchers we want to see the mindset of saying this is a Security review most protocols won't know what the hell we're talking about and they'll say oh like that's an audit right U but it's important for us to say it's a Security review uh and let the protocols call in an audit whatever they want to do okay uh the audit process more in depth uh it's really kind of broken into three phases this is a a private audit uh and even competitive audits where you do an initial review um you give the report of hey here's all the stuff we found the protocol fixes the issues and then you do a mitigation review where you check to see hey if all the stuff that they fixed actually fix the stuff or introduced more issues and the more we do security reviews The more we do audits the more we look into uh security the better we're going to get repetition here is the mother's skill in the beginning it's very difficult and it gets easier the more you do it the better you get uh here are the tools we're going to be working with today we're going to be working with Foundry uh we're going to be doing a little bit of static analysis with slyther I'll explain that as we actually use it uh and then we will be uh working with solid it as well and I'll explain what that tool is when we get there we won't be causing fuzzing foral verification symbolic execution Etc uh if you're interested in those I have a couple video of I have a couple of videos of those on my uh YouTube channel so now I will let people take some time grab this link and go to this codebase and we'll give people five minutes to do two things number one ask questions and then number two see how many bugs they can spot you have five minutes uh this is obviously the most expedited Security review you will ever do because I'm only giving you five minutes but let's do it here's the codebase um again you should be able to see the link Patrick alphac C eth Global live audit uh you have five minutes five minutes to find as many bugs as possible and then we're going to start the show and while we are doing this let me answer any questions that have popped up in the chat here I'm looking at the eth global. TV chat let me pull this up actually you want to jump in the chat to ask questions you can find that here uh eth global. TV uh that link right here you can jump to the chat ask questions so five minutes you all have five minutes four minutes now you have less time no questions so far okay nice and while you're all looking I'm going to take a swig of water can somebody uh can somebody paste this link to the chat that's the F oh thank you so much nuclear geek. eth much appreciated all right I'm going to answer these you can uh you can mute me if you want to spend some time looking for bugs by the way all right first question um are most of the bugs you find at assembly level or nor normal code level most of the time normal code level actually not uh assembly level there's I mean sometimes you find bugs at assembly level normally if a protocol is using assembly to me that's usually I usually kind of just um almost like as a blanket statement hey like maybe try to avoid doing that there's a reason we have compilers um but most most bugs the vast majority of bugs are come directly from somebody doing some incorrect business logic as opposed to some weird assembly thing what are the advantages of using Foundry over hard hat uh it's faster everything's written in solidity those are the two advantages um I would say the advantages of hard hat are just the opposite uh everything is written in JavaScript so so uh JavaScript is one of if not the most popular uh language on the planet very approachable Tool uh gives into kind of the JavaScript everywhere mentality where you know your backend's JavaScript your front end's JavaScript so they both have their advantages uh big fan of hardhead big fan of Foundry um I like Foundry for a lot of my stuff because it is faster uh and it's kind of becoming the go-to tool for security researchers okay two minutes left two minutes left two minutes left any other questions if you find a bug at the end of the two minutes drop in the chat how many bugs you found don't actually say the bugs just the number of bugs that you found this codebase has been kind of specifically tailored to have a lot of bugs in it it's a little bit of a sneak peek of one of the modules in the upcoming security course the codebase in the security course is going to be a little bit different I customized this specifically for this Workshop so this is a neverbe seen codebase and I will probably never use it again unless I want to repurpose it for some other content actually I probably will do that that's probably smart right one minute left any other questions before we Dive In I'm looking at the chat looking at the chat all right no other questions if there's any other questions as we go along feel free to drop in the chat like I said um we will be uh viewing that from time to time all right cool well welcome so I have already cloned this should got rid of that that's fine though I've already cloned this it's already in my repo um it's already here so we're going to be approaching this as if it is a real Security review as if we're actually doing a review live so what did we say the first step was going to be the first step for any code base is what what did we say here's kind of the more advanced one here the basic one get context or scoping and then reconnaissance right really we just want to understand what's this codebase supposed to do so before I even look into this code uh and this actually has a pretty familiar like I'm already very familiar with Foundry so I know that all the main code is going to be in this SRC all the tests are going to be in test script I can kind of ignore I want to read the docs so for this all the docs are in this readme.
md all right cool I'm seeing some people post in the chat the number of bugs they found excellent excellent okay so we want to read what is this codebase supposed to do right you can 100% look in the codebase um as you're kind of getting context here but I like to just try to understand what this codebase even does right and while I'm doing this what a lot of people will do a lot lot of security research what a lot of security researchers will do is they'll create a little folder or a little file or something called like oh like notes. MD or something just some place to dump your thoughts right someplace to to write down all your thoughts um and as you're going along reading the docs you can kind of put these thoughts down on paper some people print out the codebase to to write thoughts down some people write it down man whatever you want to do okay so let's read what this is supposed to do and we're going to go kind of quick through this because we've got a lot to show uh so this protocol is designed to be a fair Lottery users can enter the raffle and the owner of the contract takes a cut of the total pot as a fee so right away we understand a lot about what what we should be looking for in this code base the protocol is designed to be a fair Lottery just by that we say okay cool I know how lotteries work they often have a randomization right a random winner has to be picked to win some prize immediately attack vectors start coming to head okay is there a way to manipulate the randomness so that a not fair winner is picked is there a way to break some of the payouts is their way to break um the entrances users can enter the raffle and the owner of the contract takes a cut of the pot as total fees maybe is there some place where the cut is incorrectly uh calculated is there place where we can block the owner from taking their cut all of these attack vectors can start to percolate just by reading these two lines getting context incredibly important now we have some more docs here uh call the enter raffle function with the following parameters uh an address array of participants uh a list of addresses that enter you can use this to enter yourself multiple times or yourself in a group of friends duplicate duplicate addresses oh duplicate addresses are not allowed so that's something we want to look for users are allowed to get a refund of their ticket and value if they call the refund function so again more tack Victors can I break the refund function every X seconds the raffle will be able to draw a winner the raffle will be able to draw a winner will be given eth so the docs are kind of messed up and will be given eth uh the owner of the protocol will set a fee address take a cut of the value and the rest of the funds will be set to the winner uh I actually meant to take this out before this uh there will be no fee address but that might be something right there that's a vulnerability or we just say hey uh you have this in the docs and it's not there right so now we know a lot more about what the codebase is supposed to do we've got some context now we can actually start you know diving to the code base and really taking whatever steps we want to do right and like I said as we were reading maybe we took some notes like oh look for X attack you know uh ah it needs to be fair you know whatever we want to do any any notes in here I'm not I wouldn't I probably wouldn't have put in any notes yet but often times when I'm reading the docs like a big D5 protocol can be really complicated so I will um usually take a lot of notes uh when working with the def5 protocol and especi uh especially when you're doing a private audit or competitive audit if you have questions during this context uh this gain context piece of your your Security review you want to ask a lot of questions you know it's really important to have a highway of communication between yourself and the protocol so you can understand as much information as possible um so I probably would have asked a bunch of questions hey you know what's up with this fee address you know um or or maybe not actually because I think the docs are pretty clear but that's because I wrote them haha cool so we've gotten some context we know what the code base is supposed to do so maybe the next step The Next Step that we do is we just try to run the test Suite right so with Forge you know we can build it with Forge build looks like I'm missing dependencies so we're going to install some dependencies here um it looks like we need to install some uh some dependencies let's go back to the read me let's see if there's anything we missed ah okay we need to run the make make file so if I run make maybe that'll install the dependencies if I go to the make file not familiar with make file don't worry too much about it basically we just ran make and it ran remove install build and it looks like it has this install step which install stalls these very specific open Zeppelin contracts cool so uh the make actually just did that for us it installed all the dependencies it ran remove install build install it removed dependencies before just to start clean install installed the dependencies and then actually build it now we can run Forge build which is like kind of the equivalent of like hardhead compile or Browning compile and it looks like compilation went fine cool so next maybe I run the test right and usually for Foundry do usually just check founder. tomomo to make sure there's nothing in this config that's malicious it looks pretty benign to me so maybe then I'll run Forge test just R the test Suite see what we're working with here okay we've got some tests great I don't see any fuzz tests or invariant tests or something that might be something I I take a look at um but for the most part it seems like a pretty pretty standard repo with not super Advanced uh with a not super Advanced test Suite as a security research Searcher if I'm doing competitive audit that's actually music to my ears because I go aha these developers might not be as advanced uh when it comes to writing tests and they might have missed something because of that so as a as a private auditor that makes me very nervous because then I go oh my God they don't have a mature code base I'm probably going to tell them hey you need to do invariant tests like I actually I definitely would tell them hey you need to do a VAR test stateful fuzzing um if you have questions about what those are we can uh talk about that a little bit maybe I'll run Forge coverage get a coverage report right there is something that's very telling I see 0.7.5 of solidity uh that should be a red flag why are they using this super weird version and then we can see uh here it looks like they have pretty weak test coverage as well so around 76% of their lines are tested 76% of statements 70 70% of branches that's pretty 6 2% of functions that's pretty low so just by me me running this test Suite I can pretty much guarantee I'm going to find some bugs why because to me this says this is a not a very mature codebase because they haven't done a whole lot of testing here so in a competitive audit music to my ears in a private audit uh Panic well not panic just like oh okay now in a private audit you're gonna have to teach the client hey here's how you write better test Suite here's how you get your test coverage up Etc so and as you can see we're still kind of getting context what does the codebase feel like what does it look like security is kind of this journey and we are on it so now that we've done some of that you know we can start to look through uh some of the actual codebase right it looks like this is the main codebase it's Lottery friends.
Soul um because I'm very familiar with with Foundry I know I can look at their deploy script which is in the script fold ER and this will give me a good idea of how they plan on deploying it okay it looks like they're going to be doing some stuff with Unis swap some stuff with the we contract some stuff with the usdc and maybe one of the first things I do is I double check this uh even though this might be out of scope sorry I didn't talk about scope um normally whenever you do a review there's a scope uh I forgot to fill that in here uh the the exact contracts that you want to be auditing for us what's in scope is just Lottery friends. Soul right so in scope Lottery friends. Soul so the deploy script if we find bugs in here it doesn't matter but this can still give us some good context on on what to look for so we see some compatibilities using an older version of solidity um and we're going to be deploying this on the ethereum Chain we've got some stuff about rolls which is great um but in any case all just getting context right and this piece here I probably am going to report this as an issue you know why are you using this old version of solidity 076 uh why aren't you using a new one right that's probably one of the first questions we going to ask the protocol why are you using this old version of slity but Okay cool so now let's actually uh jump to the code base and uh before U actually um I was thinking we would do manual review first um actually I am going to show you Slither first so Slither is something called a static analysis tool um there's a static analysis tool it's a tool that very kind of dumbly just checks the codebase for for for for bugs but without actually running it's called Static analysis because it analyzes the code without running anything the opposite of that would be dynamic analysis where it's actually running some computation right so St static analysis tool just looks at the raw solidity and says okay is there anything I can kind of pattern match that's an issue uh and most of the time most code bases should run this before audit but if they don't you know you can run this now I will say you know if you're a competitive auditor and um a codebase uh runs and and Slither finds a bunch of vulnerabilities everyone's going to report that everyone's going to report the slyther vulnerabilities because everyone knows about slyther it's a phenomenal tool it should be one of the first tools that is actually run so I kind of cheated a little bit um I put this little Slither config in here which um basically helps me run slyther I've already installed it so I'm going to run it if you're not familiar with installing it don't worry about it if this is the first time uh that you've seen this tool run uh see how freaking cool it is uh so what this does like I said it'll basically read this entire codebase oops it'll read this entire codebase and it'll look for issues it might take a hot minute to run so we'll we'll come back to it I'm G to I'm going to hide the terminal for now while it runs uh and we'll do some manual review we will come back to to slow there but okay we're finally in the code base we're finally looking at code um and this is where we we start taking notes so right at the top of this code base we see this little carrot here prma solidity 0.7.3 my initial thought is this is a bug right here and I'll put like a note in the codebase I'll say like at audit or you know at review or just whatever whatever comment you know uh to say hey like this is an issue I might say floating pragma isn't cool you know it's I'm probably going to write something more foros in that but I might report this as like an informational informational finding uh you really want to nail your solidity types as opposed to do using like a flat a floating pragma just so that you're always using the exact compiler version that you want right maybe you run maybe a test only works on a certain compiler version so you really want to nail it um to in a specific version as opposed to using a floating one so that's going to be my my first finding just say hey you know nail this okay cool next looks like we have some imports they're doing named Imports love to see that uh we have some package dependencies from open Zeppelin we're going to skip looking at open Zeppelin for us we're going to say you know using open Zeppelin is a gimme uh we have some interfaces that they they're using as well it looks like they're using this I Unis swap V2 router you know maybe I I I ch she did I put the link here in here uh maybe I compare their interface to the actual interface to make sure they're they're actually the same they're actually correct um for now I'm just going to ignore that and I'm assume it's correct but I probably would double check to make sure it's good uh and we're doing some other stuff from open zeppin cool uh one thing to note we did install where is it package.
json we did install a very spe specific version of opens up in contracts that's great to see in the uh in the make file we got to see that a very specific version of open Zeppelin contract that's great to see but it's also weird to see because open Zeppelin V5 just came out why are you using this Antiquated version of open Zeppelin what's going on here are you trying to roll rug pull somebody what's going on anyways finally into the contract we have some comments this will give us more context I would read this I would leave notes but uh cool Lottery friends is ownable great so we know that own um the owner of the contract who's going to be taking the fees it's probably coming from this ownable stuff we have all these State variables we have this library that we're using cool we have some more State variables uh we have some events cool okay cool here's the first actual function that we probably look at right the Constructor we probably might double check make sure all these are right I'm just going to assume though right for now um I I might start asking a question like what is this swap path like what the heck is this I might put like a q what is a swap path why is this needed why are we doing some weird stuff with with Unis swap uh if we're unfamiliar with Unis swap maybe we have to get some context on Unis swap the rest of this stuff looks pretty straightforward uh obviously I would probably take more time looking at this but right now I'm kind of just skimming skimming the codebase looking for looking for any ideas cool we have this enter raffle function and this is where we probably are going to start going uh line by line and I actually usually like to look for the I call it like one one of the main functions what what is the protocol actually what are users actually going to be doing this enter raffle is going to be one of the functions users are actually going to be using ah there's the refund function that they were talking about um where if user enters the raffle and they want to refund before the raffle is up they can refund um we have get you know this is kind of just a view function whatever the select winner function we're going to be looking all over this one withdraw fees that's for the owner withdraw tokens it looks like that's for the owner too this is the internal function and it looks like we have this weird Unis swap sell profits for usdc function uh it looks like we're swapping the eth we get paid for for usdc to prevent currency risk so for some reason the protocol wants to swap the eth for usdc you know whatever they want to do cool I guess but so now we have some context of what the functions actually do um actually Foundry has a method that prints them out we can actually start looking through some of these functions to make sure they actually do what they do right so now we actually get into the manual review so this first function enter raffle these are what users are going to be doing they give a a an address array of new players cool it looks like they need to pay a required message. value equals some entr fee times new players. length Okay cool so for each player they need to pay some message. value let's check entrance fee uh looks like it's an immutable variable great it's set Once In The Constructor okay that makes sense right cool and it looks like they Loop through this array and they push each one of these players to the players array okay cool and then we have this weird check for duplicates where we again Loop through and we check for duplicates and right away this is something weird to me um first off when you see a loop you really want to think gas Loops are not great for gas the wi are we doing all this this gas stuff here and reading this I see we actually Loop through so we have this players array which is a state variable uh we Loop through this players array and then we Loop through it again to make sure that there's no duplicates here right and this already is an issue this is actually a security vulnerability That We're looping through this players array twice now why is this a vulnerability well so and then I might here just do like at audit and then uh why is this sticking out to me well the reason this is chat is actually giv me some some hints here it is very expensive but this is actually what's called a denial of a potential denial of service uh attack Vector now why is this a denial of potential denial of a service attack Vector well as this gets bigger and bigger doing this check for duplicates is going to get more and more gas expensive so if maybe there's when there's one person in here uh you know maybe this function costs 10 gas because you only need to check for one person but if there's a thousand people in this maybe this costs 5,000 gas and it becomes incrementally more expensive for a new player to enter the raffle because of this super gas expensive um check here and now what I would do to um actually well so right away because I've done this a long time I I know right away that this is an issue um but what I'll probably do to convince the protocol that this really is is an issue is I'll try to write a proof of concept an actual proof of code um that this is a this is an issue and I have written a test for this already uh and I'm just going to pop it on screen real quick oh to double check this test Lottery friends test so I'm just going to copy paste this in here call it uh oops oh puppy raffle whoops uh some hidden Alpha puffy raffle friends so what I can do now is I'm going to run this to oh and cool it looks like slyther actually finished we'll come back to that in a second what I'm going to do is Forge test d-mt I'm going to run my proof of code I wrote a test Suite that proves that it actually is gas o gas inefficient and then I need to do- vbv to get my console logs to print out uh so here is here are my dos gas cost of the first 100 players costs this much gas gas cost of the second 100 players costs this much gas so I've actually written proof and this is really good for competitive audits as well to actually have a test that gives you something like this that entering the and sorry I didn't show you the test we're we're running a little short on time that entering this raffle is actually going to get more and more expensive for the new for new people so what I might want to do if I'm a a a lottery attendee who wants to win is the instant the lottery opens I bang up the queue with a ton of fake uh alternate addresses that are all mine so that it's incredibly expensive for a new person to come in and join the lottery I basically say hey if you want to join this Lottery you're going to need to pay a hundred times more gas than I did to enter this Lottery disincentivizing other other people from entering the lottery giving you a very unfair chance of winning the lottery right so we have found a vulnerability we have proved its vulnerability and we're going to write it up as a report so um oftentimes uh oftentimes you know uh Auditors can write the report whenever they want let's assume we're going to be writing the reports as we do it so we're going to say report. MD uh we're going to write our first we're going to write our first report very quickly because time is of the essence okay cool and so we're just going to kind of keep it um some minimal markdown for that for now first thing we need to do is we need to decide uh the severity of this issue so right we we know there's some denial of service attack here uh we need to decide the um the impact and then the likelihood so how impactful is this I would say this is actually this is high U this is high maybe it's medium um because it it directly will affect other people entering this Lottery right it makes it way crappier for somebody to enter the lottery uh making this really not fair at all right it's going to cost the next person way more gas the likelihood of this is high or medium right so this might be a high or a medium it really depends for this one I'm going to say it's uh it's a medium because it will be incredibly gas uh costly for the attacker as well so I'm I'm going to mark this as medium you know uh I I I think marking this as a high would be very reasonable as well so I might say all right cool this is our first m one let's call it a medium uh and the way we want to uh and the way we do uh reports here is we do it with this format uh write up format.
MD I'm just going to copy paste here so this is the format of a write up so you're going to do the severity uh hyper uh hyphen the number description impact proof of concept and then recommended mitigation so the description of the attack what is the impact what is the proof that it's a real issue and then what the protocol should do to fix it uh and then for the title we want to use the format root cause plus impact that's how we want to title this so if we copy this bring it over to our report place it in like so this is M1 what is the root cause of this is the duplicate check duplic uh could say gas expensive duplicate check in we go back up what's the what's the name of this function in the enter raffle function so maybe I'll say watery friends enter raffle this is this is one popular way to reference a function you you say the contract name two semicolons and then the function name gas expensive duplicate check in Lottery friends into raffle causes remember because we're doing root root cause impact causes denial of service attack um making subsequent I spelled that wrong subsequent uh participants causing subsequent participants to spend more gas to enter the raffle uh because we're a little bit low on time I'm going to kind of speed through this you know um got some of this already written up so I'm gonna I'm going to cheat a little bit uh be like function Loops to the players array and this is actually Lottery friends players are ready to check for duplicates however the longer the r is the more checks the player will do the more guess blah blah uh impact uh this is where we would explain the impact um uh makes it less Fair blah blah blah proof of concept this is where I put my test Suite um for proof of concept uh you don't always have to do like write a test that actually proves it sometimes just explaining the exact steps is perfect enough um but here's what's really important the onus is on you and actually let me focus on myself for for a hot minute here I know I'm running short on time the onus is actually on you to prove to the protocol why this is an issue because here's what's going to happen for a private audit or a competitive audit number one if you're doing a private audit you report this thing to a protocol you go hey this is an issue but you do kind of a weak proof of concept here's exactly what's going to happen they're going to say yeah I don't think so and then not going to fix it and they're going to go get wrecked and it's going to be your fault because it is your job to prove to them why they need to fix this so the more proof you can put in this proof of concept the better in a competitive audit what will happen is if you put up a lofty proof of concept you don't write a proof of code which like includes a test Suite the judge is going to look at your code base and go I don't think so because it depends on the judge if your judge isn't a very good judge or they haven't been doing it very long they're going to go and this is probably fine this is probably not an issue and you are not going to get that right up and it is going to be your fault not the judge's fault so getting this right is incredibly important when in doubt if you're not sure you're getting across the importance of this write a test Suite write a proof of code prove that it is an issue drill this into you it is your job to prove issues two judges or the protocol you are an educator as a security researcher believe it or not you're an educator you need to educate why it is an issue great hopefully that is clear moving on uh so this is where I would put you know my code base I would explain it some more blah blah blah okay uh and then finally a recommed uh recommended mitigation uh this is where you would kind of this is where you would explain what to do uh I'm going to cheat a little bit here I'm going to copy paste some stuff um also have some diffs that I would uh that I would give like hey here's the exact code base I would change but uh this is you know at a high level this is where you explain what to do excuse me for this one maybe we consider allowing duplicates they can create new wallet addresses anyways so like you providing duplicates kind of doesn't do anything um consider adding a mapping to check for duplicates you know do something else um and then I you know uh for the for the puffy rapple for for the uh for the code base that this codebase was designed off of um I even did some diffs uh get really good at doing diffs and markdown if we show what this looks like in markdown it actually shows um you know in green additions in red subtractions um like that so like do this three back ticks and then say diff and then put pluses where you added stuff and minuses where you uh subtracted stuff that's great makes it very obvious uh what your recomend amended change is great I'm going to pause real quick for questions and then I want to show at least one more vulnerability uh from uh from slyther I wanted to show three I was super optimistic uh I wanted to show three manual review attacks and three we'll see how yeah I'm gonna pause pause for questions real quick and then try to rip through these last couple ones uh online 67 the duplicate check is not efficient nested Loop has a complexity yes exactly it is a um denial of service attack spot on you found it Well Done excellent okay cool looks like there's no other questions or um or uh I need to pause more often if there's questions we'll come back in a minute cool let me know in the chat if you if you're learning something or if I'm going too fast and this is not a very good use of time uh cool moving on so we found uh an issue in here maybe there's more issues I'm going to I'm going to get to the uh Slither in just a second maybe there's more issues um you know but we're going to kind of speed up Okay cool so then I would keep doing that that exact process go line by line you know I'm leaving you know oh this is a loop for players you know I'm leaving notes you know I like to do this double slash e to explain something to myself I'll do double slash Q to like ask myself a question just some type of commenting Syntax for you right and like I said often times I'll be going back and forth between the notes and the file to just dump as much as my thoughts as possible right so I would keep going I would read through all this stuff um I'm going to do I'm going to do one more uh one more manual review find uh we're going to do one Slither find and then we're going to do the report uh at an hour I approve it nuclear geek if only uh we're going to do one more manual find we're going to do one slither find and then we're actually do the report Generation Um I have to do that in less than 10 minutes okay cool so we come to the bottom ble word rep and we see this function Here Sell profits for usdc this says it swaps the eth for usdc to prevent currency risk on Unis swap V2 So Okay cool so we saw above players pay eth it looks like we get some profits uh and then this protocol is actually going to allow people to sell it and then it looks like it's going to send um those sold that sold eth for usdc it's going to send it to the owner cool this is where if you're not familiar with Unis swap you would need to get familiar with Unis swap so this is where again context is important if you know nothing about sometimes you will have to go down rabbit holes on that audit uh with tincho we would have to uh we would say hey uh excuse me tincho was actually went down a huge rabbit hole on DNS what is DNS the domain um uh domain name service how does that actually work and compared it to ens okay so sometimes you're going to have to go down rabbit holes to understand context to really understand what this codebase does you got to know how Unis swap works so even though Unis swap is out of scope for this audit review to actually effectively audit this you need to understand Unis swap so um we would read this and let's figure out if there's any issues here uh as we as I go read this if you see the issue go ahead drop it in chat there's at least one glaring issue so okay sell profits for usdc okay the first thing I noticed okay why is this public you know maybe I'll put like shouldn't this be external uh it you know if the contract doesn't call it elsewhere this should be external instead maybe I'll put um put that as a question public okay why is this public should this be only owner maybe ask this question should this be only owner um I'm just asking questions right should this be only owner okay I see amount address this. balance okay that looks fine okay here's the actual swap here so it takes the interface wraps it around the unit swap router we call this function swap eth for tokens we send some value and then we have some parameters in here maybe I'm not familiar with these parameters I'm going to control click or just go back to this interface and we know what are we looking for swap exact eth for tokens okay let's do a toggle word wrap what are the inputs amount out minimum path to and deadline so just a real quick precursor on what these actually do amount out minimum says the minimum amount that we want to get back so when you're using Unis swap it's like it's an amm an automated Market maker and it uses pools to without going too deep into how Unis swap works it uses pools to figure out the price of something but that price can kind of subtly change over time and you just want to you want to give Unis swap hey like here's at least how much usdc I want back uh the path I'm I'm not going to explain this for now address two is going to be the receiver of the usdc and then deadline is what block we should consider this swap bad right because let's say we send this transaction we send it to our node our node takes a long time to actually send it or maybe there's like a gas Spike um uh the price might actually turn bad so we want to say Hey you know in a couple blocks cancel the transaction right that's what this dead L is for so knowing that you know I could say Okay so this zero zero is going to be the amount out Min SWOT path we're going to ignore owner this is the two so that's good we're sending the usdc to the owner of this contract um and then we have block. timestamp which is going to be the deadline right away this should be kind of alarming to you but maybe we're we're we think there's an issue here but we're not exactly sure what it is so one thing a lot of people do is kind of Sanity check hey this swap exact eth for tokens is there some other person who was reported an issue with this maybe let's go to whoops it's not important maybe there's some other person who reported this uh we can come to the tool solid it you do have to sign up uh where we can search for somebody else who reported a finding using this function so we'll put this in it looks like somebody has reported this um on another competitive Auto platform and we can actually read the details of this uh to see if this is similar to the issue that we're actually running into um for me I know uh because I have read solid it a whole bunch that this is actually a slippage issue uh we can also look that up we can do slippage and actually I'm sure if we read one of these it would tell us what we want having the the deadline be blocked. timestamp this is saying that basically anytime you send this uh this function this will always be valid because they saying the deadline is the current block and it'll always check the current block and just always be valid so we're saying there's basically no deadline to send this and then number two the amount out Min is zero somebody could swap in uh somebody could jump in uh tank the price of eth on Unis swap with a flash loan to basically make uh this swap worthless give you zero eth or excuse me give you almost zero usdc because your minimum is zero and basically ruin all profits for the owner of this contract so if you're unfamiliar with me if you're like what the hell is Patrick talking about uh sorry we're kind of short on time sorry I can't explain it but this would be an issue this would be a massive high and we would report this uh there's a slippage issue here um if you go to Cod Hawks if you're familiar um if you go to contests this was a big issue in um the beetle contest U and this would show up on solid it as well and if you're uh interested in learning more about how uh slippage Works uh it's it's H4 sandwich attack to steal erc20 tokens in the feeds contract it's basically this exact attack if you want to learn more about what this attack looks like I've also got some me videos on my YouTube as well so I'll report that I know I'm pretty much at time uh and also yeah somebody Post in the chat can't block to time stamp be manipulated by miners uh kind of it's a lot harder now that they're validators um but it doesn't really matter because you putting in block a amp here basically says that this transaction is valid forever um which is no good because a node could basically just hold on to this transaction until they want the swap to happen and wreck you for your funds and and they can make a whole bunch of funds so um I know I'm a little bit over time uh he said I have an hour though so I'm going to go go for the next three four or five minutes uh we're g to pull up slyther we're going to see what slyther pointed out to us uh this is kind of the output of what slyther looks like it's categorized by red yellow and green green means it's low confident maybe low impact that this is an issue yellow is kind of medium and red is like this is probably an issue and if we read these there's actually a couple issues right out the gate um we have some arbitrary sending um and we have some weak Randomness excuse me we have some weak Randomness and then uh yeah arbitrary sends some weak Randomness it looks like we have potential re-entrancy and the refund function we've got a whole bunch of issues that we would want to check out uh and actually for this code base a lot of these issues are actually valid so we want to do writeups for all of these uh so that's what sther looks like now finally let's actually do a report so I'm going to kind of cheat a little bit uh I'm going to cheat a lot I'm actually going to give you a sneak peek into um a bigger sneak peek into my upcoming course so a derivative of this code base is actually uh this codebase called Puppy raffle um and this was this is my markdown report of that actual codebase so it's got uh four highs eight mediums eight informationals uh at the moment there probably will be more after I after I give it another world um but uh you can um you want to you want to put all your reports into a markdown file and then actually turn them into a PDF for clients because clients like PDFs uh report um sorry one second um Hub cyphon we have a code Base called it's called like report something audit report template planting where it gives you actually some templated markdown um that you can use to to help generate a PDF that looks really nice right and so this is kind of built off of that that markdown here so it has this like report example. MD it's a markdown file with a bunch of stuff in here just that you can kind of AutoFill in for creating your own PDF report uh once you've you know filled all your stuff in let's just assume we took our you know we have our report we took all of our findings in here we copy pasted them into this this template that we give us sorry I know I'm speeding through this um what we can do then is we can run this uh report generation uh script here uh once we've installed pandex and latex I'm going super fast I'm so sorry I I was way too optimistic with how quickly I can do a all this stuff we can run this tool I'm going to run it for you right here is pandoc um what did I copy paste this report is 2023 Lottery friends.
MD got to give it an output name from markdown a template listing great this we'll run it this will take a minute and it'll turn your markdown into a uh PDF and I forgot to add a logo in here so let's paste my logo in here R it again and then I'm then I'm done all right cool and so now we have this output called report.pdf if I reveal this and finder we now get this very nice PDF view of our uh report uh the logo uh that you added appears at the top you can obviously change a lot of the stuff in that markdown file but it makes this like nice looking report uh that you can use um for clients to be like hey here's here's my report here's how nice it is look at how look at how nice it is right um cool so I know I went super fast towards the end but hope you all learned a ton uh if we have time for questions I'll take questions uh if we don't thank you so much for coming and I hope you learned a lot if not uh you can find us on uh you can find us on Twitter as Cipher audits um you can also find me on Twitter at Patrick alphac C uh you can go to like I said go to Cod Hawks cipher.com to learn more about some of these competitive audits uh and stay tuned for the upcoming security course so uh thank you all so much for coming and hope you learned a lot
Automatic transcript — names and jargon may be misspelled.