From 4ae341b1e3cf47fb76a75d802008a205b47a09e6 Mon Sep 17 00:00:00 2001 From: manojampalam Date: Wed, 13 Apr 2016 21:48:30 -0700 Subject: [PATCH] 4-13 C2 --- contrib/win32/win32compat/ssh-agent/agent.c | 9 ++-- .../win32/win32compat/ssh-agent/connection.c | 51 ++++++------------- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/contrib/win32/win32compat/ssh-agent/agent.c b/contrib/win32/win32compat/ssh-agent/agent.c index f91349f..7db0a1a 100644 --- a/contrib/win32/win32compat/ssh-agent/agent.c +++ b/contrib/win32/win32compat/ssh-agent/agent.c @@ -56,7 +56,7 @@ void agent_sm_process_action_queue() { break; } else if (action_queue & ACTION_LISTEN) { - HANDLE h, temp; + HANDLE h; long prev_queue; struct agent_connection* con = (struct agent_connection*)malloc(sizeof(struct agent_connection)); @@ -64,8 +64,8 @@ void agent_sm_process_action_queue() { h = CreateNamedPipe( AGENT_PIPE_ID, // pipe name PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, // read/write access - PIPE_TYPE_MESSAGE | // message type pipe - PIPE_READMODE_MESSAGE | // message-read mode + PIPE_TYPE_BYTE | // message type pipe + PIPE_READMODE_BYTE | // message-read mode PIPE_WAIT, // blocking mode PIPE_UNLIMITED_INSTANCES, // max. instances BUFSIZE, // output buffer size @@ -78,7 +78,8 @@ void agent_sm_process_action_queue() { con->next = list; list = con; prev_queue = InterlockedAnd(&action_queue, ~ACTION_LISTEN); - temp = CreateIoCompletionPort(h, ioc_port, (ULONG_PTR)con, 0); + CreateIoCompletionPort(h, ioc_port, (ULONG_PTR)con, 0); + ConnectNamedPipe(h, &con->ol); if (prev_queue == ACTION_LISTEN) break; } diff --git a/contrib/win32/win32compat/ssh-agent/connection.c b/contrib/win32/win32compat/ssh-agent/connection.c index a84e5bd..59abf66 100644 --- a/contrib/win32/win32compat/ssh-agent/connection.c +++ b/contrib/win32/win32compat/ssh-agent/connection.c @@ -42,21 +42,17 @@ void agent_connection_on_io(struct agent_connection* con, DWORD bytes, OVERLAPPE if (con->state == DONE) DebugBreak(); - while (1) { - switch (con->state) { + //while (1) + { + switch (con->state) { + case LISTENING: + agent_listen(); case WRITING: /* Writing is done, read next request */ - case LISTENING: con->state = READING_HEADER; - if (con->state == LISTENING) - agent_listen(); ZeroMemory(&con->request, sizeof(con->request)); - if (ReadFile(con->connection, con->request.buf, - HEADER_SIZE, NULL, &con->ol)) { - bytes = HEADER_SIZE; - continue; - } - if (GetLastError() != ERROR_IO_PENDING) { + if (!ReadFile(con->connection, con->request.buf, + HEADER_SIZE, NULL, &con->ol) && (GetLastError() != ERROR_IO_PENDING)) { con->state = DONE; agent_cleanup_connection(con); return; @@ -66,25 +62,18 @@ void agent_connection_on_io(struct agent_connection* con, DWORD bytes, OVERLAPPE con->request.read += bytes; if (con->request.read == HEADER_SIZE) { con->request.size = *((DWORD*)con->request.buf); + con->request.read = 0; con->state = READING; - if (ReadFile(con->connection, con->request.buf, - con->request.size, NULL, &con->ol)) { - bytes = con->request.size; - continue; - } - if (GetLastError() != ERROR_IO_PENDING) { + if (!ReadFile(con->connection, con->request.buf, + con->request.size, NULL, &con->ol)&&(GetLastError() != ERROR_IO_PENDING)) { con->state = DONE; agent_cleanup_connection(con); return; } } else { - if (ReadFile(con->connection, con->request.buf + con->request.read, - HEADER_SIZE - con->request.read, NULL, &con->ol)) { - bytes = HEADER_SIZE - con->request.read; - continue; - } - if (GetLastError() != ERROR_IO_PENDING) { + if (!ReadFile(con->connection, con->request.buf + con->request.read, + HEADER_SIZE - con->request.read, NULL, &con->ol)&& (GetLastError() != ERROR_IO_PENDING)) { con->state = DONE; agent_cleanup_connection(con); return; @@ -96,24 +85,16 @@ void agent_connection_on_io(struct agent_connection* con, DWORD bytes, OVERLAPPE if (con->request.read == con->request.size) { /* process request and get response */ con->state = WRITING; - if (WriteFile(con->connection, con->request.buf, - con->request.size, NULL, &con->ol)) { - bytes = con->request.size; - continue; - } - if (GetLastError() != ERROR_IO_PENDING) { + if (!WriteFile(con->connection, con->request.buf, + con->request.size, NULL, &con->ol)&& (GetLastError() != ERROR_IO_PENDING) ){ con->state = DONE; agent_cleanup_connection(con); return; } } else { - if (ReadFile(con->connection, con->request.buf + con->request.read, - con->request.size - con->request.read, NULL, &con->ol)) { - bytes = con->request.size - con->request.read; - continue; - } - if (GetLastError() != ERROR_IO_PENDING) { + if (!ReadFile(con->connection, con->request.buf + con->request.read, + con->request.size - con->request.read, NULL, &con->ol)&& (GetLastError() != ERROR_IO_PENDING)) { con->state = DONE; agent_cleanup_connection(con); return;