-
Notifications
You must be signed in to change notification settings - Fork 0
/
stutter.c
308 lines (254 loc) · 7.99 KB
/
stutter.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright (c) 2009-2010 Janne Johansson <jj@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include "kex.h"
#include "myproposal.h" // taken from real SSH
void keep_busy(int, int);
// #define JJDEBUG 1
const char *mystring="SSH-2.0-OpenSSH_5.3\r\n";
char proposal_buffer[35000];
int
main()
{
int s, s2, sockaddr_len, one=1;
int fake_packet_len, proposals;
int buffer_pointer, temp_int;
pid_t mypid;
struct sockaddr_in min_sockaddr;
struct sigaction mysig;
char *myproposal[PROPOSAL_MAX] = { KEX_SERVER };
char kexinit_buffer[4096];
s = socket(AF_INET, SOCK_STREAM, 0);
if (!s) {
err(1,"socket() call failed");
}
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
&one, sizeof(one))) {
err(1,"setsockopt() RCVBUF call failed");
}
if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT,
&one, sizeof(one))) {
err(1,"setsockopt() REUSEADDR call failed");
}
//if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
// &one, sizeof(one))) {
// err(1,"setsockopt() SNDBUF call failed");
//}
sockaddr_len = sizeof(min_sockaddr);
memset (&min_sockaddr, 0, sockaddr_len);
min_sockaddr.sin_len=sockaddr_len;
min_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
min_sockaddr.sin_family=AF_INET;
min_sockaddr.sin_port = htons(9876);
#if JJDEBUG
printf("port=%d\n", ntohs(min_sockaddr.sin_port));
#endif
if (bind(s, (struct sockaddr *)&min_sockaddr, sockaddr_len)) {
err(1,"bind() call failed");
}
if (listen(s, 100)) {
err(1,"listen() call failed");
}
/* set up sigaction to prevent zombies, I dont want to know about
nor reap children processes */
sigemptyset(&mysig.sa_mask);
mysig.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT;
mysig.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &mysig, NULL);
/* Calculate the fake proposal packet once */
temp_int=0;
kexinit_buffer[0]=20; // SSH_MSG_KEXINIT = 20
bcopy(mystring, &kexinit_buffer[1], 16); // using SSH2.0- as 'random bytes'
buffer_pointer=17; // we have written 17 bytes into it
for (proposals=0; proposals < sizeof(myproposal)/sizeof(*myproposal); proposals++)
{
int prop_len=strlen(myproposal[proposals]);
temp_int=htonl(prop_len);
bcopy(&temp_int, &kexinit_buffer[buffer_pointer], 4);
buffer_pointer += 4;
if (prop_len) {
bcopy(myproposal[proposals],
&kexinit_buffer[buffer_pointer], prop_len);
}
buffer_pointer += prop_len;
#ifdef JJDEBUG
printf("prop len %d, hex %x\n",prop_len, prop_len);
printf("size now is: %d\n",buffer_pointer);
#endif // JJDEBUG
}
kexinit_buffer[buffer_pointer++]=0; // boolean for kex-packet-follows
temp_int=0;
bcopy(&kexinit_buffer[buffer_pointer], &temp_int, 4); // last 4 zero bytes.
buffer_pointer += 4;
// now we know the packet length.
temp_int=htonl(1+16+1+4 + buffer_pointer);
#ifdef JJDEBUG
printf("kexinit size %d, buffer pointer %d + 22\n",
ntohl(temp_int), buffer_pointer);
#endif // JJDEBUG
// KEXINIT + cookie + boolean + reserved uint32 + the payload
bcopy(&temp_int, &proposal_buffer[0], 4);
fake_packet_len=(((1+16+1+4 + buffer_pointer + 4 + 1)/8)+1)*8;
#ifdef JJDEBUG
printf("packet_len is now thought to be %d\n", fake_packet_len);
#endif // JJDEBUG
// minimum random padding (4) plus the lenght byte for it,
// then rounded to nearest multiple of eight
//padding_length is the even-8-byte-len minus the real length
proposal_buffer[4]=fake_packet_len - (1+16+1+4 + buffer_pointer + 4 + 1);
#ifdef JJDEBUG
printf("padding size %d\n",
fake_packet_len - (1+16+1+4 + buffer_pointer + 4 + 1));
#endif // JJDEBUG
// packet_len includes the calculated padding length
// add the kexinit payload
bcopy(&kexinit_buffer[0], &proposal_buffer[5], buffer_pointer);
#ifdef JJDEBUG
printf("kexinit size is: %d\n",buffer_pointer);
#endif // JJDEBUG
// add padding (the fixed OpenSSH version string comes handy again)
bcopy(mystring, &proposal_buffer[5+buffer_pointer],
(size_t)proposal_buffer[4]);
// final size calculation of the complete SSH packet.
// size should be 1 for the padding length byte, plus
// the kexinit packet (buffer_pointer), plus the random
// padding (stored in the fifth byte already), plus 4 for
// the empty MAC checksum.
#ifdef JJDEBUG
printf("SSH packet size %d\n", fake_packet_len);
#endif // JJDEBUG
temp_int=htonl(fake_packet_len);
bcopy(&temp_int, &proposal_buffer[0], sizeof(temp_int));
// now the packet is good to send.
/* start the loop */
mypid=getpid();
while (1) {
s2 = accept(s, (struct sockaddr *)&min_sockaddr, &sockaddr_len);
if (s2 != -1) {
switch (fork()) {
case 0:
keep_busy(s2, fake_packet_len);
_exit(0);
case -1:
/* in case we cant fork, no action right now */
break;
default:
/* we are the parent, close the for-the-child socket and
run the loop again */
close(s2);
break;
}
} else {
err(1,"accept() call failed");
}
}
if (errno)
{
perror("exiting");
}
return (0);
}
void
keep_busy(int s2, int buffer_size) {
int socklen, gai_return;
time_t now, then;
// double timediff;
struct sockaddr_storage peersock;
char buf[35000];
char remotename[NI_MAXHOST];
if (chdir("/var/empty")==0 && s2 > 2) {
daemon(0,0);
}
setpriority(PRIO_PROCESS, 0, 10);
socklen=sizeof(peersock);
if(getpeername(s2, (struct sockaddr *)&peersock, &socklen)==0) {
gai_return=getnameinfo((struct sockaddr *)&peersock, peersock.ss_len,
remotename, sizeof(remotename),
NULL, 0, NI_NUMERICHOST);
if (gai_return) {
snprintf(remotename, sizeof(remotename), "%s",
gai_strerror(gai_return));
syslog(LOG_WARNING, "%s", remotename);
}
} else {
syslog(LOG_INFO, "got no peername");
}
if (time(&now) != -1 ) {
strftime(buf, sizeof(buf), "%F %T", localtime(&now));
syslog(LOG_INFO, "caught %s at %s, pid %d",
remotename, buf, getpid());
memset (&buf, 0, sizeof(buf));
}
memset (&buf, 0, sizeof(buf));
snprintf(buf, strlen(mystring), "%s", mystring);
write(s2,&buf,strlen(mystring));
memset (&buf, 0, sizeof(buf));
/* old "repeat what the other side said" code
retcode=0;
while ((loops < 5) && (retcode != -1))
{
switch(retcode)
{
case 0:
sleep(1);
loops++;
break;
default:
write(s2,&buf,retcode);
#if JJDEBUG
printf("%c\n", buf[0]);
#endif
sleep(1);
break;
}
retcode=read(s2, &buf, 1);
}
*/
write(s2, &proposal_buffer, buffer_size);
//when they respond to this, it will hopefully take a lot of time
while (read(s2, buf, 1) == 1) {
sleep(1);
#ifdef JJDEBUG
printf("got this byte %x", buf[0]);
#endif
}
if(getpeername(s2, (struct sockaddr *)&peersock, &socklen)==0) {
getnameinfo((struct sockaddr *)&peersock, socklen,
remotename, sizeof(remotename),
NULL, 0, 0);
}
if (time(&then) != -1 ) {
memset (&buf, 0, sizeof(buf));
strftime(buf, sizeof(buf), "%F %T", localtime(&then));
syslog(LOG_INFO, "exited at %s, kept %s busy for %d seconds, pid %d",
buf, remotename, (int)difftime(then, now), getpid());
}
}