-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake-sshd.c
281 lines (241 loc) · 6.59 KB
/
fake-sshd.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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id: fake-sshd.c,v 1.9 2012/01/25 09:07:44 james.stevenson Exp $
*
* Author:
* NAME: James Stevenson
* WWW: http://www.stev.org
*
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <libssh/libssh.h>
#include <libssh/server.h>
#include <time.h>
int use_syslog = 0;
int verbose = 0;
void logger(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
printf("\n");
if (use_syslog)
vsyslog(LOG_NOTICE, fmt, ap);
va_end(ap);
}
void handle_sigchild(int signum) {
int status = -1;
int pid = 0;
do {
int pid = waitpid(-1, &status, WNOHANG);
if (verbose > 0)
logger("Process %d Exited", pid);
} while(pid > 0);
}
void print_help(FILE *fp, char *app) {
fprintf(fp, "Usage: %s [<options>]\n", app);
fprintf(fp, "\n");
fprintf(fp, "\t-a <secs> Failed Auth delay\n");
fprintf(fp, "\t-b <str> Set the banner\n");
fprintf(fp, "\t-h Print this help and exit\n");
fprintf(fp, "\t-m <n> Max attempts per connection\n");
fprintf(fp, "\t-p <port> Port to listen on\n");
fprintf(fp, "\t-r <file> Path to rsa key\n");
fprintf(fp, "\t-d <file> Path to dsa key\n");
fprintf(fp, "\t-s Log to syslog\n");
fprintf(fp, "\t-t <secs> Timeout\n");
fprintf(fp, "\t-v Verbose. Repeat for more info\n");
fprintf(fp, "\t-w <secs> Delay after connection\n");
fprintf(fp, "\t-z Multiple Delay by 2 each failure\n");
fprintf(fp, "\n");
}
int main(int argc, char **argv) {
ssh_bind sshbind;
ssh_session session;
int r = -1;
int auth = 0;
int c;
int delay = 0;
char *port = "22";
char *dsakey = "/home/james/etc/ssh/ssh_host_dsa_key";
char *rsakey = "/home/james/etc/ssh/ssh_host_rsa_key";
char *banner = 0;
int timeout = 0;
int authdelay = 0;
int doubledelay = 0;
int maxfail = 0;
while( (c = getopt(argc, argv, "a:b:hm:p:r:d:st:vw:z")) != -1) {
switch(c) {
case 'a':
authdelay = atoi(optarg);
break;
case 'b':
banner = optarg;
break;
case 'p':
port = optarg;
break;
case 'h':
print_help(stdout, argv[0]);
exit(EXIT_SUCCESS);
break;
case 'm':
maxfail = atoi(optarg);
break;
case 'r':
rsakey = optarg;
break;
case 'd':
dsakey = optarg;
break;
case 's':
use_syslog = 1;
break;
case 't':
timeout = atoi(optarg);
break;
case 'v':
verbose++;
break;
case 'w':
delay = atoi(optarg);
break;
case 'z':
doubledelay = 1;
break;
default:
break;
}
}
if (dsakey == NULL) {
if (access("/etc/ssh/ssh_host_rsa_key", R_OK) == 0)
rsakey = "/etc/ssh/ssh_host_rsa_key";
}
if (rsakey == NULL) {
if (access("/etc/ssh/ssh_host_dsa_key", R_OK) == 0)
dsakey = "/etc/ssh/ssh_host_dsa_key";
}
if (rsakey == NULL || dsakey == NULL) {
print_help(stderr, argv[0]);
exit(EXIT_FAILURE);
}
sshbind = ssh_bind_new();
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT_STR, port);
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, dsakey);
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, rsakey);
if (banner != NULL)
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BANNER, banner);
if (ssh_bind_listen(sshbind) < 0) {
logger("Error listening to socket: %s", ssh_get_error(sshbind));
exit(EXIT_FAILURE);
}
signal(SIGCHLD, &handle_sigchild);
restart:
session = ssh_new();
if (timeout > 0)
ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &timeout);
r = ssh_bind_accept(sshbind, session);
if (r == SSH_ERROR) {
logger("Error accepting connection: %s", ssh_get_error(sshbind));
goto restart;
}
int ret = fork();
if (fork < 0) {
logger("fork: %s", strerror(errno));
logger("exiting ...");
exit(EXIT_FAILURE);
}
int sockfd = ssh_get_fd(session);
struct sockaddr_in peer;
socklen_t peer_len = sizeof(peer);
char *peername = 0;
int attempts = 0;
if (ret > 0) {
if (verbose > 0)
logger("Started Process %d", ret);
ssh_free(session);
goto restart;
}
ret = getpeername(sockfd, (struct sockaddr *) &peer, &peer_len);
peername = inet_ntoa(peer.sin_addr);
logger("Connection From %s:%d", peername, peer.sin_port);
if (ssh_handle_key_exchange(session)) {
logger("ssh_handle_key_exchange: %s", ssh_get_error(session));
goto error;
}
do {
ssh_message message = ssh_message_get(session);
if (message == NULL)
break;
switch(ssh_message_type(message)) {
case SSH_REQUEST_AUTH:
switch(ssh_message_subtype(message)) {
case SSH_AUTH_METHOD_PASSWORD:
attempts++;
time_t timer;
char t_buffer[26];
struct tm* tm_info;
time(&timer);
tm_info = localtime(&timer);
strftime(t_buffer, 26, "%Y-%m-%dT%H:%M:%S%z", tm_info);
logger("TIME: %s, IP: %s USER: %s PASS: %s", t_buffer, peername, ssh_message_auth_user(message), ssh_message_auth_password(message));
if (authdelay > 0)
sleep(authdelay);
if (doubledelay)
authdelay *= 2;
if (attempts > maxfail) {
if (verbose > 1)
logger("Max failures reached");
ssh_message_free(message);
goto error;
}
case SSH_AUTH_METHOD_NONE:
if (verbose > 1)
logger("AUTH_METHOD_NONE Requested");
// break missing on purpose
default:
if (verbose > 1)
logger("REQUEST_AUTH: %d", ssh_message_subtype(message));
ssh_message_auth_set_methods(message, SSH_AUTH_METHOD_PASSWORD);
ssh_message_reply_default(message);
break;
}
break;
default:
if (verbose > 0)
logger("Message Type: %d", ssh_message_type(message));
ssh_message_reply_default(message);
break;
}
ssh_message_free(message);
} while(auth == 0);
error:
ssh_disconnect(session);
ssh_free(session);
ssh_bind_free(sshbind);
logger("Connection Closed From %s", peername);
return 0;
}