forked from kingtuna/sshpot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.c
More file actions
188 lines (142 loc) · 4.93 KB
/
Copy pathauth.c
File metadata and controls
188 lines (142 loc) · 4.93 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "auth.h"
#include "config.h"
#include <libssh/libssh.h>
#include <libssh/server.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#define VAL(str) #str
extern int debug;
/* Stores the current UTC time. Returns 0 on error. */
static int get_utc(struct connection *c)
{
time_t t;
t = time(NULL);
return strftime(c->con_time, MAXBUF, "%Y-%m-%d %H:%M:%S", gmtime(&t));
}
/* Write interesting information about a connection attempt to LOGFILE.
* Returns -1 on error. */
static int log_attempt(struct connection *c)
{
FILE *f;
int r;
if ((f = fopen(LOGFILE, "a+")) == NULL)
{
fprintf(stderr, "Unable to open %s\n", LOGFILE);
return -1;
}
if (get_utc(c) <= 0)
{
fprintf(stderr, "Error getting time\n");
fclose(f);
return -1;
}
c->user = (char*) ssh_message_auth_user(c->message);
c->pass = (char*) ssh_message_auth_password(c->message);
if (c->banner == NULL)
c->banner = "No client ssh banner";
if (debug)
printf("%s %s %s %s (%s)\n", c->con_time, c->client_ip, c->user, c->pass, c->banner);
r = fprintf(f, "# %s (%s)\n%s\t%s\t%s\t%s\n", c->client_ip, c->banner, c->con_time, c->client_ip, c->user, c->pass);
fclose(f);
return r;
}
/* Logs password auth attempts. Always replies with SSH_MESSAGE_USERAUTH_FAILURE. */
int handle_auth(ssh_session session, char *client_ip)
{
unsigned int i, msg, auth_msg;
struct connection con;
int timeout = CTIMEOUT;
con.session = session;
strcpy(con.client_ip, client_ip);
ssh_options_set(con.session, SSH_OPTIONS_TIMEOUT, (const void *)&timeout);
/* Perform key exchange. */
if (ssh_handle_key_exchange(con.session))
{
fprintf(stderr, "Error exchanging keys: `%s'.\n", ssh_get_error(con.session));
return -1;
}
if (debug)
printf("Successful key exchange.\n");
con.banner = ssh_get_clientbanner(con.session);
if (debug && con.banner != NULL)
printf("Client banner '%s'.\n", con.banner);
/* only announce password authentication */
ssh_set_auth_methods(con.session, SSH_AUTH_METHOD_PASSWORD);
/* Wait for a message, which should be an authentication attempt. Send the default
* reply if it isn't. Log the attempt and quit. */
for (i = 0; i < MAX_AUTH_TRIES; )
{
if ((con.message = ssh_message_get(con.session)) == NULL)
{
if (debug)
printf("Client %s disconnected (%s).\n", client_ip, ssh_get_error(con.session));
break;
}
msg = ssh_message_type(con.message);
if (debug && msg != 0x1 && msg != 0x4)
printf("received message type (0x%02x).\n", ssh_message_type(con.message));
auth_msg=ssh_message_subtype(con.message);
switch (auth_msg)
{
case SSH_AUTH_METHOD_NONE:
if (debug)
printf("Received request %s\n", VAL(SSH_AUTH_METHOD_NONE));
/* send not supported */
break;
case SSH_AUTH_METHOD_PUBLICKEY:
if (debug)
printf("Received request %s\n", VAL(SSH_AUTH_METHOD_PUBLICKEY));
/* send not supported */
break;
case SSH_AUTH_METHOD_HOSTBASED:
if (debug)
printf("Received request %s\n", VAL(SSH_AUTH_METHOD_HOSTBASED));
/* send not supported */
break;
case SSH_AUTH_METHOD_GSSAPI_MIC:
if (debug)
printf("Received request %s\n", VAL(SSH_AUTH_METHOD_GSSAPI_MIC));
/* send not supported */
break;
case SSH_AUTH_METHOD_INTERACTIVE:
if (debug)
printf("Received request %s\n", VAL(SSH_AUTH_METHOD_INTERACTIVE));
/* can we use this for user/pwd/token? */
break;
case SSH_AUTH_METHOD_PASSWORD:
if (debug)
printf("Received request %s\n", VAL(SSH_AUTH_METHOD_PASSWORD));
/* Log the authentication request */
log_attempt(&con);
break;
case 0xffffffffu:
if (msg == 0x4)
break;
if (debug)
printf("Received request %0x04x %s\n", msg, "0xffffffffu");
/* don't know why maybe a set of all values enabled? */
break;
default:
if (debug)
printf("Received unknown request 0x%04x\n", auth_msg);
/* unknown */
break;
}
/* increase number of auth attempts */
i++;
/* Send the default message regardless of the request type. */
ssh_message_reply_default(con.message);
ssh_message_free(con.message);
}
if (i == MAX_AUTH_TRIES)
{
/* send a maxtries reply */
if (debug)
printf("Max tries (%d) reached.\n", i);
/* send the reply */
}
if (debug)
printf("Exiting auth subprocess.\n");
return 0;
}