-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverflow_example.c
More file actions
39 lines (34 loc) · 1.17 KB
/
Copy pathoverflow_example.c
File metadata and controls
39 lines (34 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Ejemplo clásico de stack buffer overflow.
//
// El buffer mide 16 bytes. strcpy no comprueba el tamaño del input.
// Con >=21 caracteres, el primer byte tras el padding cae sobre auth_flag,
// lo que basta para "burlar" la autenticación.
//
// Compilar SIN protecciones para reproducir el bug:
// gcc -g -fno-stack-protector -z execstack -no-pie \
// overflow_example.c -o overflow_example
// (lo hace make 0x320 automáticamente)
#include <stdio.h>
#include <string.h>
int check_authentication(char *password) {
int auth_flag = 0;
char password_buffer[16];
strcpy(password_buffer, password); // ⚠ overflow
if (strcmp(password_buffer, "supersecret") == 0) auth_flag = 1;
if (strcmp(password_buffer, "letmein") == 0) auth_flag = 1;
return auth_flag;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Uso: %s <password>\n", argv[0]);
return 1;
}
if (check_authentication(argv[1])) {
printf("\n-=-=-=-=-=-=-=-=-=-=-=-=\n");
printf(" Acceso garantizado.\n");
printf("-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
} else {
printf("\nAcceso DENEGADO.\n");
}
return 0;
}