Skip to content

Commit b6aca59

Browse files
committed
5-10 C2
1 parent 351f141 commit b6aca59

File tree

3 files changed

+80
-90
lines changed

3 files changed

+80
-90
lines changed

auth2-pubkey.c

Lines changed: 64 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -207,93 +207,74 @@ userauth_pubkey(Authctxt *authctxt)
207207
* On pure win32 try to logon using lsa first.
208208
*/
209209

210-
#ifdef WIN32_FIXME
210+
#ifdef WIN32_FIXME
211+
{
212+
#define SSH_AGENT_ROOT "SOFTWARE\\SSH\\Agent"
213+
HKEY agent_root = 0;
214+
DWORD agent_pid = 0, tmp_size = 4, pipe_server_pid = 0xff;
215+
int sock = -1, r;
216+
u_char *blob = NULL;
217+
size_t blen = 0;
218+
DWORD token = 0;
219+
HANDLE h = INVALID_HANDLE_VALUE;
220+
struct sshbuf *msg = NULL;
221+
222+
while (1) {
223+
RegOpenKeyEx(HKEY_LOCAL_MACHINE, SSH_AGENT_ROOT, 0, KEY_QUERY_VALUE, &agent_root);
224+
if (agent_root)
225+
RegQueryValueEx(agent_root, "ProcessId", 0, NULL, &agent_pid, &tmp_size);
226+
227+
228+
h = CreateFile(
229+
"\\\\.\\pipe\\ssh-authagent", // pipe name
230+
GENERIC_READ | // read and write access
231+
GENERIC_WRITE,
232+
0, // no sharing
233+
NULL, // default security attributes
234+
OPEN_EXISTING, // opens existing pipe
235+
FILE_FLAG_OVERLAPPED, // attributes
236+
NULL); // no template file
237+
if (h == INVALID_HANDLE_VALUE)
238+
break;
211239

212-
authctxt -> methoddata = NULL;
213-
214-
/*
215-
* Retrieve name of current login user (i.e. sshd process owner).
216-
*/
217-
218-
GetUserName(currentUser, &currentUserSize);
240+
if (!GetNamedPipeServerProcessId(h, &pipe_server_pid) || (agent_pid != pipe_server_pid))
241+
break;
219242

220-
/*
221-
* Try to get token from lsa, but only if targetUser != currentUser.
222-
* Owerthise we already have targetUser's token in current thread, so
223-
* we only need key verify from original OpenSSH code.
224-
*/
243+
sock = w32_allocate_fd_for_handle(h, FALSE);
244+
msg = sshbuf_new();
245+
if (!msg)
246+
break;
247+
if ((r = sshbuf_put_cstring(msg, "keyauthenticate")) != 0 ||
248+
(r = sshkey_to_blob(key, &blob, &blen)) != 0 ||
249+
(r = sshbuf_put_string(msg, blob, blen)) != 0 ||
250+
(r = sshbuf_put_cstring(msg, authctxt->pw->pw_name)) != 0 ||
251+
(r = sshbuf_put_string(msg, sig, slen)) != 0 ||
252+
(r = sshbuf_put_string(msg, buffer_ptr(&b), buffer_len(&b))) != 0 ||
253+
(r = ssh_request_reply(sock, msg, msg)) != 0 ||
254+
(r = sshbuf_get_u32(msg, &token)) != 0 )
255+
break;
256+
257+
break;
258+
259+
}
260+
if (agent_root)
261+
RegCloseKey(agent_root);
262+
if (blob)
263+
free(blob);
264+
if (sock != -1)
265+
close(sock);
266+
if (msg)
267+
sshbuf_free(msg);
268+
269+
if (token) {
270+
authenticated = 1;
271+
authctxt->methoddata = token;
272+
}
273+
274+
}
225275

226-
targetIsCurrent = (strcmp(currentUser, authctxt -> user) == 0);
227-
228-
if (targetIsCurrent)
229-
{
230-
doOpenSSHVerify = 1;
231-
}
232-
else
233-
{
234-
loginStat = LsaLogon(&authctxt->methoddata, HomeDirLsaW,
235-
authctxt -> user, pkblob, blen, sig, slen,
236-
buffer_ptr(&b), buffer_len(&b), datafellows);
237-
238-
/*
239-
* If lsa logon process success.
240-
*/
241-
242-
if (loginStat == 0)
243-
{
244-
/*
245-
* And user authorized OK.
246-
*/
247-
248-
if (authctxt->methoddata)
249-
{
250-
doOpenSSHVerify = 0;
251-
252-
/*
253-
* This is part of openssh authorization needed for parsing
254-
* 'options' block in key.
255-
*/
256-
257-
authctxt -> pw -> pw_dir = GetHomeDir(authctxt -> user);
258-
259-
if (PRIVSEP(user_key_allowed(authctxt -> pw, key, 1))) // PRAGMA:TODO
260-
{
261-
authenticated = 1;
262-
}
263-
else
264-
{
265-
authenticated = 0;
266-
}
267-
268-
buffer_free(&b);
269-
270-
free(sig);
271-
}
272-
}
273-
}
274-
275-
if (doOpenSSHVerify)
276-
{
277-
/*
278-
* If lsa fails, test for correct signature using openssh code.
279-
*/
280-
281-
authctxt -> pw -> pw_dir = GetHomeDir(authctxt -> user);
282-
283-
if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0)) //PRAGMA:TODO
284-
&&
285-
PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b))) == 1)
286-
287-
{
288-
authenticated = 1;
289-
}
290-
}
291-
292-
/*
293-
* Original code.
294-
*/
295276

296-
#else /* #ifdef WIN32_FIXME */
277+
#else /* #ifdef WIN32_FIXME */
297278

298279
if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
299280
PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),

authfd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ ssh_get_authentication_socket(int *fdp)
154154
}
155155

156156
/* Communicate with agent: send request and read reply */
157-
static int
157+
int
158158
ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply)
159159
{
160160
int r;

contrib/win32/win32compat/ssh-agent/authagent-request.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <ntstatus.h>
3737
#include "agent.h"
3838
#include "agent-request.h"
39+
#include "key.h"
3940

4041
static void
4142
InitLsaString(LSA_STRING *lsa_string, const char *str)
@@ -65,8 +66,8 @@ generate_user_token(wchar_t* user) {
6566
DWORD cbProfile;
6667

6768
InitLsaString(&logon_process_name, "ssh-agent");
68-
InitLsaString(&auth_package_name, MICROSOFT_KERBEROS_NAME_A);
69-
//InitLsaString(&auth_package_name, "Negotiate");
69+
//InitLsaString(&auth_package_name, MICROSOFT_KERBEROS_NAME_A);
70+
InitLsaString(&auth_package_name, "Negotiate");
7071
InitLsaString(&originName, "sshd");
7172
if (ret = LsaRegisterLogonProcess(&logon_process_name, &lsa_handle, &mode) != STATUS_SUCCESS)
7273
goto done;
@@ -127,12 +128,13 @@ generate_user_token(wchar_t* user) {
127128

128129
int process_authagent_request(struct sshbuf* request, struct sshbuf* response, struct agent_connection* con) {
129130
int r = 0;
130-
char* opn, key_blob, user, sig, blob;
131+
char *opn, *key_blob, *user, *sig, *blob;
131132
size_t opn_len, key_blob_len, user_len, sig_len, blob_len;
132133
struct sshkey *key = NULL;
133-
HANDLE token = NULL, dup_token = NULL;
134+
HANDLE token = NULL, dup_token = NULL, client_proc = NULL;
134135
wchar_t wuser[MAX_USER_NAME_LEN];
135136
PWSTR wuser_home = NULL;
137+
ULONG client_pid;
136138

137139
user = NULL;
138140
if ((r = sshbuf_get_string_direct(request, &opn, &opn_len)) != 0 ||
@@ -148,12 +150,17 @@ int process_authagent_request(struct sshbuf* request, struct sshbuf* response, s
148150
goto done;
149151
}
150152

151-
if (0 == MultiByteToWideChar(CP_UTF8, 0, user, user_len + 1, wuser, MAX_USER_NAME_LEN) {
153+
if (0 == MultiByteToWideChar(CP_UTF8, 0, user, user_len + 1, wuser, MAX_USER_NAME_LEN)) {
152154
r = GetLastError();
153155
goto done;
154156
}
155157

156-
if ((token = generate_user_token(wuser)) == 0) {
158+
if (key_verify(key, sig, sig_len, blob, blob_len) != 1 ||
159+
(token = generate_user_token(wuser)) == 0 ||
160+
(FALSE == GetNamedPipeClientProcessId(con->connection, &client_pid)) ||
161+
( (client_proc = OpenProcess(PROCESS_DUP_HANDLE, FALSE, client_pid)) == NULL) ||
162+
(FALSE == DuplicateHandle(GetCurrentProcess(), token, client_proc, &dup_token, TOKEN_QUERY | TOKEN_IMPERSONATE, FALSE, DUPLICATE_SAME_ACCESS)) ||
163+
(sshbuf_put_u32(response, dup_token) != 0) ) {
157164
r = EINVAL;
158165
goto done;
159166
}
@@ -167,5 +174,7 @@ int process_authagent_request(struct sshbuf* request, struct sshbuf* response, s
167174
CloseHandle(token);
168175
if (wuser_home)
169176
CoTaskMemFree(wuser_home);
177+
if (client_proc)
178+
CloseHandle(client_proc);
170179
return r;
171180
}

0 commit comments

Comments
 (0)