1 From 52adbb34c32d3e2e1bcdb941e20a6f81138b8248 Mon Sep 17 00:00:00 2001
2 From: Matt Johnston <matt@ucc.asn.au>
3 Date: Thu, 23 Aug 2018 23:43:12 +0800
4 Subject: [PATCH 2/2] Wait to fail invalid usernames
8 svr-auth.c | 19 +++++--------------
9 svr-authpam.c | 26 ++++++++++++++++++++++----
10 svr-authpasswd.c | 27 ++++++++++++++-------------
11 svr-authpubkey.c | 11 ++++++++++-
12 5 files changed, 54 insertions(+), 35 deletions(-)
16 @@ -37,9 +37,9 @@ void recv_msg_userauth_request(void);
17 void send_msg_userauth_failure(int partial, int incrfail);
18 void send_msg_userauth_success(void);
19 void send_msg_userauth_banner(buffer *msg);
20 -void svr_auth_password(void);
21 -void svr_auth_pubkey(void);
22 -void svr_auth_pam(void);
23 +void svr_auth_password(int valid_user);
24 +void svr_auth_pubkey(int valid_user);
25 +void svr_auth_pam(int valid_user);
27 #ifdef ENABLE_SVR_PUBKEY_OPTIONS
28 int svr_pubkey_allows_agentfwd(void);
31 @@ -176,10 +176,8 @@ void recv_msg_userauth_request() {
32 if (methodlen == AUTH_METHOD_PASSWORD_LEN &&
33 strncmp(methodname, AUTH_METHOD_PASSWORD,
34 AUTH_METHOD_PASSWORD_LEN) == 0) {
36 - svr_auth_password();
39 + svr_auth_password(valid_user);
44 @@ -191,10 +189,8 @@ void recv_msg_userauth_request() {
45 if (methodlen == AUTH_METHOD_PASSWORD_LEN &&
46 strncmp(methodname, AUTH_METHOD_PASSWORD,
47 AUTH_METHOD_PASSWORD_LEN) == 0) {
52 + svr_auth_pam(valid_user);
57 @@ -204,12 +200,7 @@ void recv_msg_userauth_request() {
58 if (methodlen == AUTH_METHOD_PUBKEY_LEN &&
59 strncmp(methodname, AUTH_METHOD_PUBKEY,
60 AUTH_METHOD_PUBKEY_LEN) == 0) {
64 - /* pubkey has no failure delay */
65 - send_msg_userauth_failure(0, 0);
67 + svr_auth_pubkey(valid_user);
73 @@ -178,13 +178,14 @@ pamConvFunc(int num_msg,
74 * Keyboard interactive would be a lot nicer, but since PAM is synchronous, it
75 * gets very messy trying to send the interactive challenges, and read the
76 * interactive responses, over the network. */
77 -void svr_auth_pam() {
78 +void svr_auth_pam(int valid_user) {
80 struct UserDataS userData = {NULL, NULL};
81 struct pam_conv pamConv = {
83 &userData /* submitted to pamvConvFunc as appdata_ptr */
85 + const char* printable_user = NULL;
87 pam_handle_t* pamHandlep = NULL;
89 @@ -204,12 +205,23 @@ void svr_auth_pam() {
91 password = buf_getstring(ses.payload, &passwordlen);
93 + /* We run the PAM conversation regardless of whether the username is valid
94 + in case the conversation function has an inherent delay.
95 + Use ses.authstate.username rather than ses.authstate.pw_name.
96 + After PAM succeeds we then check the valid_user flag too */
98 /* used to pass data to the PAM conversation function - don't bother with
99 * strdup() etc since these are touched only by our own conversation
100 * function (above) which takes care of it */
101 - userData.user = ses.authstate.pw_name;
102 + userData.user = ses.authstate.username;
103 userData.passwd = password;
105 + if (ses.authstate.pw_name) {
106 + printable_user = ses.authstate.pw_name;
108 + printable_user = "<invalid username>";
112 if ((rc = pam_start("sshd", NULL, &pamConv, &pamHandlep)) != PAM_SUCCESS) {
113 dropbear_log(LOG_WARNING, "pam_start() failed, rc=%d, %s",
114 @@ -236,7 +248,7 @@ void svr_auth_pam() {
115 rc, pam_strerror(pamHandlep, rc));
116 dropbear_log(LOG_WARNING,
117 "Bad PAM password attempt for '%s' from %s",
118 - ses.authstate.pw_name,
121 send_msg_userauth_failure(0, 1);
123 @@ -247,12 +259,18 @@ void svr_auth_pam() {
124 rc, pam_strerror(pamHandlep, rc));
125 dropbear_log(LOG_WARNING,
126 "Bad PAM password attempt for '%s' from %s",
127 - ses.authstate.pw_name,
130 send_msg_userauth_failure(0, 1);
135 + /* PAM auth succeeded but the username isn't allowed in for another reason
136 + (checkusername() failed) */
137 + send_msg_userauth_failure(0, 1);
140 /* successful authentication */
141 dropbear_log(LOG_NOTICE, "PAM password auth succeeded for '%s' from %s",
142 ses.authstate.pw_name,
143 --- a/svr-authpasswd.c
144 +++ b/svr-authpasswd.c
145 @@ -48,22 +48,14 @@ static int constant_time_strcmp(const ch
147 /* Process a password auth request, sending success or failure messages as
149 -void svr_auth_password() {
150 +void svr_auth_password(int valid_user) {
152 char * passwdcrypt = NULL; /* the crypt from /etc/passwd or /etc/shadow */
153 char * testcrypt = NULL; /* crypt generated from the user's password sent */
155 + char * password = NULL;
156 unsigned int passwordlen;
158 unsigned int changepw;
160 - passwdcrypt = ses.authstate.pw_passwd;
162 -#ifdef DEBUG_HACKCRYPT
163 - /* debugging crypt for non-root testing with shadows */
164 - passwdcrypt = DEBUG_HACKCRYPT;
167 /* check if client wants to change password */
168 changepw = buf_getbool(ses.payload);
170 @@ -73,12 +65,21 @@ void svr_auth_password() {
173 password = buf_getstring(ses.payload, &passwordlen);
175 - /* the first bytes of passwdcrypt are the salt */
176 - testcrypt = crypt(password, passwdcrypt);
178 + /* the first bytes of passwdcrypt are the salt */
179 + passwdcrypt = ses.authstate.pw_passwd;
180 + testcrypt = crypt(password, passwdcrypt);
182 m_burn(password, passwordlen);
185 + /* After we have got the payload contents we can exit if the username
186 + is invalid. Invalid users have already been logged. */
188 + send_msg_userauth_failure(0, 1);
192 if (testcrypt == NULL) {
193 /* crypt() with an invalid salt like "!!" */
194 dropbear_log(LOG_WARNING, "User account '%s' is locked",
195 --- a/svr-authpubkey.c
196 +++ b/svr-authpubkey.c
197 @@ -79,7 +79,7 @@ static int checkfileperm(char * filename
199 /* process a pubkey auth request, sending success or failure message as
201 -void svr_auth_pubkey() {
202 +void svr_auth_pubkey(int valid_user) {
204 unsigned char testkey; /* whether we're just checking if a key is usable */
205 char* algo = NULL; /* pubkey algo */
206 @@ -102,6 +102,15 @@ void svr_auth_pubkey() {
207 keybloblen = buf_getint(ses.payload);
208 keyblob = buf_getptr(ses.payload, keybloblen);
211 + /* Return failure once we have read the contents of the packet
212 + required to validate a public key.
213 + Avoids blind user enumeration though it isn't possible to prevent
214 + testing for user existence if the public key is known */
215 + send_msg_userauth_failure(0, 0);
219 /* check if the key is valid */
220 if (checkpubkey(algo, algolen, keyblob, keybloblen) == DROPBEAR_FAILURE) {
221 send_msg_userauth_failure(0, 0);