-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathegg-vulnerable-server.c
212 lines (149 loc) · 4.89 KB
/
egg-vulnerable-server.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
Jean-Pierre LESUEUR (@DarkCoderSc)
jplesueur@phrozen.io
https://www.phrozen.io/
https://github.com/darkcodersc
License : MIT
---
SLAE32 Assignment 3 : Linux x86-32 Egg Hunter Research.
---
gcc egg-reallife.c -o egg-reallife -z execstack -no-pie -fno-stack-protector -pthread
Warning: This C program is willingly vulnerable to buffer overflow which could led to remote code execution.
(!) Do not copy paste pieace of code without real caution (!)
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <ctype.h>
/****************************************************************************************************
Server Thread Child Thread: Egg Host (cmd=1) for phase n°1.
In our scenario this would be phase n°1.
Imagine CacheMe() as a perfectly secured function to cache some data in memory. We have can cache
up to 1KiB of data per thread. Far sufficient to host our real shellcode payload.
****************************************************************************************************/
void *CacheMe(void *param) {
int client = (int)param;
char buffer[1024]; // Buffer that will contain our future egg + shellcod
// Waiting for data from client.
int result = recv(client, buffer, sizeof(buffer), 0);
if (result <= 0) {
printf("Could not receive data from client.\n");
} else
printf("Buffer successfully filled with %d bytes of data.\n", result);
///
close(client);
}
/****************************************************************************************************
Server Thread Child Thread: Buffer Overflow Location (cmd=2) for phase n°2.
In our scenario this would be phase n°2.
Image ExploitMe() as a function vulnerable to buffer overflow but with a small buffer.
However it is sufficient to place our egg hunter shellcode here to locate our second and real shellcode
payload.
****************************************************************************************************/
void *ExploitMe(void *param) {
char feedback[60];
char buffer[200];
///
int client = (int)param;
int result = recv(client, buffer, sizeof(buffer), 0);
printf("Received %d bytes for feedback.");
strcpy(feedback, buffer); // who cares about security? :-P
}
/****************************************************************************************************
Server Thread
This server accept two commands:
1. Cache some data in child thread stack (memory).
2. Write data to an uncontrolled buffer (In our scenario, a fake rating system).
****************************************************************************************************/
void *Server() {
printf("Server thread has started.\n");
/*
Create a new socket
*/
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s == 0) {
printf("Could not create socket");
pthread_exit(NULL);
}
printf("Socket created with handle:%d\n", s);
/*
Avoid error already in use.
*/
int optval = 1;
int result = setsockopt(s, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &optval, sizeof(int));
if (result == -1) {
printf("Could not call setsockopt().");
close(s);
pthread_exit(NULL);
}
/*
Bind socket to port.
*/
struct sockaddr_in saddr_in;
saddr_in.sin_family = AF_INET;
saddr_in.sin_port = htons(1403); // Listening on port 1403.
saddr_in.sin_addr.s_addr = 16777343; // Listenning on address 127.0.0.1.
result = bind(s, (struct sockaddr*)&saddr_in, sizeof(struct sockaddr_in));
if (result == -1) {
printf("Could not bind socket.\n");
close(s);
pthread_exit(NULL);
}
printf("Socket successfully binded.\n");
/*
Start listening
*/
result = listen(s, 5);
if (result == -1) {
printf("Could not listen.\n");
close(s);
pthread_exit(NULL);
}
printf("Listening...\n");
/*
Wait for new clients to connect.
*/
for (;;) {
int client = accept(s, NULL, NULL);
///
if (client < 0)
break;
printf("New client connected our server with handle: %d\n", client);
char cmd[1];
result = recv(client, cmd, sizeof(cmd), 0);
if (result <= 0)
continue;
if (!isdigit(*cmd)) {
printf("Bad command format.\n");
continue;
}
int icmd = atoi(cmd);
printf("command=[%d]\n", icmd);
pthread_t thread;
switch(icmd) {
case 1:
pthread_create(&thread, NULL, CacheMe, (void *)client);
break;
case 2:
pthread_create(&thread, NULL, ExploitMe, (void *)client);
break;
default:
close(client);
}
}
close(s);
///
pthread_exit(NULL);
}
/****************************************************************************************************
Program Entry Point
****************************************************************************************************/
void main() {
pthread_t thread;
pthread_create(&thread, NULL, Server, NULL); // Create a new thread.
pthread_join(thread, NULL); // Wait for thread to finish his task.
return;
}