-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvuln.c
More file actions
executable file
·165 lines (139 loc) · 4.27 KB
/
vuln.c
File metadata and controls
executable file
·165 lines (139 loc) · 4.27 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <alloca.h>
#include <ctype.h>
#include "my_malloc.h"
#define MAX_GRP 100
#define err_abort(x) do { \
if (!(x)) {\
fprintf(stderr, "Fatal error: %s:%d: ", __FILE__, __LINE__); \
perror(""); \
exit(1);\
}\
} while (0)
void print_escaped(FILE *fp, const char* buf, unsigned len) {
int i;
for (i=0; i < len; i++) {
if (isprint(buf[i]))
fputc(buf[i], stderr);
else fprintf(stderr, "\\x%02hhx", buf[i]);
}
}
/************ Function vulnerable to buffer overflow on stack ***************/
int auth(const char *username, int ulen, const char *pass, int plen) {
char *user = alloca(LEN2 + (random() % LEN2));
// make offsets unique for each group
bcopy(username, user, ulen); // possible buffer overflow
unsigned l = (plen < ulen) ? plen : ulen;
return (strncmp(user, pass, l) == 0);
}
int wrauth(const char *username, int ulen, const char *pass, int plen) {
return auth(username, ulen, pass, plen);
}
int login_attempts;
void g(const char *username, int ulen, const char *pass, int plen) {
int authd=0;
char *s1 = "/bin/ls";
char *s2 = "/bin/false";
if (RANDOM)
authd |= wrauth(username, ulen, pass, plen);
else authd |= auth(username, ulen, pass, plen);
if (authd) {
// Successfully authenticated
fprintf(stderr, "Authentication succeeded, executing ls\n");
err_abort(execl(s1, "ls", NULL)>=0); // Execute a shell, or
}
else { // Authentication failure
fprintf(stderr, "Login denied, ");
if (login_attempts++ > 3) {
fprintf(stderr, "executing /bin/false\n");
err_abort(execl(s2, "false", NULL)>=0); // a program that prints an error
}
else fprintf(stderr, "try again\n");
}
}
#ifndef ASM_ONLY
void padding() {
int i, z;
#include "padding.h"
}
#endif
void ownme() {
fprintf(stderr, "ownme called\n");
}
int main_loop(unsigned seed) {
int nread;
char *user=NULL, *pass=NULL;
unsigned ulen=0, plen=0;
srandom(seed);
unsigned s = (unsigned)random();
s = LEN1 + (s % LEN1);
char *rdbuf = (char*)alloca(s);
char *tbuf;
unsigned tbufsz = ((unsigned)random()) % LEN1;
tbuf = (char*)alloca(tbufsz);
do {
err_abort((nread = read(0, rdbuf, s-1)) >= 0);
if (nread == 0) {
fprintf(stderr, "vuln: quitting\n");
return 0;
}
fprintf(stderr, "vuln: Received:'");
print_escaped(stderr, rdbuf, nread);
fprintf(stderr, "'\n");
rdbuf[nread] = '\0'; // null-terminate
switch (rdbuf[0]) {
case 'e': // echo command: e <string_to_echo>
printf(&rdbuf[2]);
fflush(stdout);
break;
case 'u': // provide username
ulen = nread-3; // skips last char
user = malloc(ulen);
bcopy(&rdbuf[2], user, ulen);
break;
case 'p': // provide username
pass = malloc(plen);
plen = nread-3;
bcopy(&rdbuf[2], pass, plen);
break;
case 'l': { // login using previously supplied username and password
if (user != NULL && pass != NULL) {
fprintf(stderr, "vuln: Got user=%s, pass=%s\n", user, pass);
g(user, ulen, pass, plen);
free(pass);
free(user);
user=pass=NULL;
ulen=0; plen=0;
}
else fprintf(stderr, "vuln: Use u and p commands before logging in\n");
break;
}
case 'q':
fprintf(stderr, "vuln: quitting\n");
return 0;
default:
fprintf(stderr, "vuln: Invalid operation. Valid commands are:\n");
fprintf(stderr, "\te <data>: echo <data>\n");
fprintf(stderr, "\tu <user>: enter username\n");
fprintf(stderr, "\tp <pass>: enter password\n");
fprintf(stderr,
"\tl: login using previously provided username/password\n");
fprintf(stderr, "\tq: quit\n");
break;
}
} while (1);
}
int main(int argc, char *argv[]) {
unsigned seed=GRP;
if (argc >= 2) seed = atoi(argv[1]);
if (seed > MAX_GRP) {
fprintf(stderr, "Usage: %s <group_id>\n", argv[0]);
fprintf(stderr, "<group_id> must be between 0 and %d\n", MAX_GRP);
exit(1);
}
return main_loop(seed);
};