Repository navigation
Expand file tree
/
Copy pathLogParser.c
More file actions
79 lines (65 loc) · 1.9 KB
/
Copy pathLogParser.c
File metadata and controls
79 lines (65 loc) · 1.9 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* LogParser v1.0 - Simple Log File Parser
*
* Author: Ishan Oshada
* Date: 2026-11-11
*
* WARNING: This application contains an INTENTIONAL format string
* vulnerability for educational / CTF purposes. DO NOT run in
* production.
*
* Build:
* gcc -m32 -fno-stack-protector -no-pie -z norelro logparser.c -o logparser
*
* Vulnerable line:
* printf(user_input); <-- missing "%s"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_LINE 256
/* ---- Fake "log parser" helpers ---- */
void parse_line(const char *line) {
printf("[LOG] ");
printf(line); /* <-- VULNERABILITY: format string */
printf("\n");
}
void show_menu(void) {
printf("=========================================\n");
printf(" LogParser v1.0 (CTF build) \n");
printf("=========================================\n");
printf("Commands:\n");
printf(" parse <text> - Parse a log line\n");
printf(" help - Show this menu\n");
printf(" quit - Exit\n");
printf("=========================================\n");
}
int main(void) {
char input[MAX_LINE];
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stdin, NULL, _IONBF, 0);
show_menu();
while (1) {
printf("logparser> ");
if (!fgets(input, sizeof(input), stdin))
break;
/* strip trailing newline */
input[strcspn(input, "\n")] = '\0';
if (strcmp(input, "quit") == 0)
break;
if (strcmp(input, "help") == 0) {
show_menu();
continue;
}
if (strncmp(input, "parse ", 6) == 0) {
/* user-controlled string goes straight into printf */
parse_line(input + 6);
} else {
/* even unknown commands hit the vulnerable path */
parse_line(input);
}
}
printf("bye.\n");
return 0;
}