Get Into The Attacker's Mindset

Owen Thurm
18 May 202326:02

Summary

TLDRIn this video, the speaker delves into the essence of the 'attacker's mindset,' a crucial perspective for uncovering vulnerabilities and bugs in smart contracts. By adopting an offensive approach and actively seeking to disprove security protocols, auditors can significantly boost their success rate in identifying critical flaws. The speaker outlines four key steps to cultivate this mindset, including incentivizing oneself appropriately, believing vulnerabilities exist, collecting 'knobs' or potential vulnerabilities, and combining them to construct exploits. Emphasizing the importance of avoiding pitfalls that undermine this mindset, the speaker provides a real-world example demonstrating how this approach uncovered a critical risk-free trade vulnerability in a perpetuals exchange protocol.

Takeaways

  • 😎 Adopting an attacker's mindset is crucial for uncovering vulnerabilities and bugs in smart contracts. Think offensively to find what developers, thinking defensively, may have missed.
  • 💰 Incentivize yourself properly to find bugs by structuring your compensation based on the number and criticality of vulnerabilities discovered.
  • 🧠 Mentally prepare yourself to believe that bugs and vulnerabilities exist in the codebase you're auditing.
  • 📝 While auditing, collect and note down interesting behaviors, missing lines, or any peculiarities in the code ('knobs').
  • 🧩 Combine these 'knobs' to construct unique and critical attack vectors, like puzzle pieces.
  • ⚠️ Avoid charging upfront fees that remove the incentive to find vulnerabilities, as it may subconsciously convince you of the protocol's security.
  • 🚫 Never assume the codebase is secure or agree with the developers' assumptions. Question everything from the ground up.
  • 🔍 The example exploit involved delaying order execution by reverting with an 'empty position' error, then toggling the revert condition to execute the order at a favorable price.
  • 🤖 Leverage the two-step order execution system by executing orders with outdated prices from when the order was created.
  • 🗃️ Join communities like lab.guardianaudits.com to learn from other auditors, collaborate on audits, and access comprehensive smart contract security training.

Q & A

  • What is the attacker's mindset, and why is it important for smart contract auditing?

    -The attacker's mindset is a way of looking at code with the intent of breaking as many things as possible, rather than verifying or proving its security. It's important because adopting this mindset makes an auditor more likely to uncover vulnerabilities by actively trying to disprove the protocol's security.

  • What are the four key steps to get into the attacker's mindset?

    -1) Incentivize yourself correctly by structuring compensation based on the number and criticality of vulnerabilities found. 2) Personally believe that there are bugs and vulnerabilities in the codebase. 3) Collect 'knobs' or interesting pieces of behavior as you go through the code. 4) Combine the knobs to construct potential exploits.

  • What are the two critical mistakes that can prevent an auditor from getting into the attacker's mindset?

    -1) Not getting the incentives right, such as charging all the money upfront regardless of findings, which incentivizes spending as little time as possible on the audit. 2) Not believing that vulnerabilities exist in the codebase, leading to a mindset of verifying rather than attacking.

  • In the GMX V2 example, what was the first 'knob' or interesting behavior noticed?

    -The first knob was the two-step execution process, where a keeper executes orders using prices from the block when the order was created. This opened up the potential for a risk-free trade if the execution could be delayed until a more favorable price movement.

  • What was the second knob discovered, and how did it contribute to the potential exploit?

    -The second knob was that limit orders that reverted with an 'empty position' error would be retried instead of canceled. This allowed for delaying the execution of the order.

  • What was the third knob, and how did it enable the risk-free trade?

    -The third knob was the ability to set the remaining collateral amount to zero while still having non-zero size in USD and tokens. This allowed the order to revert with the 'empty position' error, delaying execution until a favorable price movement occurred.

  • What was the final knob that allowed toggling the exploit on and off?

    -The final knob was an if statement that set the initial collateral delta amount to zero if the order size matched the position size. By decreasing the position size to match the order size, this condition could be met, allowing the order to execute without reverting.

  • How does the provided example demonstrate the importance of combining multiple 'knobs' to construct a successful exploit?

    -The example shows how several individual behaviors or 'knobs' had to be pieced together strategically to enable the risk-free trade exploit. Each knob on its own was not sufficient, but by combining them in a specific way, a critical vulnerability was revealed.

  • What advice does the speaker give for finding interesting vulnerabilities and adopting the attacker's mindset?

    -The speaker advises auditors to be malicious in their approach, actively trying to break things and keeping track of interesting behaviors (knobs) as they go through the code. These knobs can then be combined to construct unique and critical attack vectors.

  • What resources does the speaker recommend for aspiring smart contract auditors?

    -The speaker recommends joining the Guardian Audits community (lab.guardianaudits.com) to connect with other auditors and participate in team audits. He also mentions a paid training resource he collaborated on, which aims to provide thorough training in smart contract security auditing.

Outlines

00:00

🧠 Embracing the Attacker's Mindset for Effective Vulnerability Discovery

Owen, founder of Guardian Audits, emphasizes the importance of adopting an attacker's mindset for uncovering vulnerabilities and bugs in smart contracts. This mindset involves thinking offensively to find issues that defensive-minded developers may overlook. Owen outlines four key steps to adopt this mindset: incentivizing oneself appropriately, believing in the presence of bugs, noting down peculiar behaviors or 'knobs' in the code, and combining these knobs to identify unique vulnerabilities. He contrasts this approach with the typical defensive stance of developers, who aim to secure protocols rather than break them. The goal is to shift focus from proving security to disproving it, thus uncovering potential vulnerabilities more effectively.

05:01

🚫 Avoiding Common Mistakes that Hinder Adopting an Attacker's Mindset

Owen highlights two critical mistakes that can prevent one from effectively adopting the attacker's mindset. The first mistake involves incorrect incentives; auditors should be rewarded based on the vulnerabilities they discover, rather than being paid a flat fee. This aligns their incentives with finding more bugs. The second mistake is failing to believe that vulnerabilities exist in the code being audited. Auditors should always be skeptical and question the security of the code, avoiding the trap of automatically agreeing with developers. This skepticism is vital for maintaining an offensive perspective and effectively identifying vulnerabilities.

10:02

🔍 Practical Application of Attacker's Mindset in Smart Contract Auditing

Owen applies the attacker's mindset to uncover a vulnerability in the GMX V2 smart contract. He identifies a unique attack vector arising from a two-step execution process, which allows for a potential risk-free trade by exploiting the delay in order execution. By observing how limit orders can be manipulated to delay executions, Owen pieces together various components ('knobs') of the contract to construct a potential exploit. This includes exploiting the price range validation in orders and manipulating the collateral amount in positions. The exercise demonstrates the practicality of the attacker's mindset in identifying complex vulnerabilities that might be overlooked in a traditional audit approach.

Mindmap

Keywords

💡Attacker's mindset

The attacker's mindset refers to the mental approach of looking at code or a system from an offensive perspective with the intent of breaking or finding vulnerabilities in it. This is in contrast to the defensive mindset of developers who aim to secure the protocol. The video emphasizes the importance of adopting this mindset to effectively uncover bugs and vulnerabilities that developers may have missed. For example, the script states, 'getting into the attacker's mindset is, probably the number one thing that you, can do to immediately boost the amount, of findings vulnerabilities and bugs, that you uncover.'

💡Incentives

Incentives refer to the motivations or rewards that drive an auditor's approach to finding vulnerabilities. The video suggests structuring incentives in a way that encourages the auditor to actively seek and uncover vulnerabilities, such as being compensated based on the number and criticality of findings. As stated in the script, 'you want to ensure that before you even, go into the audit you have the correct, incentives that are setting you up for, Success where you really want to find, vulnerabilities.'

💡Knobs

Knobs are the individual pieces of code or behavior that an auditor identifies as interesting or potentially exploitable. The video advises collecting these knobs during the initial code review and then combining them to construct more complex exploits or attack vectors. As the script states, 'you want to, note down everything interesting every, interesting piece of behavior anything, that's off any line that's missing you, want to note that down and save it for, later because as you're going to see, things like this can be combined with, other knobs in order to yield really, unique and critical attack vectors.'

💡Risk-free trade

A risk-free trade is a type of vulnerability or exploit that allows an attacker to execute a trade or transaction with the knowledge of future price movements, effectively enabling them to profit without any risk. The video discusses an example where the attacker can delay the execution of an order and then execute it with outdated prices, resulting in a risk-free trade. As stated, 'essentially there's a unique attack, Vector here where if I can delay the, execution of the keepers transaction I, can get a risk free trade.'

💡Empty position error

The empty position error is a specific error condition mentioned in the video that, when triggered, allows the attacker to delay the execution of an order. By exploiting this error and then toggling it off at the right time, the attacker can execute a risk-free trade. As the script explains, 'if I can get get a, limit order to revert with the empty, position error then I can delay the, execution of the Keeper's transaction.'

💡Keeper

In the context of the video, a keeper refers to the entity responsible for executing orders on the exchange. The exchange uses a two-step execution process where users create orders, and then keepers execute those orders in a separate transaction. This is done to mitigate front-running attacks. The attacker's goal is to manipulate the keeper's execution to enable a risk-free trade. As stated in the script, 'GMX has a, two-step execution system where you, create an order it's it's sort of like a, request to have an order executed and, then a keeper comes in and executes your, order.'

💡Limit order

A limit order is a type of order used in trading where the trader specifies a specific price at which they want to buy or sell an asset. In the context of the video, the attacker exploits the logic around limit decrease orders (a type of limit order used to close a position at a profitable price) to set up the risk-free trade. The script mentions, 'particularly for a limit order we can, see that in the set exact order price, function which is used to essentially, set the prices that are going to be used, to execute this order for a limit, decreased order.'

💡Oracle

An oracle is a third-party service that provides external data, such as asset prices, to smart contracts or decentralized applications. In the context of the video, the attacker manipulates the logic around how the oracle's price ranges are used to execute limit orders, enabling the risk-free trade. As mentioned in the script, 'we're going to validate that price is, ascending and so essentially this is, going to take the earlier price in the, range of prices that are provided by the, Oracle and the later price and the range, of prices provided by the Oracle.'

💡Proof of Concept (PoC)

A Proof of Concept (PoC) is a demonstration or implementation of an idea or vulnerability to prove its feasibility. In the video, the narrator mentions writing a PoC to verify that they could set the remaining collateral amount to zero, which is a crucial step in the risk-free trade exploit. As stated, 'we wrote a POC and it's actually, true you can essentially have an initial, collateral Delta amount that will allow, you to set the remaining collateral, amount to zero.'

💡Lego bricks

The term 'Lego bricks' is used as a metaphor in the video to describe the individual knobs or pieces of code that the attacker combines to construct the final exploit or attack vector. By piecing together these Lego bricks (knobs) in a specific way, the attacker can create a 'masterpiece' exploit, in this case, the risk-free trade. The script states, 'we have successfully found a way to, essentially make an order revert And, Delay that success here's what we've, done if we can use this as a timeline of, price action here's what we'll use as a, timeline of order execution, let's say before this we have a position, that has let's say we have two thousand, two thousand USD of size so this is the, size of our position and let's say we, have just 10 collateral tokens 10, collateral tokens of collateral maybe, this is ether let's say it's ether it, doesn't matter right so we have that and, then we have an order in the store and, let's say the order is for 10 collateral, tokens and only 1500 size, so now what we've got so far is we see, that if this order is to be executed at, this point in time right so let's say, you know the order would be executed, tried it would be normally attempted to, be executed at this point in time what, would happen is this order this 10, initial collateral Delta amount would, negate the collateral on the position we, would end up with zero collateral tokens, we would end up reverting on this, validate position function when we check, validate non-empty position the, remaining collateral Mount is zero and, we revert with the empty position the, empty position up in the older order, Handler is ultimately causing a top, level revert which means that the keeper, will not execute the order so as long as, this order lives in this collateral, amount here is 10 and the collateral the, initial collateral Delta amount on the, order is 10 then this will continue to, revert revert revert until let's say at, this point right here now we see that, prices moved in a favorable Direction, meaning that if we were to be executed, with prices from over here on the left, at this point in time on the right, we would make risk-free money we would, be able to buy ether at cheaper than the, the market rate or you'd sell ether at a, higher price than the market rate you, know something like this so now exactly, what we need is the knob to twist to be, able to toggle this off and be able to, make the empty position error not get, raised and so after scanning and looking, and looking for exactly that thing to, twist we can find that exactly here on, this if statement right here this is the, last Lego Brick of our attack Vector.'

Highlights

Getting into the attacker's mindset is probably the number one thing that you can do to immediately boost the amount of findings vulnerabilities and bugs that you uncover.

You need to start thinking offensively to uncover the bugs and vulnerabilities that the developers, who are thinking defensively did not uncover.

The attacker's mindset is very simply boiled down just a way of looking at a code base where you're trying to break as many things as possible and there's an optimal way to do this.

The attacker's mindset is going to benefit us because if we set out looking to disprove the security of a protocol by finding a valid vulnerability in it then we're going to be a lot more likely to uncover that vulnerability than if we were to set out to verify or essentially convince ourselves of the security of the protocol.

Incentivize yourself accordingly by structuring the way you get paid or rewarded by the amount of vulnerabilities and criticality of vulnerabilities you uncover.

You have to personally believe that there are bugs and vulnerabilities in the code base itself.

Collect knobs as you're going through the code base - note down everything interesting, every interesting piece of behavior, anything that's off, any line that's missing.

Combine your knobs - take the list you've compiled and piece them together, find each individual knob that can fit with another to create exploits.

Not getting your incentives right by charging all the money for an audit upfront no matter how many findings and vulnerabilities are uncovered can prevent you from getting into the attacker's mindset.

Not believing that vulnerabilities exist in the code you're auditing and subconsciously agreeing with the developers can prevent you from getting into the attacker's mindset.

GMX has a two-step execution system where you create an order and then a keeper comes in and executes your order in two independent transactions to avoid front-running attack vectors.

If you can delay the execution of the keeper's transaction, you can get a risk-free trade by executing your order at a later block with the prices from the block you created the order.

Discovered that if a limit order reverts with an empty position error, it will be retried, allowing potential to delay execution.

Combined the price execution logic with the empty position error delay to construct an exploit for a risk-free trade.

Pieced together multiple code intricacies to toggle off the empty position error at a favorable time and execute the risk-free trade.

Transcripts

play00:00

getting into the attacker's mindset is

play00:03

probably the number one thing that you

play00:06

can do to immediately boost the amount

play00:09

of findings vulnerabilities and bugs

play00:11

that you uncover you need to start

play00:13

thinking offensively to uncover the bugs

play00:16

and vulnerabilities that the developers

play00:19

who are thinking defensively did not

play00:21

uncover in this video we're going to

play00:23

cover exactly the four things that you

play00:26

need to do to get into the attacker's

play00:29

mindset and then we're going to cover

play00:30

two things that will prevent you from

play00:33

getting into the attacker's mindset and

play00:35

then we're going to cover a particular

play00:37

finding that required me to really get

play00:41

into the attacker's mindset and pull a

play00:43

few knobs in order to pull it off and

play00:46

actually have a valid finding and so

play00:48

this way you're going to see exactly how

play00:50

the attacker's mindset comes into play

play00:52

and how it is actually applied in

play00:55

practice all right but first of all who

play00:56

am I and why should you listen to me

play00:59

talk about out the attacker's mindset

play01:02

well my name is Owen and over a year and

play01:04

a half ago I founded Guardian Audits and

play01:06

ever since then we've uncovered dozens

play01:09

and dozens of critical and high

play01:11

vulnerabilities in over a hundred

play01:14

different smart contracts and over a

play01:17

thousand different hours spent auditing

play01:19

smart contracts and so my goal is to

play01:22

take down everything that I've learned

play01:24

along the way and give it to you so that

play01:27

you can ultimately become a much better

play01:30

smart contract auditor than I ever could

play01:32

be in a fraction of the time alright so

play01:34

without further Ado let's get right into

play01:37

the attacker's mindset

play01:43

[Music]

play01:46

okay so we are talking about the

play01:49

attacker's mindset but first of all what

play01:51

really is it and why will it benefit us

play01:54

the attacker's mindset is very simply

play01:56

boiled down just a way of looking at a

play01:59

code base where you're trying to break

play02:02

as many things as possible and there's

play02:04

an optimal way to do this this is of

play02:07

course in direct opposition to the sort

play02:10

of mindset of a defender or a developer

play02:14

where they're looking to secure a

play02:16

protocol and essentially verify that it

play02:19

is indeed secure at its core what this

play02:21

boils down to is looking to disprove

play02:24

rather than prove and of course the

play02:26

attacker's mindset is going to benefit

play02:28

us because if we set out looking to

play02:30

disprove the security of a protocol by

play02:34

finding a valid vulnerability in it then

play02:36

we're going to be a lot more likely to

play02:38

uncover that vulnerability than if we

play02:41

were to set out to verify or essentially

play02:44

convince ourselves of the security of

play02:47

the protocol okay so now let's discuss

play02:49

the four key steps to get into the

play02:52

attacker's mindset and then we'll

play02:54

discuss two critical mistakes that are

play02:57

pulling you right out of the attacker's

play02:59

mindset okay so first of all of course

play03:01

is to incentivize yourself accordingly

play03:04

you want to ensure that before you even

play03:06

go into the audit you have the correct

play03:09

incentives that are setting you up for

play03:11

Success where you really want to find

play03:15

vulnerabilities what does this mean this

play03:18

means that you structure the way that

play03:20

you get paid or rewarded or however

play03:23

you're being compensated for this audit

play03:25

by the amount of vulnerabilities and the

play03:27

criticality of vulnerabilities that you

play03:29

uncover and so obviously if you're

play03:31

competing in a contest or maybe if

play03:34

you're charging per vulnerability found

play03:36

of course this is a fantastic way to

play03:39

correctly incentivize yourself

play03:41

and sort of change the structure of your

play03:44

mind and make you think by default like

play03:47

an attacker because you are literally

play03:49

getting compensated like an attacker

play03:51

would be okay then second of all after

play03:53

we've gotten the incentives right we've

play03:55

set the Baseline for how a good

play03:58

incentivized audit can occur now we just

play04:01

have to personally believe that there

play04:03

are bugs and vulnerabilities in the code

play04:06

base itself of course if an attacker did

play04:08

not believe that there were

play04:09

vulnerabilities in a code base they

play04:11

would move on to a new one in order to

play04:13

find as many bugs and vulnerabilities as

play04:16

possible you simply have to be convinced

play04:18

that many bugs and vulnerabilities exist

play04:21

in the first place okay and then the

play04:23

third step is to collect knobs as we're

play04:26

going through the code base so as you're

play04:28

going through the code base and you're

play04:29

getting that initial context you want to

play04:31

note down everything interesting every

play04:33

interesting piece of behavior anything

play04:35

that's off any line that's missing you

play04:37

want to note that down and save it for

play04:39

later because as you're going to see

play04:41

things like this can be combined with

play04:43

other knobs in order to yield really

play04:46

unique and critical attack vectors which

play04:49

leads us into the fourth step which is

play04:51

of course to combine your knobs now what

play04:54

you have to do is take this list that

play04:56

you've compiled and piece them together

play04:58

find each individual knob that can fit

play05:01

with another to create one of these

play05:03

really cool puzzle piece sort of

play05:06

exploits each of these knobs is going to

play05:09

be a Lego brick that you can use to

play05:11

construct a more complicated exploit all

play05:14

right now that we've discussed that

play05:15

let's talk about two key mistakes that

play05:18

you might be making right now that are

play05:20

preventing you from getting into the

play05:22

attacker's mindset and these are really

play05:24

the flipped version of the earlier steps

play05:29

and so first of all of course if you're

play05:31

not getting your incentives right you're

play05:33

going to be starting off on a bad foot

play05:36

and setting yourself up for failure so

play05:39

what does this mean this looks like

play05:41

charging all of the money for an audit

play05:43

up front no matter how many findings and

play05:46

vulnerabilities are uncovered so if you

play05:48

think about the Baseline incentive here

play05:50

you are purely incentivized to spend as

play05:53

little time on the audit as possible and

play05:55

your mentality is going to be to

play05:56

convince yourself of the security of the

play06:00

protocol you're going to subconsciously

play06:02

want to believe after a certain amount

play06:04

of time that you've spent on the

play06:05

protocol that the protocol is indeed

play06:08

secure you might not notice this

play06:10

consciously affecting your actions but

play06:12

this is exactly what makes the

play06:14

difference between spending an extra day

play06:16

on a piece of functionality that's

play06:19

really involved just to get that last

play06:21

little tiny knob that ends up being a

play06:25

full-blown critical exploit and versus

play06:28

if you weren't incentivized to find that

play06:30

little tiny knob and get that critical

play06:32

exploit you might end up subconsciously

play06:35

convincing yourself that that area of

play06:37

functionality is actually secure and

play06:39

there's no need to spend many many hours

play06:42

digging into it okay and then second of

play06:44

all is of course to not believe that

play06:46

vulnerabilities exist of course we've

play06:48

talked a lot about this but I cannot

play06:51

iterate this enough you have to believe

play06:53

that there are vulnerabilities in the

play06:55

code you're auditing you have to

play06:57

distrust the developers always otherwise

play07:00

you start to get into the headspace of

play07:03

verifying rather than attacking and you

play07:05

start to agree with the developers if

play07:07

you find yourself subconsciously

play07:09

agreeing with the developers immediately

play07:12

turn that on his head and start to

play07:13

question everything from the ground up

play07:16

okay now that we've explored what

play07:18

exactly the attacker's mindset is let's

play07:21

have a look at it in practice and see

play07:24

how it can yield some really interesting

play07:26

unique exploits that are even missed in

play07:28

audit contests okay so before we hop

play07:31

into this particular finding and walk

play07:33

through it walk through the steps that

play07:35

it took me to actually come across this

play07:38

finding we need to get some context on

play07:41

the code base for so this is of course

play07:43

from the GMX V2 code base from an

play07:46

earlier audit as GMX was early on in the

play07:49

development stage of the protocol before

play07:52

it's released now if you want a full

play07:55

Deep dive and breakdown of the GMX V2

play07:58

code base and to really get enough

play08:01

context and introduction to fully

play08:03

understand this finding go ahead and

play08:05

watch my intro to GMX V2 video on this

play08:09

channel but to give some really really

play08:11

quick high-level context just enough so

play08:14

you can get the gist of this finding as

play08:16

we go through it GMX is a perpetuals

play08:19

exchange Traders can open positions and

play08:22

they use orders to do that you can use

play08:24

orders to increase positions and

play08:26

decrease positions and you have all

play08:28

sorts of market and limit orders as well

play08:30

as stop losses and importantly GMX has a

play08:34

two-step execution system where you

play08:36

create an order it's it's sort of like a

play08:39

request to have an order executed and

play08:41

then a keeper comes in and executes your

play08:44

order and these are in two independent

play08:46

transactions and this is essentially

play08:48

done to avoid a lot of front-running

play08:51

attack vectors on the exchange so the

play08:54

first knob here actually arises from

play08:56

that two-step execution process the

play08:59

keeper will essentially provide prices

play09:02

from the Block in which you created your

play09:06

order in order to execute your order so

play09:09

what does this mean this means that

play09:10

essentially there's a unique attack

play09:12

Vector here where if I can delay the

play09:15

execution of the keepers transaction I

play09:18

can get a risk free trade let's have a

play09:21

look at exactly what this looks like

play09:24

right so if we have a timeline here and

play09:27

let's say I simply created a straight up

play09:30

Market order at this time here let's say

play09:33

this is block 100 over here and then I'm

play09:36

able to delay the Keeper's execution of

play09:39

my order until this block over here

play09:41

let's say it's block 110 essentially I

play09:45

can execute have this order executed at

play09:48

this time block 110 with the prices from

play09:51

block 100. so in Block 100 the price of

play09:54

ethereum might be a thousand dollars in

play09:57

Block 110 the price of ethereum might be

play09:59

eleven hundred dollars and so I could

play10:02

see that the price of ethereum currently

play10:04

in Block 110 is eleven hundred dollars

play10:07

and then make a risk-free trade where I

play10:10

say oh yes I would like my order that

play10:13

was submitted here to be executed only

play10:15

now at block 110 and end up getting an

play10:19

Arbitrage of a hundred dollars per ether

play10:22

risk free right I have the knowledge

play10:24

that ether has appreciated 100 since

play10:27

these prices here in Block 100 so that

play10:30

is the most simple version of and a

play10:33

risk-free trade that could occur and

play10:35

that's essentially the first knob that

play10:38

we're going to use in our list of knobs

play10:40

right so as I'm going through the code

play10:43

base this is something that I I just

play10:45

know as a class of vulnerabilities that

play10:48

exists on the exchange and so it's in my

play10:51

list of knobs that I have to twist as

play10:54

I'm going through the code base so let's

play10:56

go into the first knob that I discovered

play10:59

that essentially begetted what

play11:02

ultimately became this vulnerability so

play11:05

first of all I'm going to notice that

play11:07

the empty position error is something

play11:10

that gets reverted on so what does this

play11:13

mean this means that when we revert here

play11:16

this order is going to be retried

play11:19

instead of ultimately Frozen or canceled

play11:23

if it were a market order or something

play11:25

like this so if we have a limit order

play11:27

for example and it reverts with an empty

play11:30

position error it's going to be retried

play11:32

what does this mean this means that we

play11:36

can delay the execution here so I'm

play11:39

going to note that if I can get get a

play11:42

limit order to revert with the empty

play11:44

position error then I can delay the

play11:47

execution of the Keeper's transaction so

play11:50

I know it can delay it but I need

play11:52

something to keep the prices that the

play11:56

order is going to execute with outdated

play11:58

so if the keeper has to keep making new

play12:01

basically executions so the way the

play12:03

keeper will work is they'll see that

play12:06

this would revert with this error by

play12:09

basically simulating this and they will

play12:11

not execute this transaction until they

play12:14

see that it doesn't revert and it

play12:16

successfully goes through so how do I

play12:18

get it such that the keeper will use

play12:21

outdated prices when all of a sudden

play12:23

they see that this no longer reverts

play12:26

with an empty position error well

play12:28

particularly for a limit order we can

play12:30

see that in the set exact order price

play12:34

function which is used to essentially

play12:36

set the prices that are going to be used

play12:38

to execute this order for a limit

play12:40

decreased order we can see that we're

play12:44

going to end up validating that the

play12:47

price is ascending right so it is not a

play12:50

limit increase it is not a stop loss

play12:53

this is a limit decrease and let's say

play12:56

it is for a long to make things simple

play12:57

so we're going to validate that price is

play13:00

ascending and so essentially this is

play13:02

going to take the earlier price in the

play13:04

range of prices that are provided by the

play13:06

Oracle and the later price and the range

play13:09

of prices provided by the Oracle and

play13:11

assert essentially that price is

play13:14

validating over this range and this

play13:17

range must encapsulate my trigger price

play13:19

so if we draw this out and we have a

play13:21

look at what this looks like on a a

play13:25

price graph so we'll say this is my

play13:28

trigger price now what has to happen for

play13:31

my limit decrease essentially like a

play13:33

take profit to execute is price has to

play13:37

be ascending right so now any arbitrary

play13:41

range here is hypothetically a valid

play13:45

range to execute this limit decrease

play13:48

over as long as it's including my

play13:50

trigger price so now I can see that if I

play13:53

combine this price execution logic with

play13:56

the fact that you could potentially

play13:58

delay this execution with an empty

play14:01

position error then I could potentially

play14:04

get some sort of benefit from a

play14:06

risk-free trade if price did something

play14:09

like this so let's talk about this if I

play14:13

have my limit decrease order waiting to

play14:15

get executed and then price enters this

play14:18

range here but I do not allow it to be

play14:21

executed by reverting with the empty

play14:23

position error then essentially price is

play14:26

going to continue time is going to

play14:27

continue on we're still reverting with

play14:29

the empty position error the keeper is

play14:31

not trying to execute this yet as they

play14:33

see that it's reverting until we see

play14:35

price goes down down and now let's say

play14:39

we're at this point here every time

play14:42

before this we were reverting with the

play14:44

empty position error now you'll notice

play14:46

something if we look at the price option

play14:48

here there has never been a valid price

play14:51

range to execute this over after you

play14:55

know

play14:56

so you know something like this point

play14:58

here now there's no valid range after

play15:01

this point where you could execute this

play15:04

there's no range that is increasing that

play15:07

includes our trigger price and so now if

play15:10

all of a sudden we're to stop reverting

play15:12

with an empty position error and allow

play15:14

the order to be executed at this point

play15:16

in time now we're going to be forcing

play15:19

the keeper to use prices from in this

play15:22

range and so of course this is a classic

play15:24

risk-free trade I can execute an order

play15:27

at this time with knowledge of what the

play15:30

current price is only I'm executing it

play15:33

with old prices so I'll get a price

play15:37

somewhere in this range here and of

play15:39

course I can modify my acceptable price

play15:41

to get any particular price that I might

play15:44

want okay so we have the means to make a

play15:47

risk-free trade happen but the question

play15:49

is where can we get that empty position

play15:51

error to end up delaying this and how

play15:55

can we talk toggle it such that if I

play15:57

decide at this point in time that I want

play15:59

this order to go through and the

play16:01

risk-free trade to be realized then How

play16:04

Can I toggle that empty position error

play16:06

off and allow the order to go through so

play16:09

these are things that I'm immediately

play16:10

looking for now in the attacker's

play16:13

mindset right I am looking to

play16:15

maliciously really maliciously find how

play16:18

I can make this sort of risk-free trade

play16:20

happen so I'll notice one key thing when

play16:23

I'm looking at the decrease position

play16:25

this if statement here if our position

play16:29

was closed of course we remove the

play16:31

position from the order store and we

play16:33

don't do any extra validation and that's

play16:34

if the size in USD or the size and

play16:37

tokens are zero however if there's a

play16:39

position left over then we're going to

play16:41

go ahead and do this critical check here

play16:44

which is to validate position and if we

play16:46

click into this we'll see that we

play16:48

validate non-empty position and which

play16:50

revert do you think that gives it gives

play16:52

the empty position revert and so we have

play16:55

three different knobs here that we can

play16:58

twist in order to get the empty position

play17:00

revert and cause this initial sort of

play17:03

delay so now here's my goal after

play17:05

realizing that I can get the empty

play17:07

position revert when a position is left

play17:10

with size so we must have size in USD

play17:13

and we must have size and tokens enabled

play17:16

to to go into this else branch and

play17:18

ultimately do this validation now I know

play17:21

that this cannot be zero and I know that

play17:22

this cannot be zero in this case so

play17:24

we're left with one option here the

play17:27

collateral amount we need to be able to

play17:29

manipulate the collateral amount to be

play17:31

zero here while the size and tokens and

play17:34

the size in USD are non-zero and then

play17:36

once we're able to get into that state

play17:38

we need to be able to retroactively

play17:40

toggle that state off without touching

play17:42

the order in order to ensure that this

play17:45

can be executed retroactively so what I

play17:48

first notice is if I go into the process

play17:52

collateral function if we scroll down

play17:55

here there's actually no check that I

play17:59

can't just set the remaining collateral

play18:01

amount to zero I'm not going to go

play18:02

through this whole 300 line function but

play18:05

you can essentially take my word for it

play18:08

that we wrote a POC and it's actually

play18:10

true you can essentially have an initial

play18:14

collateral Delta amount that will allow

play18:16

you to set the remaining collateral

play18:18

amount to zero and so that's exactly the

play18:21

knob that we needed to make this attack

play18:24

work to give us some Ray of Hope so now

play18:27

after processing collateral and getting

play18:30

these values we set the remaining

play18:32

collateral to zero right so this

play18:33

remaining collateral amount is now zero

play18:35

from our initial collateral Delta which

play18:38

is essentially the amount of collateral

play18:40

we want to remove using our order so

play18:42

this gets set to zero on the position

play18:44

and then when we do this check here

play18:48

we're going to have some size left over

play18:50

so we go here and we validate and we get

play18:53

the empty position error so we're

play18:55

starting to piece together the Lego

play18:58

building blocks and we've almost

play19:00

finished our Masterpiece of an attack

play19:03

here but we still need some more Lego

play19:05

bricks to come in and help us out we

play19:07

have successfully found a way to

play19:09

essentially make an order revert And

play19:12

Delay that success here's what we've

play19:14

done if we can use this as a timeline of

play19:17

price action here's what we'll use as a

play19:19

timeline of order execution

play19:22

let's say before this we have a position

play19:25

that has let's say we have two thousand

play19:28

two thousand USD of size so this is the

play19:31

size of our position and let's say we

play19:34

have just 10 collateral tokens 10

play19:37

collateral tokens of collateral maybe

play19:40

this is ether let's say it's ether it

play19:42

doesn't matter right so we have that and

play19:44

then we have an order in the store and

play19:46

let's say the order is for 10 collateral

play19:49

tokens and only 1500 size

play19:53

so now what we've got so far is we see

play19:56

that if this order is to be executed at

play19:59

this point in time right so let's say

play20:02

you know the order would be executed

play20:03

tried it would be normally attempted to

play20:06

be executed at this point in time what

play20:08

would happen is this order this 10

play20:11

initial collateral Delta amount would

play20:14

negate the collateral on the position we

play20:16

would end up with zero collateral tokens

play20:19

we would end up reverting on this

play20:21

validate position function when we check

play20:24

validate non-empty position the

play20:26

remaining collateral Mount is zero and

play20:28

we revert with the empty position the

play20:31

empty position up in the older order

play20:33

Handler is ultimately causing a top

play20:36

level revert which means that the keeper

play20:39

will not execute the order so as long as

play20:42

this order lives in this collateral

play20:45

amount here is 10 and the collateral the

play20:47

initial collateral Delta amount on the

play20:49

order is 10 then this will continue to

play20:52

revert revert revert until let's say at

play20:55

this point right here now we see that

play20:58

prices moved in a favorable Direction

play21:01

meaning that if we were to be executed

play21:03

with prices from over here on the left

play21:06

at this point in time on the right

play21:09

we would make risk-free money we would

play21:11

be able to buy ether at cheaper than the

play21:15

the market rate or you'd sell ether at a

play21:18

higher price than the market rate you

play21:20

know something like this so now exactly

play21:22

what we need is the knob to twist to be

play21:25

able to toggle this off and be able to

play21:28

make the empty position error not get

play21:30

raised and so after scanning and looking

play21:33

and looking for exactly that thing to

play21:36

twist we can find that exactly here on

play21:40

this if statement right here this is the

play21:43

last Lego Brick of our attack Vector

play21:47

what is this doing right here we're

play21:49

seeing if the size Delta USD on the

play21:52

order is the same as the size and USD of

play21:55

the position

play21:55

then what we're doing is we're setting

play21:59

the initial collateral Delta amount to

play22:01

zero ladies and gentlemen this is

play22:03

everything we need this is exactly the

play22:05

toggle that we need so how are we going

play22:07

to achieve it so let's remember this one

play22:10

case this is literally the case we need

play22:12

if the order size is the same as the

play22:15

position size then we can toggle our

play22:19

risk-free trade so what can we do we can

play22:22

decrease the position size to match the

play22:27

order size so we'll see the position

play22:28

size is 2000 USD the order size is 1500

play22:32

USD at this point in time when we want

play22:34

to toggle it normal

play22:36

Market decrease to decrease our position

play22:40

by 500

play22:43

USD so our position now becomes at this

play22:48

point in time it is now 1500 USD size

play22:52

and 10 tokens in collateral so what does

play22:57

this match up with it matches up with

play22:58

perfectly the 1500 from our order over

play23:03

here so we're going to hit this exact if

play23:05

statement right here we're going to set

play23:07

the initial collateral Delta amount to

play23:09

zero and in fact it doesn't even matter

play23:13

because we're going to go into this if

play23:17

statement here and so we're not even

play23:19

going to have any position left over

play23:20

because ultimately the size in USD is

play23:23

now going to be equal to zero and we're

play23:25

not even going to enter the branch where

play23:27

it's possible to throw the empty

play23:29

position error okay so now ladies and

play23:31

gentlemen this is literally the entire

play23:34

exploit this is everything we need to

play23:37

pull off a critical risk-free trade on

play23:40

the exchange so I hope that wasn't too

play23:43

too in depth to understand how exactly

play23:46

we go through and piece each of these

play23:48

individual Lego bricks together I

play23:50

recommend you go through and watch this

play23:52

a few more times in order to fully

play23:54

understand the attack vector and really

play23:56

try to notice how we're going through

play23:59

and as we uncover bits we just keep them

play24:02

in our backlog until we match them up

play24:04

with something else that's potentially

play24:06

related and notice overall how we're

play24:08

really being malicious about this and

play24:10

we're really going for and trying to

play24:12

break things intently this is the core

play24:15

of the attacker's mindset okay so that

play24:18

covers everything to do with the

play24:20

attacker's mindset everything I know

play24:22

about getting into the attacker's

play24:23

mindset and finding those really

play24:25

interesting vulnerabilities I hope that

play24:27

this helps you find something really

play24:29

interesting and if you learned anything

play24:31

new go ahead and just pop it in the

play24:33

comment section below alright and of

play24:35

course if you're looking to grow as an

play24:37

auditor and connect with a community of

play24:39

growing aspiring Auditors to get to the

play24:42

next level of auditing skills or

play24:44

whatever and go check out lab.guardian

play24:47

audits.com where you can join over 500

play24:50

smart contract security Auditors and

play24:53

learn from everybody in the community

play24:55

share your thoughts get experiences from

play24:58

others and even team up with others to

play25:01

participate in team Audits and get

play25:03

rewarded for findings that you uncover

play25:06

and of course if you're not 100

play25:08

confident in your web 3 auditing

play25:11

abilities I collaborated with a number

play25:14

of top tier Security Professionals in

play25:17

the field to bring you exactly the most

play25:20

thorough training on Smart contract

play25:23

security this training will literally

play25:25

take you from just understanding

play25:27

solidity to a full-blown solidity

play25:30

auditing Chad as efficiently as possible

play25:33

if you are looking to go the paid route

play25:36

then this is absolutely in my opinion

play25:39

the best paid resource hands down out

play25:42

there on smart contract auditing so if

play25:45

you are indeed looking to go the paid

play25:47

route then of course you can take fifty

play25:49

dollars off in the link in the

play25:51

description below alright guys that is

play25:53

all for this time I can't wait to see

play25:55

you in the next one

play25:59

foreign

Rate This

5.0 / 5 (0 votes)

相关标签
Smart ContractsSecurity AuditingHacking MindsetWeb3 SecurityVulnerability HuntingRisk AnalysisBlockchain TechnologyEthical HackingPenetration TestingDecentralized Applications
您是否需要英文摘要?