File: halfscan-mod.c

package info (click to toggle)
cheops 0.61-15.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,272 kB
  • ctags: 1,312
  • sloc: ansic: 12,731; sh: 155; makefile: 151
file content (268 lines) | stat: -rw-r--r-- 6,264 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**********************************************************************
** This is a real simple half opened connection scanner I
** put together. It scans ports 0 to 1024 for listening
** processes, and writes to stdout the ports that are 
** listening.
**
** halflife@saturn.net
**
** Modified for Cheops by Mark Spencer
**
** NOTE: You have to define MY_IP as your ip. If you have
** a dynamic ip, this is gonna bite :-).
*********************************************************************/

/* define the following as your ip address */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

#include <gtk/gtk.h>
#include "cheops.h"
#include "tcpip-mod.h"

static int tcp_socket = -1;


int init_halfscan()
{
   tcp_socket = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
   if((tcp_socket < 0))
   {
      fprintf(stderr, "couldnt open raw socket\n");
      exit(1);
   }
   return 0;
}

int scan_ports(unsigned int src_addr, unsigned int dst_addr, struct port *list, int timeout)
{
   struct recv_tcp
   {
      struct iphdr ip;
      struct tcphdr tcp;
      unsigned char blah[65535];
   }recv_tcp;

   int blah = 0;
   int total=0;
   int cnt=0;
   struct port *p;
   struct timeval tv;
   fd_set fds;
   int res=1;
   int res2;
   spoofrec s;
   
   /* Don't bother on an empty list */
   if (!list)
   	return 0;
   
   /* Quickly, flush anything that is in the queue already. */
   while(res > 0)
   {
      /* Count on Linux's uniqiue in-place timeval modification to make this work... */
      FD_ZERO(&fds);
      FD_SET(tcp_socket, &fds);
      tv.tv_sec = 0;
      tv.tv_usec = 0;
      res = select(tcp_socket + 1, &fds, NULL, NULL, &tv);
      if (res > 0) 
      	res2=recv(tcp_socket, (struct recv_tcp *)&recv_tcp, 65535, 0);
   }
   res=1;
   
   p = list;
   while(p) {
   	/* Send out the requests */
   	blah++;
	total++;

   	s.sport =  ((getpid() + blah) & 0xFFF) + 3000;
	s.seq = rand() & 0xffff;
	s.ack = 0;
	s.retries=0;
	s.next=NULL;
	s.from.sin_addr.s_addr = src_addr;
	s.dest.sin_addr.s_addr = dst_addr;
	s.dport = p->dport;
	s.dest.sin_port = htons(s.dport);
	s.dest.sin_family = AF_INET;
	s.data=NULL;
	sendtcp(&s, SYN, 1);
	p=p->next;
   }
   tv.tv_sec = timeout;
   tv.tv_usec = 0;
   while(res > 0)
   {
      /* Count on Linux's uniqiue in-place timeval modification to make this work... */
      FD_ZERO(&fds);
      FD_SET(tcp_socket, &fds);
      res = select(tcp_socket + 1, &fds, NULL, NULL, &tv);
      if (res > 0) {
      	res2=recv(tcp_socket, (struct recv_tcp *)&recv_tcp, 65535, 0);
      	p = list;
		if (recv_tcp.ip.saddr == dst_addr) {
			while(p) {
				/* Check for duplicates */
				if (p->avail > 0) {
					p=p->next;
					continue;
				}
#if (__GLIBC__ < 2)
      	  			if(ntohs(recv_tcp.tcp.th_sport) == p->dport) {
	  				cnt++;
          				if(recv_tcp.tcp.th_flags &  TH_RST) 
#else
      	  			if(ntohs(recv_tcp.tcp.source) == p->dport) {
	  				cnt++;
          				if(recv_tcp.tcp.rst == 1) 
#endif
						p->avail = 0;
         	 			else 
						p->avail=1;
					
	  			}
	  			p=p->next;
        		}
		}
		if (cnt >= total) break;
      }
   }
   return cnt;
}


#if (__GLIBC__ < 2)
#define CHECK_SELECT { \
	int port;\
      	res2=recv(tcp_socket, (struct recv_tcp *)&recv_tcp, 65535, 0);\
	if (recv_tcp.ip.saddr == dst_addr) {\
		port = ntohs(recv_tcp.tcp.th_sport);\
        	found = ((recv_tcp.tcp.th_flags &  TH_RST));\
		if ((port <= MAX_TCP) && !seen[port]) {\
			if (!found) { \
				callback(data, port);\
			} \
			cnt++;\
			seen[port]++;\
		}\
	}\
}
#else
#define CHECK_SELECT { \
	int port; \
      	res2=recv(tcp_socket, (struct recv_tcp *)&recv_tcp, 65535, 0);\
	if (recv_tcp.ip.saddr == dst_addr) {\
		port = ntohs(recv_tcp.tcp.source);\
          	found = ((recv_tcp.tcp.rst == 1)); \
		if ((port <= MAX_TCP) && !seen[port]) {\
			if (!found && ((recv_tcp.ip.saddr != recv_tcp.ip.daddr) \
			               || (port != blah))) { \
				callback(data, port);\
			} \
			cnt++;\
			seen[port]++;\
		}\
	}\
}
#endif

int full_scan(tcp_cb callback, unsigned int src_addr, 
		       unsigned int dst_addr, int timeout,
		       void *data)
{
   struct recv_tcp
   {
      struct iphdr ip;
      struct tcphdr tcp;
      unsigned char blah[65535];
   }recv_tcp;


   int blah = (getpid() & 0xFFF) + 3000;
   int total=0;
   int port;
   int cnt=0;
   struct timeval tv;
   fd_set fds;
   int res=1;
   int res2;
   int found;
   int seen[MAX_TCP];
   spoofrec s;
   memset(&seen, 0, sizeof(seen));
   
   /* Quickly, flush anything that is in the queue already. */
   while(res > 0)
   {
      FD_ZERO(&fds);
      FD_SET(tcp_socket, &fds);
      tv.tv_sec = 0;
      tv.tv_usec = 0;
      res = select(tcp_socket + 1, &fds, NULL, NULL, &tv);
      if (res > 0) 
      	res2=recv(tcp_socket, (struct recv_tcp *)&recv_tcp, 65535, 0);
   }
   for (port=1;port<=MAX_TCP;port++) {
   	/* Send out the requests */
	total++;

   	s.sport =  ((getpid() + blah) & 0xFFF) + 3000;
	s.seq = rand() & 0xffff;
	s.ack = 0;
	s.retries=0;
	s.next=NULL;
	s.from.sin_addr.s_addr = src_addr;
	s.dest.sin_addr.s_addr = dst_addr;
	s.dport = port;
	s.dest.sin_port = htons(s.dport);
	s.dest.sin_family = AF_INET;
	s.data=NULL;
	sendtcp(&s, SYN, 1);
	/* Check to see if we have a reply yet, and service
	   them before new requests go out */
	do {
        	FD_ZERO(&fds);
        	FD_SET(tcp_socket, &fds);
        	tv.tv_sec = 0;
        	tv.tv_usec = 0;
        	res = select(tcp_socket + 1, &fds, NULL, NULL, &tv);
        	if (res > 0) 
      			CHECK_SELECT; 
	} while (res > 0);
	/* Sleep if we're going too fast */
	if (total - cnt > 20)
		usleep(Zzz);
   }
   res=1;
   tv.tv_sec = timeout;
   tv.tv_usec = 0;
   total=MAX_TCP - 1;
   while(res > 0)
   {
      /* Count on Linux's uniqiue in-place timeval modification to make this work... */
      FD_ZERO(&fds);
      FD_SET(tcp_socket, &fds);
      res = select(tcp_socket + 1, &fds, NULL, NULL, &tv);
      if (res > 0) {
        CHECK_SELECT;
	if (cnt >= total) break;
#ifdef DEBUG
	printf("cnt is %d, total is %d\n",cnt,total);
#endif
      }
   }
   return cnt;
}