Buffer Overflow AttackIn a buffer overflow, while writing data to a biffer, the b/ffer’s boundary is overrun and adjacent memory is overwritten.. The most common vulnerability often expl
Trang 1B u f f e r O v e r f lo w
Trang 2Buffer Overflow Attack
In a buffer overflow, while writing data to a biffer, the b/ffer’s boundary is overrun and adjacent memory is overwritten.
Lab Scenario
Source: h ttp ://www.1c.u11icam p.br/~stolfi/urna/butfer-oflow Hackers continuously look for vulnerabilities 111 software or a computer to break into the system by exploiting these vulnerabilities
The most common vulnerability often exploited is die buffer overflow attack, where
a program failure occurs eidier 111 allocating sufficient memory for an input string or
111 testing die lengdi o f string if it lies within its valid range A hacker can exploit such
a weakness by submitting an extra-long input to the program, designed to overflow its allocated input buffer (temporary storage area) and modify the values o f nearby variables, cause the program to jump to unintended places, or even replace the program's instructions by arbitrary code
If the buffer overflow bugs lie 111 a network service daemon, the attack can be done
by direcdy feeding the poisonous input string to the daemon If the bug lies 111 an ordinary system tool or application, with no direct access, the hacker attaches the poisonous string widi a document or an email which, once opened, will launch a passive buffer overflow attack Such attacks are equivalent to a hacker logging into the system widi die same user ID and privileges as die compromised program Buffer overflow bugs are especially common 111 C programs, since that language does not provides built-in array bound checking, and uses a final null byte to mark the end of a string, instead o f keeping its length 111 a separate field To make dungs worse, C provides many library functions, such as s t r c a t and g e t l i n e , which copy strings without any bounds-checking
As an expert ethical hacker and penetration tester, you must have sound knowledge o f when and how buffer overflow occurs You must understand stacks- based and heap-based buffer overflows, perform penetration te s t s for detecting buffer overflows in programs, and take precautions to prevent programs trom buffer overflow attacks
Lab Objectives
The objective o f tins lab is to help students to learn and perform buffer overflow attacks to execute passwords
111 tins lab, you need to:
■ Prepare a script to overflow buffer
■ Run the script against an application
I CON KEY
Valuable
in to in ia d o a
Test your
knowledge
s A W eb exercise
m W orkbook review
E th ic a l H a c k in g a n d C o u n term easu res Copyright © by EC-Council
C E H L ab M an u al P ag e 902
Trang 3■ Perform penetration testing for the application
■ Enum erate a password list
Lab Environment
■ A computer running with Windows Server 2 012 as H ost machine
■ A Virtual Machine running with Back Track 5 R3
■ A web browser with Internet access
■ Administrative privileges to 11111 tools
Lab Duration
Time: 20 A luiutes
Overview of Buffer Overflow
Buffer overflow is an anomaly where a program, while wnting data to a buffer, overruns the butter's boundary and overwrites adjacent memory Tins is a special case o f violation o f memory safety Butter overflows can be triggered by inputs that are designed to execute code, or alter the way the program operates Tins may result
111 erratic program behavior, including memory access errors, incorrect results, a crash, or a breach o f system security Thus, they are the basis o f many software vulnerabilities and can be maliciously exploited
Lab Tasks
Recommended labs to assist you 111 buffer overflow:
■ Enumerating Passwords 111 “Default Password List”
o W rite a Code
o Compile die Code
o Execute the Code
o Perform Buffer Overflow Attack
o Obtain Command Shell
Lab Analysis
Analyze and document the results related to the lab exercise Give your opinion on your target’s security posture and exposure
& This lab can
be dem onstrated
using Backtrack
Virtual Machine
2* TAS K 1
Overview
P L E A S E T A L K T O Y O U R I N S T R U C T O R I F Y O U H A V E Q U E S T I O N S
R E L A T E D T O T H I S L A B
E th ic a l H a c k in g a n d C o u n term easu res Copyright © by EC-Council
C E H L ab M an u al P ag e 903
Trang 4Buffer Overflow Example
In a b/rffer oveijlow, while writing data to a b/rffer, the buffer's boundary is overrun and adjacent memory is overwritten.
Lab Scenario
111 computer security and programming, a buffer overflow, 01־ buffer overrun, vulnerability appears where an application needs to read external information such as
a character string, the receiving butter is relatively small compared to the possible size o f die input string, and the application doesn't check the size The buffer allocated at run-time is placed 011 a stack, which keeps the information for executing functions, such as local variables, argument variables, and the return address The overflowing string can alter such information Tins also means that an attacker can change the information as he 01־ she wants to For example, the attacker can inject a series o f macliine language commands as a string that also leads to the execution o f the attack code bv changing the return address to the address o f the attack code The ultimate goal is usually to get control o f a privileged shell by such methods
Programming languages commonly associated with buffer overflows include C and
C + + , which provide 110 built-in protection against accessing 01־ overwriting data 111 any part o f memory and do not automatically check that data written to an array (the built-in buffer type) is within the boundaries ot that array Bounds checking can prevent buffer overflows
As a penetration tester, you should be able to implement protection against stack- smaslung attacks You must be aware o f all the defensive measures for buffer overflow attacks You can prevent buffer overflow attacks by implementing 11111- time checks, address obfuscation, randomizing location o f functions 111 libc, analyzing static source code, marking stack as non-execute, using type safe languages such as Java, ML, etc
Lab Objectives
The objective o f tins lab is to help students to learn and perform buffer overflow to execute passwords
111 tins lab, you need to:
I C O N K E Y
/ Valuable
inform ation
y* T est your
knowledge
s W eb exercise
m W orkbook review
E th ica l H a c k in g a n d C o u n term easu res Copyright © by EC-Council
C E H L ab M an u al P ag e 904
Trang 5■ Prepare a script to overflow buffer
■ Run the script against an application
■ Perform penetration testing for the application
■ Enum erate a password list
Lab Environment
■ A computer running with Windows Server 2012 as H ost maclune
■ A Virtual Machine running with Back Track 5 R3
■ A web browser with Internet a c c e s s
■ Administrative privileges to run tools
Lab Duration
Time: 20 Minutes
Overview of Buffer Overflow
Buffer overflow takes place when data written to a buffer because o f insufficient bounds checking corrupts the data values 111memory ad d resses, which are adjacent to the allocated buffer Most often diis occurs when copying strings of characters from one buffer to another.
W hen die following program is compiled and mil, it will assign a block ot memory
11 bytes long to hold die attacker string, strcpv function will copy the string
“D D D D D D D D D D D D D D ” into an attacker string, which will exceed the buffer size o f 11 bytes, resulting 111 buffer overflow
0 1 2 3 4 5 6 7 8 9 10 1112
3 4 5 6 7 8 9 10
■c
i
1 2 3 4 S7 6״
Buffer Overflow Exam ple Code
#include<stdio.h>
int main ( int argc, char * *argv)
{
char B u ffe rfll] =״AAAAAAAAAA״;
strcpylBuffer/DDDDDDDDDODD״};
printf(“96 \n ״ Buffer);
return 0;
}
This type o f vulnerability is prevalent in UNIX• and NT-based systems
Lab Tasks
I T This lab can
be dem onstrated
using Backtrack
Virtual Machine
S TAS K 1
Write a Code
E th ica l H a c k in g a n d C o u n te n n ea su re s Copyright © by EC-Council
C E H L ab M an u al P ag e 905
Trang 6־R ״ * T
BackTrack on WIN 2N9STOSGIEN Virtual Machine Connection
י kVia Clipboard View
@3 1 וו►h פ
i 0933761 HET: Registered protocol fa n ily 17 1.0915311 input: AT T ranslated Set 2 keyboard as /d c1;iccs^platfor1v'i8l>12/'scrio0/input/'inputl 1.0952761 Registering the dns reso lve r key type
1.0957B31 registered taskstats version 1 1.1639921 llagic nunber: 12:1ZU:12G 1.1644561 acpi device:01: hash notches 1.1056581 rtc.cnos 00:02: setting syste* clock to 2012-09-25 11:06:59 UTC (1340571219) 1.165468) BIOS EDO f a c il i t y v0.16 2004-Jun-25, 0 devices found
1.1658621 COD information not a v a llable.
1.2378181 a t a t.06: ATA-8: U irtu a l HD, 1.1.0, raax MUDMfi2 1.2389361 atat.06 : 33554432 scctors, n u lti 12B: LBA48 1.2415511 ataZ.06: filAPI: U irtu a l CD, , wax hllDt1A2 1.2432671 ata2.06: configured fo r MUM1n2 1.2441101 a ta l.0 6 : configured fo r flUDHflZ 1.2442231 scsi 0:0:0 :6 : Direct-Access ATA V irtu a l HO 1.1 PQ: 6 AMSI: 5 1.2451571 sd 0:0:0 :0 : (sdal 33554432 512-byte logical blocks: (17.1 GB/׳lb.O GiB) 1.2455461 sd 0:0:0:0: lsd<1l 4096-hyte physical blocks
1.2459741 sd 0:0:0 :0 : (sdal Write Protect Is o ff 1.2463841 sd 0:0:0 :0 : Attached scsi generic sgO type 0 1.2468141 sd 0:0:0:0: fsdal U rlte cache: enabled, read cache: enabled, doesn't support DPT nr Flh 1.2404231 scsi 1:0:0 0: CD ROM Hsft U irtu al CD/ROM 1.0 PQ: 6 AHSI 5 1.2515061 sr6: scsi3 mik d rive: 0x/0k tray
1.2526091 cdron: Uniforn CD HUM d rive r Revision: 3.26 1.2527931 * r 1:0:0:0: Attached scsi generic sgl type 5 1.2506571 sda: sdal r.da2 < xda5 >
1.2506591 ad 0:0:0:0: Inda I Attached 8C5I disk 1.2602631 Freeing iiiiiisimI kernel **:wiry; 96Hk fn x d 1.2608041 U rlte p ro te c tI1M| the kernel read only data: 1228Hk 1.26S624I Freeing unused kernel M M inj: 1732k freed 1.2699051 Prering unused kernel !*rnorij: 1492k freed ling, please w a it
1.2873151 udev: s ta rtin g version 151 1.2962U0I udevd (03): /•prot/׳U3/uon adj is deprecated please use /proc/OJ/oon score ad j instead.
1.3963921 Floppy d riv e (s ): fdO is 1.44f1 1.41 rilH I PDC 6 is an 02070.
2.0203071 Refined T8C clocksource ca lib ra tio n : 3692.970 fW־׳
FIGURE 1.1: BackTrack Login
3 Type startx to launch die GUI
*־
־ 1-1°
BackTrack on WIN-2N9STOSGIEN Virtual Machine Connection
■ Re irrior I.V44 CSpbeard Vie
I't •> (- © 3 11 1►h
_ Buffer overflow occurs
when a program or process
tries to store more data in a
buffer
1.24S974I sd 0:0:6 :6 : (sdal U rlte Protect Is o ff 1.2463841 sd 0:0:6 :6 : Attached scsI generic sy6 type 6 1.2468141 sd 0:0:0:©: tsdal U rlte cache: enabled, read cache: enabled, doesn't support DP0 or FU1 1.2404231 scsi 1:6:6 :0 : CD ROM Msft U irtu a l CD-ROM 1 6 PQ: 6 ANSI: 5 1.2515061 sr6: scsi3־rwc drive: 0x/0־x tray
1.252*091 cdrm : Onifcrn CD-HW driver Revision: 3.20
I 2527931 s r !:0 :6 :6 : Attached scsi generic syl type 5
I 2586571 sda: sdal sda2 < sd»5 >
1.2506591 sd 0:0:6 6: (sdal attached SCSI disk 1.2602631 freeing unused kernel ncmury: 'J6UK freed 1.2I10H04I Write protecting the kernel read-only data: IZZIMIk 1.2656241 FreeIny unused kernel nennry: 1732k freed 1.2699851 Freeing unused kernel nonary: 1492k freed ading, please u a lt
1.2873151 udev: s ta rtin g version 151 1.2962001 udevd (83): /p r01v l l l01׳m_(1d j is deprecated, please use /׳proc/03׳׳cw»»_score_adj instead.
1.3963921 Floppy d riv e (s ): fd6 is 1.44H 1.4139841 F K 6 Is an H267H.
2.0203871 Refined TSC clocksource ca lib ra tio n : 3692.970 life cklrack 5 JO - 64 B it bt t t y l
g iu tined out a fte r 60 seconds.
Systen information as of Iuc Sep 25 16:45:47 1ST 2012
Usage o f ✓: 72.3x o f 15.23GB Users logged In: 0 rtenory usage: 1׳ IP address fo r eth6: 10.0.0.14 Swap usage: O k
Graph th is data and w1na!!r th is systen at https:/✓landscape.canonical.con✓
FIGURE 1.2: BackTrack GUI Login-Startx Command
4 BackTrack 5 R3 G UI desktop opens, as shown 111 die following screenshot
m Code which is entered
in kedit is case-sensitive
E th ica l H a c k in g a n d C o u n term easu res Copyright © by EC-Council
C E H L ab M an u al P ag e 906
Trang 7FIGURE 1.3: BackTrack 5 R3 Desktop
5 Select the BackTrack Applications menu, and then select A ccesso ries gedit Text Editor.
ל/Mem (»_J
* ^ Oik uwg* Analyzer
^ oedlt Tfcxt Editor
| TWmlrwl
ס Tkrminator
־y t > r*
*V *«es*o׳H׳s
^ BackTtock
4 &#***%
£ internet
dioflke
)14 Other
WK Sound 6 V^deo
0 System Tools
<< back track
ca Programming languages
commonly associated with
buffer overflows include C
and C++
FIGURE 1.4: Launching gedit Text Editor
6 Enter die following code 111 gedit Text Editor (Note: the code is case- sensitive)
# i n c l u d e < s t d i o h>
v o id m a in () {
c h a r *name;
c h a r *command;
nam e=(char * ) m a llo c (1 0 );
command=(char * )m a llo c (128);
p r i n t f ( " a d d r e s s o f name i s : % d\n",nam e);
p r i n t f ( " a d d r e s s o f command i s : %d\n",command);
p r i n t f ( " D i f f e r e n c e betw een a d d r e s s i s : % d\n",
command-E th ica l H a c k in g a n d C o u n term easu res Copyright © by command-EC-Council
C E H L ab M an u al P ag e 907
Trang 8nam e);
p r i n t f ( " E n te r your n am e:");
g e ts (n a m e );
p r i n t f ("H ello % s\n",nam e);
system (com m and);
}
׳׳> v x *u n s a v e d D o cum e nt 1 ־ g e d it File Edit View Search Tools Documents Help
^ 9k
n *Unsaved Document 1 X
# 1 nclude< std 10 h>
v o id m ain() { char •name;
char •command;
name=(char * )m a llo c (1 0 );
command=(char *)m a llo c ( 1 2 8 ) ;
p r in tf( " a d d r e s s o f name i s : % d\n",name);
p r in tf( " a d d r e s s o f command is:% d\n",com m and);
p r i n t f ( “ D iffe re n c e between address i s :%d\n“ ,command-name);
p r in t f ( " E n t e r your name:“ ) ; gets(nam e);
p r in t f ( " H e ll o % s\n",name);
system ( command);
Plain Text ▼ Tab Width: 8 ▼ Ln 15, Col 2 FIGURE 1.5: Writing code for execution
7 Now save die program by selecting File ־־) Save A s־) root or simply click
Save as shown 111 the following screenshot screenshot as buffer.c
_* * U n s a v e d D o c u m e n t 1 ־ g e d i t
File Edit View Search Tools Documents Help
N ow launch die command terminal and compile die cod e by running:
g c c b u f f e r c - o b u f f e r
Ii=y1 Code is compiled using
the following commend: gee
buffer.c biiffer.
ט No tool can solve
completely die problem of
buffer overflow, but die)׳■
surely can decrease the
probability of stack smashing
attacks
Compile the Code
E th ica l H a c k in g a n d C o u n term easu res Copyright © by EC-Council
C E H L ab M an u al P ag e 908
Trang 9/v v x ro o t @ b t :
-File Edit View Terminal Help root@ bt: ־־# |gcc b u f fe r c -0 b u ffe r fj
FIGURE 1.7: BackTrack compiling the code
9 If there are any errors, ignore them
/v v X rootcabt: -File Edit View Terminal Help
r o o tg b t:- # gcc b u f fe r c ■0 b u ffe r
b u f fe r c : In fu n c tio n 'm a in ': —
b u f fe r c :6 : warning: in co m p a tib le im p l ic i t d e c la ra tio n o f b u itfs tlH ^ u n c tio n ‘ mal
b u f fe r c :8 : w arning: form at '%d' expects type 1 " ־n t ' , b ut a rg u m e n t^'tts s type 'ch
a r • ׳
b u f fe r c :9 : warning: form at '%d' expects type , i n j ^ o u t argument 2 jM F t y p e *ch
b u ffe r c :1 0 : w arning: form at '%d' expects type ' i n t ׳ , but a rg u m e n t# has type ' I ong i n t '
/tm p /ccx6 Y 3 vl.o : In fu n c tio n m a in ':
b u f fe r c : ( te x t+ 6 x 9 0 ): w arning: the g e ts ' fu n c tio n is dangerous a n ^ t a u ^ ^ i o t
be used
root@ bt:~# [ ]
: b a c k I t r a c k
FIGURE 1.8: BackTrack Error Message Window
10 To execute the program type /buffer
The program executes
using following command:
.!buffer
— j
Execute the Code
E th ica l H a c k in g a n d C o u n term easu res Copyright © by EC-Council
C E H L ab M an u al P ag e 909
Trang 10־־ * r o o t @ b t : ~
File Edit View Terminal Help
r o o tg b t: •־# | /b u f f e r | address o f name i s : 20144144 address o f command i s :20144176
E nter your name:|
■
ם
m An executable program
on a disk contains a set of
binary instructions to be
executed by die processor
FIGURE 1.9: BackTrack Executing Program
11 Type any name in die Input held and press Enter; here, using Jason as an
example.
v x r o o t @ b t :
»
File Edit View Terminal Help root@ bt:~# /b u f f e r
20144144 address o f name is
26144176 : address o f command i s
32 :
D iffe re n c e between address is
| as :|נ Enter your name
b a c k I t r a c k
c a Buffer overflows work
by manipulating pointers
(including stored addresses)
FIGURE 1.10: Input Field
12 Hello Jason should be printed
E th ica l H a c k in g a n d C o u n term easu res Copyright © by EC-Council
C E H L ab M an u al P ag e 910