ex:vcode:start
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| ex:vcode:start [2023/03/08 09:26] – created ebaer | ex:vcode:start [2023/07/31 05:51] (current) – titannet | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Vulnerable Code ====== | ====== Vulnerable Code ====== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | |||
| + | void vulnerable_function(char* input) { | ||
| + | char buffer[10]; | ||
| + | strcpy(buffer, | ||
| + | } | ||
| + | |||
| + | int main() { | ||
| + | char input_string[] = "This input is too long!"; | ||
| + | vulnerable_function(input_string); | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ====== Stack 1 ====== | ||
| + | |||
| + | <file example1.c c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | |||
| + | char buffer[20]; | ||
| + | |||
| + | int copy_buffer(char *input_buffer) { | ||
| + | strcpy(buffer, | ||
| + | | ||
| + | printf(" | ||
| + | } | ||
| + | | ||
| + | |||
| + | int main(int argc, char *argv[]) { | ||
| + | |||
| + | | ||
| + | |||
| + | |||
| + | if (argc < 2) { | ||
| + | printf(" | ||
| + | printf(" | ||
| + | exit(0); | ||
| + | } | ||
| + | |||
| + | copy_buffer(argv[1]); | ||
| + | | ||
| + | printf(" | ||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Walkthrough Windbg ===== | ||
| + | |||
| + | <code bash> | ||
| + | cl.exe /Zi /GS- /EH- | ||
| + | # debugging symbols, no stack security, no seh (not working??) | ||
| + | |||
| + | bp $exentry (if no symbols) | ||
| + | bp example1!main | ||
| + | # F5 | ||
| + | # debug -> make sure that source mode is unchecked | ||
| + | |||
| + | # x86 calling convention: parameter 1-3 on stack | ||
| + | # x86 function prolog: push ebp, mov ebp, esp | ||
| + | # [ebp+8] == local variable | ||
| + | |||
| + | # dd esp -> first address on stack == return pointer | ||
| + | # dd ebp -> old base pointer | ||
| + | # dd poi(ebp+8) | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ====== Stack 2 ====== | ||
| + | |||
| + | < | ||
| + | titan.net2k@protonmail.ch | ||
| + | show me an example of a buffer overflow for windows in c that uses commandline input and a seperate function | ||
| + | |||
| + | Sure! Here's an example of a buffer overflow vulnerability in C for Windows that uses command-line input and a separate function: | ||
| + | |||
| + | c | ||
| + | Copy code | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | void vulnerable_function(char *input) { | ||
| + | char buffer[10]; | ||
| + | strcpy(buffer, | ||
| + | } | ||
| + | |||
| + | int main(int argc, char *argv[]) { | ||
| + | vulnerable_function(argv[1]); | ||
| + | printf(" | ||
| + | return 0; | ||
| + | } | ||
| + | In this example, the program takes a string argument from the command line and passes it to the vulnerable_function function, which copies it into a fixed-size buffer of size 10 using the strcpy function. If the argument passed in is longer than the buffer size, the strcpy function will write beyond the bounds of the buffer and overwrite other data on the stack. | ||
| + | |||
| + | To exploit this vulnerability, | ||
| + | |||
| + | To prevent this type of vulnerability, | ||
| + | </ | ||
| + | |||
| + | |||
| + | ====== Heap 2 ====== | ||
| + | |||
| <code c> | <code c> | ||
| Line 31: | Line 141: | ||
| + | </ | ||
| + | ====== Heap2-2 ====== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Use after free example, original code from https:// | ||
| + | |||
| + | #define SERVICE_SIZE 32 | ||
| + | |||
| + | struct AuthStruct { | ||
| + | char name[32]; | ||
| + | int is_authenticated; | ||
| + | }; | ||
| + | |||
| + | struct AuthStruct *auth; | ||
| + | char *service; | ||
| + | |||
| + | int main(int argc, char **argv) | ||
| + | { | ||
| + | char line[128]; | ||
| + | |||
| + | while(1) { | ||
| + | printf(" | ||
| + | if(fgets(line, | ||
| + | | ||
| + | if(strncmp(line, | ||
| + | auth = malloc(sizeof(*auth)); | ||
| + | memset(auth, | ||
| + | if(strlen(line + 5) < 31) { | ||
| + | strcpy(auth-> | ||
| + | } | ||
| + | } | ||
| + | if(strncmp(line, | ||
| + | free(auth); | ||
| + | } | ||
| + | if(strncmp(line, | ||
| + | service = malloc(SERVICE_SIZE); | ||
| + | strcpy(service, | ||
| + | } | ||
| + | if(strncmp(line, | ||
| + | if(auth-> | ||
| + | printf(" | ||
| + | } else { | ||
| + | printf(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
ex/vcode/start.1678263993.txt.gz · Last modified: 2023/03/08 09:26 by ebaer
