HACKED! How a Buffer Overflow Exploit works, plus Code Red!

Dave's Garage
20 Aug 202125:49

Summary

TLDRThe video script narrates the story of the 'Code Red' computer worm, a notorious virus released in 2001 that exploited a buffer overrun vulnerability in Microsoft's IIS web servers. It details the worm's rapid spread, causing widespread internet disruption and targeting the White House. The script also includes an educational segment on buffer overruns, demonstrating how such exploits work with a C program example. It concludes with a discussion on security measures like DEP, stack canaries, and the importance of patching systems, highlighting the ongoing need for vigilance against cyber threats.

Takeaways

  • ๐Ÿ—“๏ธ The Code Red virus was released close to the 9/11 attacks, specifically on a Friday the 13th, causing a rapid spread of infection.
  • ๐Ÿ“‰ The virus led to a quick societal impact with police, ATMs, and airline reservation systems being affected within hours.
  • ๐Ÿ’ป The infection targeted a vulnerability in the Internet Information Services (IIS), affecting large web servers and causing parts of the internet to disappear.
  • ๐Ÿ”„ Code Red had a cyclical pattern of infection, attack, and waiting, with specific actions on different days of the month.
  • ๐Ÿ” The worm was programmed to launch a massive DDoS attack on the White House, specifically targeting 1600 Pennsylvania Avenue.
  • ๐Ÿ› ๏ธ The buffer overrun exploit, a common security issue in software, was the technical root of the Code Red worm's success.
  • ๐Ÿ‘จโ€๐Ÿ’ป Dave, a retired Microsoft engineer, provides an in-depth explanation of how buffer overflows work and demonstrates writing an exploit in C.
  • ๐Ÿ›ก๏ธ Modern defenses against such exploits include safer programming practices, Data Execution Prevention (DEP), and stack canaries.
  • ๐Ÿ”„ Code Red had a self-regulation mechanism; the presence of a 'no worm' file on the C: drive would halt the worm's activity.
  • ๐ŸŒ The incident highlighted the need for proactive cybersecurity measures, leading to more frequent updates and improved security in software development.

Q & A

  • When was the Code Red worm released?

    -The Code Red worm was released just two months before the 9/11 attacks, on a Friday the 13th.

  • How quickly did the Code Red worm spread initially?

    -The virus was highly contagious, infecting more than 75,000 systems within 10 minutes of its release.

  • What was the impact of the Code Red worm on emergency services?

    -Within hours of the worm's release, the police in the Seattle area stopped responding to 9-1-1 calls due to the infection.

  • How did the Code Red worm affect financial systems?

    -ATMs around the nation and then globally stopped dispensing cash due to the worm's infection.

  • What was the primary target of the Code Red worm's DDoS attack?

    -The worm was programmed to launch a distributed denial of service (DDoS) attack aimed at the White House, located at 1600 Pennsylvania Avenue.

  • What was the significance of the 'ghost file' in the Code Red worm?

    -The 'ghost file' was a file that the worm searched for on web servers; the request for this non-existent file would trigger the worm's attack vector on vulnerable systems.

  • What security update was available prior to the Code Red worm that could have mitigated its impact?

    -An update named MS01-33 was available, which was labeled by Microsoft as critical and aimed to fix the buffer overrun vulnerability exploited by the worm.

  • How did the Code Red worm differentiate between infected and non-infected machines?

    -The worm kept track of infected machines and would periodically re-infect them, ensuring that even if the worm was removed, the underlying vulnerability would likely lead to reinfection.

  • What was the 'no worm' backdoor discovered in the Code Red worm?

    -A file named 'no worm' on the C drive could be created to shut down the worm's attacks, providing system administrators with time to remove the malware and update their servers.

  • What is a buffer overrun and how does it relate to the Code Red worm?

    -A buffer overrun is a classic security vulnerability in software where a program allows more data to be written to a buffer than it can hold, overwriting adjacent memory. The Code Red worm exploited a buffer overrun in the IIS web server extension to propagate itself.

  • What modern defenses are available against buffer overrun attacks?

    -Modern defenses include the use of safe string functions, data execution prevention (DEP), stack canaries, and guard pages that prevent or detect buffer overruns before they can be exploited.

Outlines

00:00

๐Ÿ˜ท The Emergence of Code Red Worm

The script begins with a dramatic recount of the release of the Code Red worm, a malicious computer virus, which occurred just two months prior to the 9/11 attacks. The virus spread rapidly, infecting over 75,000 systems within 10 minutes and causing significant disruption, including the failure of ATMs and airline reservation systems. The virus was programmed to infect as many systems as possible, rendering infected machines useless and leaving a cryptic message claiming Chinese responsibility. The script also introduces the concept of a 'zombie army' of infected machines that were programmed to launch a coordinated attack on the White House. The narrative highlights the early detection of suspicious activity by a sharp-eyed administrator and the subsequent involvement of security researchers from a company called ei Digital Security. The virus, named 'Code Red' after a caffeinated drink consumed during late-night research, is described as one of the most destructive computer viruses, causing over a billion dollars in damages.

05:00

๐Ÿ›ก๏ธ The Impact and Propagation of Code Red

Paragraph 2 delves into the aftermath of the Code Red worm's spread, detailing how the infection led to an estimated 650,000 unique server IPs being compromised. The worm was characterized by its aggressive nature, with each server capable of spawning 300 threats to infect other systems. The script explains the worm's ability to track and re-infect machines, highlighting the challenges in eradicating the threat. It also discusses the worm's three-phase operation: searching for new hosts, launching denial-of-service attacks, and resting while seeking new infections. The explanation of a buffer overrun attack is introduced, setting the stage for a deeper technical exploration in subsequent paragraphs.

10:03

๐Ÿ’ป Buffer Overruns and Memory Layout

Paragraph 3 provides a technical deep dive into the concept of buffer overruns, using a simplified example to illustrate how extra characters entered into a web page can overwrite adjacent memory areas, potentially allowing for the execution of malicious code. The script explains the memory layout of a process, detailing the stack and heap, and how a buffer overrun can lead to the execution of injected code. It uses a metaphor of a cafeteria tray stack to explain the last-in, first-out nature of the stack, and how function calls and returns are managed through the stack. The paragraph concludes with a demonstration of how to exploit a buffer overrun in a simple C program, showing how to overwrite the return address to execute a predefined 'malicious payload' function.

15:05

๐Ÿ”’ Defense Mechanisms Against Buffer Overruns

Paragraph 4 discusses various defense mechanisms against buffer overrun attacks. It emphasizes the importance of programmers avoiding unsafe string functions and using modern alternatives. The script also covers protective measures like Data Execution Prevention (DEP) on Windows, which prevents code execution from data segments, and the use of stack canaries to detect buffer overruns. Additionally, it mentions the use of guard pages by the memory management unit (MMU) to prevent unauthorized memory writes. The paragraph concludes with a reflection on the practical limits and performance impacts of these defense mechanisms.

20:05

๐Ÿ”„ Code Red's Exploitation and Mitigation

Paragraph 5 returns to the narrative of the Code Red worm, explaining how it exploited a buffer overrun in the IIS web server extension. It details the worm's propagation mechanism and the discovery of a 'backdoor' file named 'no worm' that could halt the infection. The script also touches on the broader implications for cybersecurity, highlighting the shift towards more proactive security measures and the importance of keeping systems up to date. It concludes with a call to action for viewers to engage with the content and support the channel.

25:05

๐ŸŽจ Behind-the-Scenes and Bloopers

Paragraph 6 offers a behind-the-scenes look at the video production process, including some bloopers and outtakes. It captures the presenter's informal and humorous moments as he re-records parts of the script to ensure clarity and accuracy. This paragraph provides a light-hearted conclusion to the video script, showing the human side of content creation.

Mindmap

Keywords

๐Ÿ’กBuffer overrun

A buffer overrun, also known as buffer overflow, is a type of vulnerability that occurs when a program writes data beyond the boundaries of a fixed-length buffer. This can corrupt or overwrite adjacent memory, potentially allowing an attacker to execute arbitrary code. In the video, the 'Code Red' worm exploited a buffer overrun in the IIS web server extension, allowing it to spread rapidly and cause significant damage.

๐Ÿ’กCode Red

Code Red refers to a specific computer worm that propagated through a buffer overrun vulnerability in the IIS web server software. The worm infected servers, rendering them part of a 'zombie army' aimed at launching a coordinated denial-of-service attack. The video details how 'Code Red' was programmed to infect as many machines as possible and how it was eventually mitigated.

๐Ÿ’กDistributed Denial of Service (DDoS) attack

A DDoS attack is an attempt to make an online service unavailable by overwhelming it with traffic from multiple sources. In the context of the video, 'Code Red' was programmed to initiate a massive DDoS attack targeting the White House's servers, demonstrating the worm's potential to disrupt critical infrastructure.

๐Ÿ’กVulnerability

A vulnerability in the context of the video refers to a weakness in a system, such as the IIS web server, that can be exploited by malicious software like the 'Code Red' worm. The video explains how the worm targeted a specific vulnerability to propagate and cause harm.

๐Ÿ’กExploit

An exploit is a piece of software, a chunk of data, or a sequence of commands that takes advantage of a bug or vulnerability in order to cause unintended or unanticipated behavior to occur on computer software or hardware. The video describes how 'Code Red' exploited a buffer overrun in IIS to spread and execute its payload.

๐Ÿ’กPayload

In the context of malware, a payload is the part of the code that carries out the malicious intent, such as causing damage or stealing data. The video explains how 'Code Red' contained a payload that turned infected machines into part of a botnet aimed at a DDoS attack.

๐Ÿ’กStack

The stack is a region of memory that a program uses to store temporary data, such as the return addresses of functions and local variables. The video discusses how buffer overruns can overwrite the stack, allowing attackers to change the return address and execute arbitrary code.

๐Ÿ’กHeap

The heap is a region of memory where a program can dynamically allocate and deallocate memory. While the video primarily focuses on stack-based buffer overruns, it also mentions heap attacks as a more complex method of exploiting buffer overruns.

๐Ÿ’กCanary

In the context of the video, a canary is a security feature used to detect buffer overruns. It involves placing a 'canary' value on the stack before a buffer. If the buffer is overrun, the canary value will be overwritten, signaling that a security breach has occurred. The term originates from the practice of using canaries in coal mines to detect toxic gases.

๐Ÿ’กPatch

A patch is a piece of software designed to update, fix, or improve a computer program. The video discusses the importance of applying patches to software to fix vulnerabilities, such as the one exploited by 'Code Red', and prevent future attacks.

๐Ÿ’กZero-day exploit

A zero-day exploit is a cyber attack that occurs on the same day a vulnerability is discovered, where the software provider has 'zero days' to fix it. The video implies that 'Code Red' may have been a zero-day exploit, as it took advantage of a vulnerability before a patch was widely applied.

Highlights

The Code Red virus was released just two months before the 9/11 attacks, initially infecting a handful but quickly spreading to over 75,000 systems within 10 minutes.

The virus's contagion led to a halt in police response to 9-1-1 calls in Seattle and globally affected ATMs and airline reservation systems.

Once infected, machines joined a 'zombie army' aimed at infecting others silently, with the only symptom being a cryptic message claiming Chinese responsibility.

The virus was programmed to attack the White House's servers in a coordinated DDoS attack, using the power of hijacked servers worldwide.

A new variant of the virus emerged a week later, spreading more rapidly and demonstrating the virus's ability to mutate.

The White House's astute administrator noticed suspicious activity early on, identifying thousands of requests from random servers.

Ei Digital Security's high-level security researchers were alerted to the serious situation, recognizing the need for immediate action.

The virus, named 'Code Red' after the caffeinated drink that fueled the researchers' efforts, caused over a billion dollars in damages.

Dave, a retired operating systems engineer, explains the buffer overrun bug that made Code Red possible, common in C and C++ languages.

Code Red exploited a vulnerability in Internet Information Services (IIS), affecting large data centers and corporate offices' web servers.

Microsoft had released a critical update, MS01-33, a month before the virus hit, but many systems remained unpatched.

The National Infrastructure Protection Center (NIPC) estimated over 650,000 unique server IPs were infected by Code Red.

Code Red's aggressive nature involved spawning 300 threats per server to infect others, with a higher rate directed at Chinese IPs.

The worm operated in three phases: infecting new hosts, launching DDoS attacks, and resting while seeking new machines to infect.

A buffer overrun is explained as the overflow of data into adjacent memory locations, potentially overwriting critical memory addresses.

Dave demonstrates writing a buffer exploit in C, showing how to hack the stack and inject malicious code.

Modern systems employ defenses like safe string functions, Data Execution Prevention (DEP), and stack canaries to protect against buffer overruns.

Code Red's propagation mechanism involved exploiting a buffer overrun in the IIS web server extension, demonstrating the power of such exploits.

The discovery of a 'backdoor' in Code Red, where the presence of a file named 'no worm' would halt the infection, provided a simple solution.

The story of Code Red emphasizes the importance of proactive security measures and the evolution of programming practices to prevent such exploits.

Transcripts

play00:00

[Music]

play00:05

the infection was released into the wild

play00:07

just two months before the 9 11 attacks

play00:10

on a friday the 13th

play00:12

initially just a handful were infected

play00:14

but the virus proved so contagious that

play00:16

within 10 minutes more than 75 000 were

play00:19

infected within hours the police would

play00:21

stop responding to 9-1-1 calls in the

play00:24

seattle area and shortly after that atms

play00:26

scattered around the nation and then

play00:28

around the globe would soon stop

play00:30

dispensing cash

play00:32

airline reservation systems were

play00:33

overwhelmed that in some quarters panic

play00:36

began to erupt

play00:37

once infected each new machine would

play00:39

join a zombie army silently striving to

play00:41

infect as many others as possible

play00:44

once the worm had successfully spawned

play00:46

itself the host would become useless

play00:48

effectively rolling over and going to

play00:50

sleep

play00:51

the only symptom of infection was a

play00:52

cryptic message claiming chinese

play00:54

responsibility but the silent countdown

play00:57

had already begun

play00:58

the virus had been programmed to enter a

play01:01

cycle of infection attack and waiting

play01:04

when the clock expired the attack would

play01:06

begin

play01:07

it would be a massive attack initiated

play01:09

by hundreds of thousands of the world's

play01:10

most powerful servers hijacked from all

play01:13

corners of the globe

play01:14

each bot in the army would use all of

play01:16

its available network computing

play01:17

resources in a coordinated distributed

play01:19

denial of service attack aimed squarely

play01:21

at one family's home

play01:23

a home located at 1600 pennsylvania

play01:26

avenue the american white house

play01:28

in the meantime the virus will continue

play01:30

to mutate and a week later a new variant

play01:33

appeared that spread much more rapidly

play01:35

the white house took cyber security more

play01:37

seriously than most and an astute

play01:39

administrator noticed suspicious

play01:41

activity early on

play01:43

in the web server logs he found

play01:44

thousands upon thousands of requests

play01:46

from random servers all around the world

play01:48

all looking for a file that simply did

play01:50

not exist

play01:52

more about the ghost file later he

play01:54

quickly brought it to the attention of

play01:55

two high-level security researchers at a

play01:57

company known as ei digital security who

play02:00

knew immediately that something serious

play02:02

was happening

play02:03

but what they could not yet tell

play02:05

the research went late into the night

play02:07

fueled by a caffeinated energy drink

play02:09

known as mountain dew code red

play02:12

and that is why one of the most

play02:14

destructive computer viruses of all time

play02:16

responsible for more than a billion

play02:18

dollars in damages will be forever known

play02:20

as code red it's but a single example of

play02:23

perhaps the most common security exploit

play02:25

in computer software the buffer overrun

play02:29

today in dave's garage i'll not only

play02:31

tell you the story of code red i'll

play02:33

explain in detail how a buffer overflow

play02:35

works down to actually writing one and

play02:37

walking the code with you in the

play02:38

debugger so that you can watch the

play02:40

hijack in action and see it with your

play02:42

own eyes

play02:45

[Music]

play02:52

hey i'm dave welcome to my shop i'm dave

play02:54

plummer a retired operating systems

play02:56

engineer for microsoft going back to the

play02:57

ms-dos and windows 95 days i've been

play03:00

coding in cnc plus plus for more than 30

play03:03

years and the buffer overrun bug that

play03:05

made code red possible is a classic

play03:07

problem for code written in either of

play03:08

those languages coming up we'll drop

play03:11

into the code editor and i'll even show

play03:12

you how to write a buffer exploit in c

play03:14

hack the stack inject your own code and

play03:17

watch it run live in the debugger just

play03:19

promise to use your powers for good

play03:21

[Laughter]

play03:24

to appreciate it though we first have to

play03:26

revisit some of the details of the

play03:27

buffer overrun attack itself and so back

play03:30

to the story of code red

play03:32

within a week on the 20th of july those

play03:34

initial 75 000 servers had spread to in

play03:37

fact a total of an estimated 360 000

play03:40

servers

play03:41

because codered targeted an exploit in

play03:43

the server product known as internet

play03:44

information services or iis his victims

play03:47

were almost exclusively the web servers

play03:49

in large data centers or corporate

play03:51

offices running a company's public

play03:53

presence on the internet

play03:55

that meant two things it meant that bit

play03:58

by bit some of the internet itself

play03:59

started to disappear as hundreds of

play04:01

thousands of the world's web server

play04:02

simply stopped responding

play04:04

but it also meant that the machines

play04:06

infected were often the biggest and most

play04:08

powerful systems of the day the big iron

play04:10

of the web if you will in fact client

play04:13

machines like windows 95 were generally

play04:15

immune as they simply lacked the iis

play04:17

server software that contained the

play04:19

vulnerability being exploited

play04:21

as a windows nt guy however i was

play04:24

personally running on working on nt and

play04:26

win2k systems all of which contained iis

play04:28

out of the box

play04:29

but if there's anybody who's up to date

play04:30

with the latest fixes and patches it's

play04:32

usually the developers working on the

play04:33

actual product where you often define

play04:35

up-to-date as have you installed this

play04:38

morning's build yet

play04:39

among the updates that i already had for

play04:41

example was an update named ms01-33

play04:44

it was boldly labeled by microsoft as

play04:46

critical the fix had been made available

play04:48

publicly by microsoft about a month

play04:50

before code red hit the streets the

play04:53

title of the update was sobering uncheck

play04:55

the buffer in an index server extension

play04:57

could enable web server compromise

play05:00

this was in the days before the

play05:01

automatic system updates however and the

play05:04

onus of performing regular updates

play05:05

therefore fell to the system

play05:07

administrators but when combined with a

play05:09

fairly casual approach to security in

play05:11

the halcyon days before code red and

play05:12

slammer and so on very few systems were

play05:15

properly updated on a rigorous schedule

play05:18

but how many machines would that leave

play05:20

without updates unpatched and vulnerable

play05:22

to infection there's a federal

play05:23

government agency known as the national

play05:25

infrastructure protection center whose

play05:27

job it is to worry about such things

play05:29

the nipc as it's known was formerly a

play05:32

division of the fbi and by the end of

play05:34

the summer they estimated that more than

play05:36

650 000 unique server ips had been

play05:38

infected

play05:40

codered was interesting in that each

play05:41

server kept track of any others that had

play05:43

already infected and would go back

play05:45

periodically to refresh or re-infect

play05:47

those same machines

play05:48

that meant that even if code red were

play05:50

removed from a machine but the

play05:52

underlying exploit were not promptly

play05:53

patched and fixed the server was likely

play05:56

to be reinfected again in short order

play05:58

code red was very aggressive spawning

play06:00

300 threats per server to go about its

play06:02

nasty business of infecting others

play06:04

that's unless the machine were shut to

play06:06

chinese in which case it would use 600

play06:08

threads

play06:09

even though code red randomly searched

play06:11

for ip addresses to infect it turns out

play06:14

not to have been very random at all as

play06:16

it used the same seed for the random

play06:18

number generator each time

play06:20

that means the simulate random sequence

play06:22

was in fact always the same and while

play06:24

this had the side effect of dutifully

play06:26

reinfecting some servers the second or

play06:27

third time as i mentioned it also wasted

play06:30

a lot of time and resources attacking

play06:32

servers that were already infected but

play06:34

soon after the initial code rate hit it

play06:36

was followed by a new variant that chose

play06:38

a different seed each time in so doing

play06:40

the random ip zip probe were different

play06:42

each time and all the resources could be

play06:44

invested towards new infections as a

play06:46

result the new variant spread even

play06:49

faster it was given the identifier crv2

play06:52

to distinguish it from the original crv

play06:53

one

play06:54

regardless of the variant though the

play06:56

code red worm operated in three distinct

play06:58

phases based on what calendar day of the

play07:00

month it was on the 1st through the 19th

play07:03

of the month it would spend its time

play07:04

randomly searching for new hosts to

play07:06

infect

play07:07

on the 20th through the 27th it would

play07:09

launch its denial of service attacks on

play07:12

days 28 and higher it would rest

play07:14

while trying to find new machines to

play07:16

infect it did not discriminate and it

play07:17

searched for iis in order to try to

play07:19

confirm the vulnerability was even

play07:21

present or check a version number or

play07:23

anything

play07:24

that's because it simply looked for a

play07:25

file called default.ida and it didn't

play07:28

even matter that the file didn't exist

play07:30

the mere act of asking the server about

play07:32

the file was enough to trigger the

play07:34

worm's attack vector on a vulnerable

play07:36

system by exploiting a buffer overrun in

play07:38

the iis web server extension code

play07:41

all of this begs the question of just

play07:42

what a buffer overrun is and how it can

play07:44

be used to inject malicious code

play07:47

well put most simply imagine you have a

play07:49

web page that accepts a username and a

play07:50

password as a visitor you type your

play07:52

answers into text boxes and the server

play07:54

reads them and copies them into buffers

play07:56

in its own memory

play07:57

now let's say those buffers are reached

play07:59

some reasonable length like 64

play08:00

characters each that seems plenty long

play08:02

for a typical username or password but

play08:04

what if due to a bug in the webpage it

play08:06

allows much longer ones to be entered

play08:09

what if some joker comes along and types

play08:10

a hundred letters into the username box

play08:12

what happens to the extra characters

play08:14

beyond 64th where do they go

play08:17

that is the key

play08:18

once you run out of buffer those extra

play08:20

characters the payload if you will runs

play08:22

straight into whatever is in memory next

play08:24

in many cases that memory will be on

play08:27

stack and a carefully constructed attack

play08:29

can even include code that is then

play08:30

executed heap attacks are also possible

play08:33

as well but they're more complicated to

play08:35

execute

play08:36

it's important to note that when i say

play08:38

the malicious payload code that gets

play08:40

stomped into memory will be executed it

play08:42

is executed by the server in the context

play08:44

of the server process

play08:46

your code has made the leap from web

play08:48

browser straight into executing memory

play08:49

of the web server you're visiting just

play08:51

like a true viral infection might leap

play08:53

between species but how can that even

play08:55

happen

play08:56

to understand it we must have an idea of

play08:58

how memory is laid out for a process

play09:00

with the lowest addresses at the bottom

play09:02

and the highest at the top let's look at

play09:04

a map of how a process is laid out in

play09:06

memory

play09:07

typically it starts out with the program

play09:09

code down at the bottom in a secure

play09:11

system this area can't be modified once

play09:14

running

play09:15

above that in memory we have

play09:16

pre-initialized data like built-in

play09:18

graphics images cursors global tables

play09:20

you might have defined that kind of

play09:22

thing

play09:23

above that we have uninitialized data

play09:25

area reserved for variables that will

play09:27

not be created until runtime

play09:29

above that is the program heap where

play09:31

your mallocs and calyx and new objects

play09:33

come from

play09:34

everything else in a little memory is

play09:35

fixed in size but the heap expands

play09:37

upward as you need more of it

play09:39

now up at the top of memory is the stack

play09:41

it's an area of memory that goes down as

play09:43

you add things to it

play09:45

the fact that it goes downward in memory

play09:46

is a little counter-intuitive but it's

play09:48

done that way because the heap is

play09:49

growing up from the other end it simply

play09:51

puts them the furthest apart

play09:53

the address spaces are big enough with

play09:55

the x86 and especially with the ia-64

play09:57

that you'd likely run out of memory long

play10:00

before the heap actually reaches a stack

play10:02

the traditional metaphor for the stack

play10:04

is the good old stack of food trades in

play10:06

a cafeteria

play10:07

it's a last in first out affair where

play10:09

when you add a new trade to the stack it

play10:11

becomes the first one that will be

play10:12

popped off next

play10:14

with a computer stack data can be pushed

play10:16

onto the stack and much like the trays

play10:18

they get popped off in reverse order as

play10:20

a really simplified example imagine the

play10:22

only thing the stack were used for were

play10:24

the return addresses of functions let's

play10:26

say that a called b we push the return

play10:29

address in a onto the stack and we jump

play10:31

to b

play10:33

now b calls c

play10:34

we push the return address for b onto

play10:36

the stack and jump to c perhaps

play10:38

similarly c calls d

play10:40

now when d returns it pops the first

play10:42

thing off the stack that it finds and

play10:43

that's the return address back in c when

play10:46

c returns it pops off the address in b

play10:49

when b is complete it pops off the

play10:50

original point back in a

play10:52

given how a stack works it lends itself

play10:54

very naturally to nested function calls

play10:57

in reality it's slightly more

play10:58

complicated than that for a couple of

play11:00

reasons first the function arguments are

play11:02

also pushed onto the stack by the caller

play11:04

before the return address is pushed on

play11:06

second following the return address a

play11:08

pointer to the previous frame is pushed

play11:10

onto the stack

play11:12

and third this is followed by space

play11:14

that's reserved for local variables

play11:16

finally the function itself can make use

play11:18

of the stack as long as it cleans up

play11:19

anything it pushes on by popping it off

play11:21

before returning

play11:23

note that to create local variables the

play11:25

system merely reserves space on the

play11:27

stack by adjusting the stack pointer

play11:28

down it doesn't really push them onto

play11:30

the stack and instead it just subtracts

play11:32

however many bytes it needs from the

play11:34

system stack pointer

play11:36

the little patch of stack belonging to a

play11:37

function that contains its return

play11:39

address and local variables is known as

play11:41

its stack frame

play11:42

now you can access any part of your own

play11:44

frame but no random access to stack

play11:46

memory outside of your own frame is

play11:48

typically allowed the hardware doesn't

play11:50

prevent it it's just kind of a

play11:51

convention so we however are going to

play11:53

break that rule we're actually going to

play11:55

break a lot of rules but that's what

play11:57

real hackers do they're fast talking

play11:58

rebels that play by nobody's rules not

play12:00

even their own

play12:02

but first we'll look at what happens

play12:03

when you call a function first any

play12:05

arguments that need to be passed to the

play12:06

function are pushed onto the stack in

play12:08

right to left order finally the

play12:11

instruction pointer which indicates

play12:12

where execution should pick up when the

play12:14

function returns is pushed onto the

play12:15

stack and that's key because we're going

play12:17

to cause our own code to execute by

play12:19

replacing the real return address of the

play12:21

current function with our own address

play12:24

that way when the function returns the

play12:26

cpu will do what it always does pop the

play12:28

address we came from off the stack and

play12:30

jump back to it

play12:31

little will it know that like folger's

play12:33

crystals we've secretly replaced the

play12:35

real address with our own causing our

play12:37

malicious payload to be executed as soon

play12:38

as the function even tries to return

play12:41

to see this in action let's write a

play12:42

simple c program that simply calls a

play12:44

function that's all it does

play12:46

and then i'll use a string buffer

play12:48

overrun inside that function to show you

play12:49

how to mess with the stack and inject

play12:51

our code but first let's get the basic

play12:53

demo program running

play12:55

if you'll give me a moment i'll crank

play12:57

out a little program to do exactly what

play12:58

it is that we want

play13:16

here we have a basic c program with

play13:17

three functions the entry point main is

play13:20

down at the bottom

play13:22

it simply prints a message to let us

play13:23

know that it's running and then it calls

play13:25

function one

play13:27

function one just copies a string to a

play13:29

buffer and then prints a message

play13:30

confirming that it ran and then it

play13:31

returns the program exits and that's it

play13:34

that's all it does

play13:36

but notice there's another function

play13:37

called function2 it's defined in the

play13:39

file as well

play13:40

it will represent our malicious payload

play13:42

but nobody calls function2 and hence it

play13:44

should never run or execute our exploit

play13:47

will change all that but for now when

play13:48

things are working properly unmodified

play13:51

if we run the program we get a console

play13:52

window and then the messages in the

play13:54

expected order

play13:55

first the main function runs and then

play13:57

the message from function one is

play13:58

displayed and then it exits

play14:01

now comes the exploit i'm going to

play14:03

overrun the buffer by giving stir end

play14:05

copy too much source data to work with

play14:07

because of the way local variables are

play14:09

laid out on the stack i know that right

play14:11

after the foo buffer of eight characters

play14:13

i'll be able to find the frame pointer

play14:15

or ebp

play14:16

because pushing evp is the first thing

play14:18

that the function does

play14:20

above that is the return address that we

play14:22

came from which is what we're really

play14:23

after

play14:24

now as soon as we overrun the foo buffer

play14:26

by even four bytes we will overwrite the

play14:28

copy of ebp on the stack

play14:30

the next four bytes will overrun the

play14:32

return address on the stack

play14:34

but what do i provide is the new

play14:35

hijacked value for the return address i

play14:37

need to know the address of function too

play14:39

because that's where i want it to go

play14:41

to figure that out let's run the program

play14:43

under the debugger and see where

play14:44

function 2 lives

play14:46

when we run it under the debugger i can

play14:48

pause execution right before the string

play14:50

copy call and inspect the address of

play14:52

function 2 which turns out to be 1 0 0 1

play14:55

1 5 e 0.

play14:57

thus in little envy and reverse order

play14:59

the bytes we want to stuff in our memory

play15:01

are e0 1 5 0 1 1 0.

play15:04

let's update the code to pass those

play15:06

until string copy we now have 16 bytes

play15:08

to work with

play15:11

let's run the program and stop it again

play15:13

at the break point on the string copy

play15:14

call

play15:16

i can use the debugger to see that the

play15:17

foo buffer is now located at 1 9 fe 88

play15:21

with the memory window open in the

play15:22

debugger i'll select those bytes with

play15:24

the cursor so you can see where the

play15:25

string is that's going to get copied to

play15:27

the first eight bytes worth the ones

play15:29

i've selected are completely legal it's

play15:31

okay to write there but the next four

play15:33

bytes comprise the 0 0 1 9 f e4 value of

play15:36

the ebp frame pointer that was pushed at

play15:39

function entry

play15:40

the next four bytes the one zero zero

play15:43

one one six one c

play15:45

they form the return address we're gonna

play15:47

use our string copy to fill out the

play15:49

eight legal bytes and then overrun into

play15:51

ebp with the number one two three four

play15:53

and then the magic part the address of

play15:56

function two will be poked into the next

play15:57

four bytes which we know are the return

play15:59

address

play16:00

we can sanity check that by seeing what

play16:02

now lives at one zero zero one one six

play16:05

one c the address that was currently on

play16:07

the stack and sure enough you can see

play16:09

that it's immediately after the call to

play16:10

function one just as we'd expect

play16:12

if we didn't change anything it would

play16:14

return and then happily execute that xor

play16:16

instruction

play16:18

let's see what happens when we do change

play16:20

things

play16:20

as soon as the code calls string copy

play16:22

note how the bytes in the memory window

play16:24

turn red to indicate that they've

play16:25

changed two more bytes than colored red

play16:27

were actually written out but because

play16:29

the values 0 1 and 1 0 stay the same

play16:31

before and after the call they don't get

play16:33

highlighted by the debugger but they all

play16:35

changed

play16:36

let's see what happens when we continue

play16:38

on to return out of the function we

play16:40

normally would have been going back to

play16:41

main but if our hackery has worked

play16:43

execution will wind up in the payload

play16:45

code we proceed step by step and as soon

play16:48

as the ret instruction is executed boom

play16:50

there it is we're at the top of function

play16:52

2. if i run the code sure enough the

play16:55

output confirms that the sinister

play16:56

payload code that should never run just

play16:58

ran to keep things simpler you'll notice

play17:01

i didn't preserve evp but that only

play17:03

matters if i return back out of function

play17:05

2. when i try that it winds up in a loop

play17:07

of main function 1 and function 2

play17:09

sinister payload code executing an order

play17:12

forever

play17:16

hopefully you now have a sense for how

play17:18

these can work and you're no doubt

play17:20

surprised at how little code it took

play17:21

particularly with the sample exploit

play17:23

being a single string copy line can't be

play17:26

that easy can it there must be some

play17:28

defense and indeed with a modern system

play17:30

there are several layers of defense

play17:32

the first is for programmers to follow

play17:34

the practice of banning these unsafe

play17:35

string functions like i just used from

play17:37

their coding lives the ones that don't

play17:39

accept the buffer length that have

play17:41

undesirable or unsafe behavior otherwise

play17:44

microsoft has a list of these and

play17:45

depending on the project settings the

play17:47

compiler will even warn you or generate

play17:49

an error if you try to use any of the

play17:50

old unsaved functions it's not just some

play17:53

weird paternalistic or idealistic notion

play17:55

you really should upgrade to the modern

play17:57

alternatives just to avoid common

play17:59

frustrating bugs even aside from the

play18:00

security implications of the old

play18:02

functions i'll be looking at that whole

play18:04

issue in detail in a future episode of

play18:06

my stupid c plus plus tricks series so

play18:08

please make sure that you're subscribed

play18:10

to the channel in order to see that as

play18:11

well as other cool and interesting

play18:12

technical and historical material the

play18:15

next level of protection comes from

play18:16

microsoft's depth for data execution

play18:18

prevention on windows but simply

play18:20

prevents running as code anything that's

play18:22

actually in a data segment let's say you

play18:25

managed to get your payload into a heat

play18:26

buffer and wanted to modify the return

play18:28

value of some function using a string

play18:30

exploit just as we did earlier to then

play18:31

jump to your code with depon you'd be

play18:34

protected because your cpu would not be

play18:36

allowed to run code from a page on the

play18:37

heap like that

play18:39

similarly and perhaps more important you

play18:41

can prohibit the execution of code on

play18:42

the stack

play18:43

back in the olden days of coal mining

play18:45

which is to say up until about 1986 the

play18:48

workers would be accompanied by a bird

play18:49

such as a canary

play18:51

carried in a little cage and exposed to

play18:53

the same gases and vapors as the workers

play18:55

in the mine tunnels if the air became

play18:57

toxic with carbon monoxide or other

play18:58

gases the bird would become ill first

play19:00

and act as an advanced signal that the

play19:02

air had become unsafe

play19:04

this would allow the workers to be

play19:06

safely evacuated

play19:07

that's the origin of the phrase canary

play19:09

in the coal mine and that's the reason

play19:11

why when the compiler places guard

play19:13

blocks of bytes before and after your

play19:15

buffers those blocks are known as stack

play19:17

canaries

play19:18

if their value becomes disturbed by

play19:20

anyone or anything the system knows that

play19:22

something has corrupted them and the

play19:23

application has become unstable

play19:26

stack canaries are great except that the

play19:28

corruption isn't usually detected until

play19:30

the function tries to return when

play19:31

they're inspected the health of the

play19:33

canary is then verified but the damage

play19:35

could have been done long ago somewhere

play19:37

earlier in the same function in order to

play19:39

catch buffer overruns live as soon as

play19:41

they happen the compiler can enlist the

play19:43

processor's memory management unit or

play19:45

mmu in the fight

play19:47

for each buffer an entire page of memory

play19:49

is allocated before and after it the

play19:51

hardware mmu is then instructed to deny

play19:54

rights to those pages and any attempts

play19:56

to do so are caught right as the

play19:58

offending instruction is executing it's

play20:00

nearly foolproof but it consumes extra

play20:02

memory pages for each buffer

play20:04

i'm no cpu talking smart guy but if i

play20:07

were i'd assume that there are limits to

play20:09

the number of entries you can have in

play20:10

the mmu's page table

play20:12

even if there were no practical limits

play20:14

i've got to imagine that there's a

play20:15

performance impact of some kind involved

play20:17

with massively growing the mmu's

play20:18

translation look aside buffer

play20:20

now i've never used guard pages in a

play20:22

shipping product but i have turned them

play20:24

on briefly for the heap rather than the

play20:26

stack in order to chase an elusive heap

play20:28

corruption bug once long ago

play20:31

it was very slow in operation but it

play20:33

found the problem on i think like the

play20:34

first run that i tried it with

play20:36

the two questions we'd like to know the

play20:38

answer to next though are how

play20:39

specifically did codered exploit a

play20:41

buffer overrun and how did it get its

play20:43

payload onto the machine

play20:45

apparently codered exploited a bug in an

play20:48

iis extension where a component that

play20:50

wound up inspecting get requests had a

play20:52

buffer overrun bug

play20:54

specifically there was a completely

play20:56

unchecked buffer in idq.dll which is an

play20:58

is api extension related to the indexing

play21:01

portion of the iis web server

play21:04

when presented with a really long string

play21:05

for get requests such as the one shown

play21:07

on the screen here the buffer is big

play21:09

enough to accommodate everything through

play21:10

about the last capital n after that it's

play21:13

all carefully constructed binary payload

play21:16

much as i demonstrated in the debugger

play21:17

the program counter was hijacked to

play21:19

cause the payload to execute as

play21:21

instructions

play21:22

that would serve to begin the infection

play21:24

and the propagation of the worm

play21:26

meanwhile back at the white house the

play21:28

iit staff had to contend with a new

play21:30

onslaught of denial of service attacks

play21:32

that would begin like clockwork on the

play21:34

20th of every month

play21:35

they would last a week before they'd go

play21:37

dormant again and each month's attack

play21:39

was larger than the month before it the

play21:41

infection would continue to spread

play21:42

unchecked like fire in a dry field until

play21:44

a fellow named kenneth eichman

play21:46

discovered that just like the dinosaurs

play21:48

of jurassic park the virus's creators

play21:50

had engineered a lysine deficiency of

play21:52

sorts into the worm except this one was

play21:54

sort of backwards in the movie the giant

play21:57

tyrant lizards would die out if not

play21:58

periodically fed a special nutrient

play22:00

thereby hopefully preventing them from

play22:02

running amok without their creator's

play22:04

consent

play22:05

without the lysine supplement being

play22:06

intentionally fed to them they would die

play22:08

out

play22:09

eichmann discovered that code red worked

play22:11

in a similar fashion but with a twist

play22:13

one of the first things it did upon

play22:14

starting up was to look for a file on a

play22:16

c drive called wait for it no worm

play22:19

if a file named no worm was present and

play22:22

it didn't matter what it contained as

play22:23

long as the file was just there at all

play22:25

the infection would go dormant

play22:27

once good old kant discovered this

play22:28

backdoor method of shutting down the

play22:30

attacks it became widely known that

play22:32

protecting a machine was as simple as

play22:33

creating a file by that name

play22:35

that gave system administrators the

play22:37

breathing room needed to remove the

play22:38

malware and update their servers with

play22:40

the latest security patches

play22:42

this discovery of how to shut down code

play22:44

red earned eichmann a trip to the white

play22:45

house

play22:46

of course they would still need to

play22:47

contend with the slammer worm just two

play22:49

years later and with any number of

play22:51

would-be exploits over the decades but

play22:53

the important thing is that security

play22:54

efforts became more proactive rather

play22:56

than responding to attacks after they

play22:58

happened administrators began keeping

play23:00

machines up to date as part of regular

play23:02

maintenance

play23:03

modern programming practices combined

play23:04

with updated operating systems and

play23:06

server components written with security

play23:08

in mind from the beginning rather than

play23:09

tacked on as an afterthought have

play23:11

combined to make the buffer overrun much

play23:13

less frequent than in years past

play23:15

zero day exploits are becoming

play23:17

increasingly rare and perhaps further

play23:18

and further apart but they can never be

play23:20

eradicated entirely and it only takes

play23:22

one

play23:23

as the wannacry ransomware attack of

play23:25

2017 demonstrated there really are no

play23:27

links to which a malicious actor will

play23:29

not go up to including encrypting all

play23:31

the data present on the machine holding

play23:33

it for ransom and even completely

play23:35

deleting it

play23:36

if you found this story entertaining or

play23:38

if you learned anything from the buffer

play23:39

overrun explanation please drop me a

play23:41

like on the video to let me know it was

play23:42

worthwhile

play23:43

as you can see my channel is still

play23:45

fairly small so if you can share with a

play23:46

friend please shoot them a link

play23:49

if you're interested in this kind of

play23:50

content yourself please make sure to

play23:51

subscribe to the channel these days a

play23:53

subscription doesn't do much on youtube

play23:55

in terms of content but it does serve as

play23:57

a vote of confidence in the channel and

play23:59

it makes me personally happy since i'm

play24:01

really only in this for the subs and

play24:02

likes anyway so please consider it

play24:05

thanks for joining me out here in the

play24:06

shop today in the meantime and in

play24:07

between time i hope to see you next time

play24:09

right here in dave's garage

play24:14

let's go back and redo this part

play24:16

a new variant appeared that would spreed

play24:19

spreed

play24:20

it would spreed

play24:21

damn it

play24:22

they simply lacked the iss

play24:26

that meant that even if code red were

play24:28

removed from

play24:31

just read the words that you wrote when

play24:33

you were thinking at the speed that you

play24:35

can type and think

play24:37

as a result the new variant spread even

play24:39

faster it was given the identifier

play24:43

identifier

play24:45

the first eight bytes worth the ones

play24:47

i've selected are completely legal we're

play24:48

allowed to write there the next four

play24:50

break ah dan

play24:56

boom there it is

play24:59

[Music]

play25:01

that's good enough

play25:02

boom there it is two sinister payload

play25:05

pages

play25:06

in main function one and function twos

play25:08

says payload uh

play25:11

payload no payload for me

play25:14

black door

play25:16

i see a black door and i want to paint

play25:19

it red

play25:20

that's the origin of the phrase canary

play25:22

in the coal mine and that's the reason

play25:24

when the compiler places

play25:28

i can't win man too many leaf blowers

play25:30

the server was likely to be re-affected

play25:32

again in

play25:34

re-effected it's gonna be a re-affected

play25:38

don't know what that

play25:39

[Music]

play25:40

gotta get up in the means

play25:42

because i have to

play25:44

do it anyway

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
CybersecurityCode Red WormBuffer OverflowHacking HistoryInternet SecurityVirus AttackMicrosoftServer ExploitsDDoS AttacksC Programming