|
|
The issue 1: https://buildsecurityin.us-cert.gov/swa/presentations_032011/CharlesHenderson-2011GlobalSecurityStatsAndTrends.pdf
The issue 2: http://www.cert.org/research/2010research-report.pdf
CERT" The goal is to execute code, which was not intended to execute?
From: (Mon Jan 15 10:33:29 EST 2007) Hr -url http://en.wikipedia.org/wiki/X_Window_System http://en.wikipedia.org/wiki/X_Window_System
From X11 man Page
1 #include <X11/Intrinsic.h>
2 #include <X11/StringDefs.h>
3 #include <X11/Xaw/Command.h>
4 #include <X11/Xaw/Form.h>
5
6 void main(int argc, char ** argv) { Widget topLevel, button, knopf;
7 /* Klassenname: Ex_1 */
8 topLevel = XtInitialize(argv[0], "Ex_1", NULL, 0,
9 &argc, argv);
10 button = XtCreateManagedWidget("button", formWidgetClass,
11 topLevel, NULL, 0);
12 knopf = XtCreateManagedWidget("this is the button",
13 commandWidgetClass,
14 button, NULL, 0);
15 XtRealizeWidget(topLevel);
16 /* Loop for events. */
17 XtMainLoop();
18 }
Source Code: Src/7/ex_1.c
1 ! 2 ! Resource file for ex_1 3 ! 4 #ifdef Class 5 Ex_1.title: class: ex_1 6 #else 7 Ex_1.title: program: ex_1 8 #endif 9 Ex_1.width: 300 10 Ex_1.height: 300 11 Ex_1.box.this is the button.bitmap: mensetmanus 12 Ex_1.box.this is the button.label: Press Me 13 Ex_1.box.this is the button.x: 10 14 Ex_1.box.this is the button.y: 10 15 Ex_1.box.this is the button.width: 100 16 Ex_1.box.this is the button.height: 50
Source Code: Src/7/Ex_1.rdb
% ex_1 % xrdb -load Ex_1.rdb % ex_1
1 XTerm*VT100*translations: #override \n\ 2 Shift <KeyPress> Prior: scroll-back(1,halfpage) \n\ 3 Shift <KeyPress> Next: scroll-forw(1,halfpage) \n\ 4 Shift <KeyPress> Select: select-cursor-start() \n\ 5 Shift <KeyPress> Insert: insert-selection(PRIMARY, CUT_BUFFER0) \n\ 6 ~Meta<KeyPress>: insert-seven-bit() \n\ 7 Meta<KeyPress>: insert-eight-bit() \n\ 8 Ctrl <Btn1Down>: popup-menu(mainMenu) \n\ 9 Lock Ctrl <Btn1Down>: popup-menu(mainMenu) \n\ 10 ~Meta <Btn1Down>: select-start() \n\ 11 ~Meta <Btn1Motion>: select-extend() \n\ 12 Ctrl <Btn2Down>: popup-menu(vtMenu) \n\ 13 Lock Ctrl <Btn2Down>: popup-menu(vtMenu) \n\ 14 ~Ctrl ~Meta <Btn2Down>: ignore() \n\ 15 ~Ctrl ~Meta <Btn2Up>: insert-selection(PRIMARY, CUT_BUFFER0) \n\ 16 Ctrl <Btn3Down>: popup-menu(fontMenu) \n\ 17 Lock Ctrl <Btn3Down>: popup-menu(fontMenu) \n\ 18 ~Ctrl ~Meta <Btn3Down>: start-extend() \n\ 19 ~Meta <Btn3Motion>: select-extend() \n\ 20 <BtnUp>: select-end(PRIMARY, CUT_BUFFER0) \n\ 21 <BtnDown>: bell(0)
Source Code: Src/7/Xterm
1 ! 2 ! Resource file for funny things 3 ! 4 *title: nicht wundern 5 *bitmap: tie_fighter
Source Code: Src/7/myXterm
A system call creates an interrupt.
1. The process mode changes from user to kernel mode.
2. The user process will be changed into a kernel process.
3. The process get special privileges:
a.) to modify kernel memory pages.
b.) to change the priority.
Questions:
The syscall function inside the OS:
#include "../port/systab.h"
long
syscall(Ureg *aur)
{
long ret;
ulong sp;
Ureg *ur;
ur = aur;
if(ur->psr & PSRPSUPER)
panic("recursive system call");
u->p->insyscall = 1;
u->p->pc = ur->pc;
...
splhi();
u->scallnr = ur->r7;
sp = ur->usp;
...
if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-sizeof(Sargs)))
validaddr(sp, sizeof(Sargs), 0);
u->s = *((Sargs*)(sp+1*BY2WD));
u->p->psstate = sysctab[u->scallnr];
ret = (*systab[u->scallnr])(u->s.args);
spllo();
return ret;
}
One:
1 #include <stdio.h>
2
3 int main() {
4 char buffer[2];
5 scanf("%s", buffer);
6 printf("buffer= -%s-\n", buffer);
7 }
Source Code: Src/7_1/readIn_1.c
Two:
1 #include <stdio.h>
2
3 int main() {
4 char buffer[2];
5 int n;
6 if ( (n = scanf("%s", buffer)) > sizeof(buffer) )
7 printf("bad things may happen\n");
8 printf("buffer= -%s-\n", buffer);
9 printf("sizeof(buffer)= -%d-\n", sizeof(buffer));
10 printf("n= -%d-\n", n);
11 }
Source Code: Src/7_1/readIn_2.c Three:
1 #include <stdio.h>
2
3 int main() {
4 char buffer[2];
5 int index = 0;
6
7 while ( index <= sizeof(buffer) ) {
8 printf("index= -%d-\n", index);
9 if ( index >= sizeof(buffer) ) {
10 printf("something bad will happen");
11 exit(1);
12 }
13 scanf("%c", &buffer[index++]);
14 }
15 printf("buffer= -%s-\n", buffer);
16 }
Source Code: Src/7_1/readIn_3.c
Stack
When g calls f, f must eventually return. The runtime system needs to know where to go back. ( g called f at address a, the right place to return is a+1).
On 1970 machines the return address was pushed on the stack. On modern machines the return address is stored in a register.
1 void hpb_f(int a, int b, int c) {
2 int buffer1[32];
3 char buffer2[32];
4 a = 5 + c;
5 buffer1[0] = 555;
6 buffer2[0] = 'a';
7 }
8
9 int main() {
10 hpb_f(1,2,3);
11 }
Source Code: Src/7_1/example1.c
1 .file "example1.c" 2 .section ".text" 3 .align 4 4 .global hpb_f 5 .type hpb_f, #function 6 .proc 020 7 hpb_f: 8 !#PROLOGUE# 0 9 save %sp, -272, %sp 10 !#PROLOGUE# 1 11 st %i0, [%fp+68] 12 st %i1, [%fp+72] 13 st %i2, [%fp+76] 14 ld [%fp+76], %g1 15 add %g1, 5, %g1 16 st %g1, [%fp+68] 17 mov 555, %g1 18 st %g1, [%fp-144] 19 mov 97, %g1 20 stb %g1, [%fp-176] 21 ret 22 restore 23 .size hpb_f, .-hpb_f 24 .align 4 25 .global main 26 .type main, #function 27 .proc 04 28 main: 29 !#PROLOGUE# 0 30 save %sp, -112, %sp 31 !#PROLOGUE# 1 32 mov 1, %o0 33 mov 2, %o1 34 mov 3, %o2 35 call hpb_f, 0 36 nop 37 mov %g1, %i0 38 ret 39 restore 40 .size main, .-main 41 .ident "GCC: (GNU) 3.4.5"
Source Code: Src/7_1/example1.s
The C code:
1 void function(char *str) {
2 char buffer[0];
3
4 strcpy(buffer,str);
5 }
6
7 int main() {
8 char large_string[256];
9 int i;
10
11 for( i = 0; i < 255; i++)
12 large_string[i] = 'A';
13
14 function(large_string);
15 }
Source Code: Src/7_1/example2.c
gcc -o example2 example2.c tiger 7_1 103 example2 Bus Error (core dumped) tiger 7_1 104 adb adb: cannot modify read-only variable 'fprs' core file = core -- program ``a.out'' on platform SUNW,A70 SIGBUS: Bus Error $v f8 = ffffffff f50 = ffffffff f9 = ffffffff f51 = ffffffff f52 = ffffffff f53 = ffffffff f54 = ffffffff ...
1 void function(int a) {
2 int firstV;
3 int *ret;
4
5 ret = &firstV ; /* example3.c:5: warning: assignment from incompatible pointer type */
6 (*ret) = 22;
7 printf("%d\n",firstV);
8
9 ret = &firstV - 8; /* this is it */
10 (*ret) = 22;
11 }
12
13 int main() {
14 int x;
15
16 x = 0;
17 function(42);
18 x = 1;
19 printf("%d\n",x);
20 }
Source Code: Src/7_1/example3.c
% gcc -S example3.c % more example3.s .file "example3.c" .section ".text" .align 4 .global function .type function, #function .proc 020 function: !#PROLOGUE# 0 save %sp, -144, %sp !#PROLOGUE# 1 st %i0, [%fp+68] st %i1, [%fp+72] st %i2, [%fp+76] add %fp, -40, %g1 add %g1, 12, %g1 st %g1, [%fp-44] ld [%fp-44], %i5 ld [%fp-44], %g1 ld [%g1], %g1 add %g1, 22, %g1 st %g1, [%i5] ret restore .size function, .-function .section ".rodata" .align 8 .asciz "%d0 .section ".text" .align 4 .global main .type main, #function .proc 020 main: !#PROLOGUE# 0 save %sp, -120, %sp !#PROLOGUE# 1 st %g0, [%fp-20] mov 11, %o0 mov 222, %o1 mov 3333, %o2 call function, 0 nop mov 1, %g1 st %g1, [%fp-20] sethi %hi(.LLC0), %g1 or %g1, %lo(.LLC0), %o0 ld [%fp-20], %o1 call printf, 0 nop nop ret restore .size main, .-main .ident "GCC: (GNU) 3.4.5"
1 #include <stdio.h>
2
3 int main() {
4 char *name[2];
5
6 name[0] = "/bin/sh";
7 name[1] = NULL;
8 execve(name[0], name, NULL);
9 }
Source Code: Src/7_1/shellCode.c
gcc -o shellCode shellCode.c spiegel.cs.rit.edu 7_1 153 shellCode sh-2.05b$ exit exit
name[0] = "/bin/sh"; name[1] = NULL; execve(name[0], name, NULL);
gdb shellcode GDB is free software and you are welcome to distribute copies of it under certain conditions; type "show copying" to see the conditions. There is absolutely no warranty for GDB; type "show warranty" for details. GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc... (gdb) disassemble main Dump of assembler code for function main: 0x8000130 <main>: pushl %ebp 0x8000131 <main+1>: movl %esp,%ebp 0x8000133 <main+3>: subl $0x8,%esp 0x8000136 <main+6>: movl $0x80027b8,0xfffffff8(%ebp) 0x800013d <main+13>: movl $0x0,0xfffffffc(%ebp) 0x8000144 <main+20>: pushl $0x0 0x8000146 <main+22>: leal 0xfffffff8(%ebp),%eax 0x8000149 <main+25>: pushl %eax 0x800014a <main+26>: movl 0xfffffff8(%ebp),%eax 0x800014d <main+29>: pushl %eax 0x800014e <main+30>: call 0x80002bc <__execve> 0x8000153 <main+35>: addl $0xc,%esp 0x8000156 <main+38>: movl %ebp,%esp 0x8000158 <main+40>: popl %ebp 0x8000159 <main+41>: ret End of assembler dump. (gdb) disassemble __execve Dump of assembler code for function __execve: 0x80002bc <__execve>: pushl %ebp 0x80002bd <__execve+1>: movl %esp,%ebp 0x80002bf <__execve+3>: pushl %ebx 0x80002c0 <__execve+4>: movl $0xb,%eax 0x80002c5 <__execve+9>: movl 0x8(%ebp),%ebx 0x80002c8 <__execve+12>: movl 0xc(%ebp),%ecx 0x80002cb <__execve+15>: movl 0x10(%ebp),%edx 0x80002ce <__execve+18>: int $0x80 0x80002d0 <__execve+20>: movl %eax,%edx 0x80002d2 <__execve+22>: testl %edx,%edx 0x80002d4 <__execve+24>: jnl 0x80002e6 <__execve+42> 0x80002d6 <__execve+26>: negl %edx 0x80002d8 <__execve+28>: pushl %edx 0x80002d9 <__execve+29>: call 0x8001a34 <__normal_errno_location> 0x80002de <__execve+34>: popl %edx 0x80002df <__execve+35>: movl %edx,(%eax) 0x80002e1 <__execve+37>: movl $0xffffffff,%eax 0x80002e6 <__execve+42>: popl %ebx 0x80002e7 <__execve+43>: movl %ebp,%esp 0x80002e9 <__execve+45>: popl %ebp 0x80002ea <__execve+46>: ret 0x80002eb <__execve+47>: nop End of assembler dump.
0x8000130 <main>: pushl %ebp 0x8000131 <main+1>: movl %esp,%ebp 0x8000133 <main+3>: subl $0x8,%esp
This is the procedure prelude. It first saves the old frame pointer, makes the current stack pointer the new frame pointer, and leaves space for the local variables. In this case its:
char *name[2];
or 2 pointers to a char. Pointers are a word long, so it leaves space for two words (8 bytes).
0x8000136 <main+6>: movl $0x80027b8,0xfffffff8(%ebp)
We copy the value 0x80027b8 (the address of the string "/bin/sh") into the first pointer of name[]. This is equivalent to:
name[0] = "/bin/sh";
0x800013d <main+13>: movl $0x0,0xfffffffc(%ebp)
We copy the value 0x0 (NULL) into the seconds pointer of name[]. This is equivalent to:
name[1] = NULL;
The actual call to execve() starts here.
0x8000144 <main+20>: pushl $0x0
We push the arguments to execve() in reverse order onto the stack. We start with NULL.
0x8000146 <main+22>: leal 0xfffffff8(%ebp),%eax
We load the address of name[] into the EAX register.
0x8000149 <main+25>: pushl %eax
We push the address of name[] onto the stack.
0x800014a <main+26>: movl 0xfffffff8(%ebp),%eax
We load the address of the string "/bin/sh" into the EAX register.
0x800014d <main+29>: pushl %eax
We push the address of the string "/bin/sh" onto the stack.
0x800014e <main+30>: call 0x80002bc <__execve>
Call the library procedure execve(). The call instruction pushes the IP onto the stack.
Now execve(). Keep in mind we are using a Intel based Linux system. The syscall details will change from OS to OS, and from CPU to CPU. Some will pass the arguments on the stack, others on the registers. Some use a software interrupt to jump to kernel mode, others use a far call. Linux passes its arguments to the system call on the registers, and uses a software interrupt to jump into kernel mode.
0x80002bc <__execve>: pushl %ebp 0x80002bd <__execve+1>: movl %esp,%ebp 0x80002bf <__execve+3>: pushl %ebx
The procedure prelude.
0x80002c0 <__execve+4>: movl $0xb,%eax
Copy 0xb (11 decimal) onto the stack. This is the index into the syscall table. 11 is execve.
0x80002c5 <__execve+9>: movl 0x8(%ebp),%ebx
Copy the address of "/bin/sh" into EBX.
0x80002c8 <__execve+12>:movl 0xc(%ebp),%ecx
Copy the address of name[] into ECX.
0x80002cb <__execve+15>:movl 0x10(%ebp),%edx
Copy the address of the null pointer into %edx.
0x80002ce <__execve+18>:int $0x80
Change into kernel mode.
1 #include <stdlib.h>
2
3 int main() {
4 exit(0);
5 }
Source Code: Src/7_1/exit.c
[aleph1]$ gcc -o exit -static exit.c [aleph1]$ gdb exit GDB is free software and you are welcome to distribute copies of it under certain conditions; type "show copying" to see the conditions. There is absolutely no warranty for GDB; type "show warranty" for details. GDB 4.15 (i586-unknown-linux), Copyright 1995 Free Software Foundation, Inc... (no debugging symbols found)... (gdb) disassemble _exit Dump of assembler code for function _exit: 0x800034c <_exit>: pushl %ebp 0x800034d <_exit+1>: movl %esp,%ebp 0x800034f <_exit+3>: pushl %ebx 0x8000350 <_exit+4>: movl $0x1,%eax 0x8000355 <_exit+9>: movl 0x8(%ebp),%ebx 0x8000358 <_exit+12>: int $0x80 0x800035a <_exit+14>: movl 0xfffffffc(%ebp),%ebx 0x800035d <_exit+17>: movl %ebp,%esp 0x800035f <_exit+19>: popl %ebp 0x8000360 <_exit+20>: ret 0x8000361 <_exit+21>: nop 0x8000362 <_exit+22>: nop 0x8000363 <_exit+23>: nop End of assembler dump.
standard execution of system calls/libraries
See Leon Reznik's research area
|
|