9 #include <sys/socket.h>
14 #include <sys/types.h>
16 #include <arpa/inet.h>
17 #include <netinet/in.h>
19 #define MAX(a,b) ((a)>(b)?(a):(b))
21 #define NTLMSSP_NEGOTIATE_UNICODE 0x00000001
22 #define NTLMSSP_NEGOTIATE_OEM 0x00000002
23 #define NTLMSSP_REQUEST_TARGET 0x00000004
24 #define NTLMSSP_NEGOTIATE_SIGN 0x00000010 /* Message integrity */
25 #define NTLMSSP_NEGOTIATE_SEAL 0x00000020 /* Message confidentiality */
26 #define NTLMSSP_NEGOTIATE_DATAGRAM_STYLE 0x00000040
27 #define NTLMSSP_NEGOTIATE_LM_KEY 0x00000080
28 #define NTLMSSP_NEGOTIATE_NETWARE 0x00000100
29 #define NTLMSSP_NEGOTIATE_NTLM 0x00000200
30 #define NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED 0x00001000
31 #define NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED 0x00002000
32 #define NTLMSSP_NEGOTIATE_THIS_IS_LOCAL_CALL 0x00004000
33 #define NTLMSSP_NEGOTIATE_ALWAYS_SIGN 0x00008000
34 #define NTLMSSP_NEGOTIATE_128 0x20000000 /* 128-bit encryption */
35 #define NTLMSSP_NEGOTIATE_KEY_EXCH 0x40000000
36 #define NTLMSSP_NEGOTIATE_NTLM2 0x00080000
38 void dump_data(int level, const char *buf1,int len)
40 #define DEBUGADD(lvl, x) printf x
41 #define MIN(a,b) ((a)<(b)?(a):(b))
42 void print_asc(int level, const uint8_t *buf,int len) {
45 DEBUGADD(level,("%c", isprint(buf[i])?buf[i]:'.'));
47 const uint8_t *buf = (const uint8_t *)buf1;
52 DEBUGADD(level,("[%03X] ",i));
54 DEBUGADD(level,("%02X ",(int)buf[i]));
56 if (i%8 == 0) DEBUGADD(level,(" "));
58 print_asc(level,&buf[i-16],8); DEBUGADD(level,(" "));
59 print_asc(level,&buf[i-8],8); DEBUGADD(level,("\n"));
60 if (i<len) DEBUGADD(level,("[%03X] ",i));
66 DEBUGADD(level,(" "));
67 if (n>8) DEBUGADD(level,(" "));
68 while (n--) DEBUGADD(level,(" "));
70 print_asc(level,&buf[i-(i%16)],n); DEBUGADD(level,( " " ));
72 if (n>0) print_asc(level,&buf[i-n],n);
73 DEBUGADD(level,("\n"));
77 static void replace_str(char *buf, int n)
80 printf("Packet %d\n", count++);
83 unsigned *x = (buf+0x10);
84 printf("Changing 0x%x\n", *x);
89 /* open a socket to a tcp remote host with the specified port */
90 static int open_socket_out(const char *host, int port)
92 struct sockaddr_in sock_out;
97 res = socket(PF_INET, SOCK_STREAM, 0);
102 if (inet_pton(AF_INET, host, &addr) > 0) {
103 memcpy(&sock_out.sin_addr, &addr, sizeof(addr));
105 hp = gethostbyname(host);
107 fprintf(stderr,"unknown host %s\n", host);
110 memcpy(&sock_out.sin_addr, hp->h_addr, hp->h_length);
113 sock_out.sin_port = htons(port);
114 sock_out.sin_family = PF_INET;
116 if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out)) != 0) {
118 fprintf(stderr,"failed to connect to %s (%s)\n",
119 host, strerror(errno));
128 open a socket of the specified type, port and address for incoming data
130 int open_socket_in(int port)
132 struct sockaddr_in sock;
136 memset(&sock,0,sizeof(sock));
138 #ifdef HAVE_SOCK_SIN_LEN
139 sock.sin_len = sizeof(sock);
141 sock.sin_port = htons(port);
142 sock.sin_family = AF_INET;
144 res = socket(AF_INET, SOCK_STREAM, 0);
146 fprintf(stderr, "socket failed\n"); return -1;
150 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
152 if (bind(res, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
159 /* write to a file descriptor, making sure we get all the data out or
161 static void write_all(int fd, unsigned char *s, size_t n)
174 static void main_loop(int sock1, int sock2)
176 unsigned char buf[1024];
180 char *fname1, *fname2;
181 asprintf(&fname1, "sockspy-in.%d", i);
182 asprintf(&fname2, "sockspy-out.%d", i);
183 log1 = open(fname1, O_WRONLY|O_CREAT|O_EXCL, 0644);
184 log2 = open(fname2, O_WRONLY|O_CREAT|O_EXCL, 0644);
188 } while (i<1000 && (log1 == -1 || log2 == -1));
190 if (log1 == -1 || log2 == -1) {
191 fprintf(stderr,"Failed to open log files\n");
203 ret = select(MAX(sock1, sock2)+1, &fds, NULL, NULL, NULL);
204 if (ret == -1 && errno == EINTR) continue;
207 if (FD_ISSET(sock1, &fds)) {
208 int n = read(sock1, buf, sizeof(buf));
213 write_all(sock2, buf, n);
214 write_all(log1, buf, n);
217 if (FD_ISSET(sock2, &fds)) {
218 int n = read(sock2, buf, sizeof(buf));
223 write_all(sock1, buf, n);
224 write_all(log2, buf, n);
229 static char *get_socket_addr(int fd)
232 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
233 socklen_t length = sizeof(sa);
234 static char addr_buf[200];
236 strcpy(addr_buf,"0.0.0.0");
242 if (getsockname(fd, &sa, &length) < 0) {
243 printf("getpeername failed. Error was %s\n", strerror(errno) );
247 strcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
252 int main(int argc, char *argv[])
254 int listen_port, dest_port;
259 struct sockaddr addr;
260 int in_addrlen = sizeof(addr);
263 printf("Usage: sockspy <inport> <host> <port>\n");
267 listen_port = atoi(argv[1]);
269 dest_port = atoi(argv[3]);
271 listen_fd = open_socket_in(listen_port);
273 if (listen_fd == -1) {
274 fprintf(stderr,"listen on port %d failed - %s\n",
275 listen_port, strerror(errno));
279 if (listen(listen_fd, 5) == -1) {
280 fprintf(stderr,"listen failed\n");
284 sock_in = accept(listen_fd,&addr,&in_addrlen);
287 fprintf(stderr,"accept on port %d failed - %s\n",
288 listen_port, strerror(errno));
292 printf("Connection from %s\n", get_socket_addr(sock_in));
296 sock_out = open_socket_out(host, dest_port);
297 if (sock_out == -1) {
301 main_loop(sock_in, sock_out);