1 #pragma once 2 3 #define _CRT_SECURE_NO_WARNINGS 4 #define WIN32_NO_STATUS 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <windef.h> 8 #include <winbase.h> 9 #include <wincon.h> 10 #define _INC_WINDOWS 11 #include <winsock2.h> 12 #include <strsafe.h> 13 14 /* 15 ** macro definitions 16 */ 17 #define TELNET_PORT (23) 18 19 #define BUFSIZE (4096) 20 #define USERID_SIZE (64) 21 #define CTRLC (3) 22 #define BS (8) 23 #define CR (13) 24 #define LF (10) 25 #define DEL (127) 26 27 #define IAC "\xff" 28 #define DONT "\xfe" 29 #define WONT "\xfc" 30 #define WILL "\xfb" 31 #define DO "\xfd" 32 #define SB "\xfa" 33 #define SE "\xf0" 34 #define ECHO "\x01" 35 #define SUPPRESS_GO_AHEAD "\x03" 36 #define TERMINAL_TYPE "\x18" 37 #define NAWS "\x1f" 38 #define LINEMODE "\x22" 39 #define NEWENVIRON "\x27" 40 #define MODE "\x01" 41 42 #define HANDSHAKE_TIMEOUT (3) 43 44 /* 45 ** types 46 */ 47 48 typedef struct client_s 49 { 50 char userID[USERID_SIZE]; 51 int socket; 52 BOOLEAN bTerminate; 53 BOOLEAN bReadFromPipe; 54 BOOLEAN bWriteToPipe; 55 HANDLE hProcess; 56 DWORD dwProcessId; 57 HANDLE hChildStdinWr; 58 HANDLE hChildStdoutRd; 59 } client_t; 60 61 typedef enum 62 { 63 NoEcho = 0, 64 Echo = 1, 65 Password = 2 66 } EchoMode; 67 68 /* 69 ** Forward function declarations 70 */ 71 static BOOL WINAPI Cleanup(DWORD dwControlType); 72 static void WaitForConnect(void); 73 static BOOLEAN StartSocketInterface(void); 74 static void CreateSocket(void); 75 static void UserLogin(int client_socket); 76 static DWORD WINAPI UserLoginThread(LPVOID); 77 static int DoTelnetHandshake(int sock); 78 static int ReceiveLine(int sock, char *buffer, int len, EchoMode echo); 79 static void RunShell(client_t *client); 80 //static BOOL CreateChildProcess(const char *); 81 static DWORD WINAPI MonitorChildThread(LPVOID); 82 static DWORD WINAPI WriteToPipeThread(LPVOID); 83 static DWORD WINAPI ReadFromPipeThread(LPVOID); 84 static void TerminateShell(client_t *client); 85 static VOID ErrorExit(LPTSTR); 86 int kickoff_telnetd(void); 87