forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto-icmp.c
178 lines (157 loc) · 6.02 KB
/
proto-icmp.c
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
#include "proto-icmp.h"
#include "proto-preprocess.h"
#include "syn-cookie.h"
#include "logger.h"
#include "output.h"
#include "masscan-status.h"
#include "templ-port.h"
/***************************************************************************
***************************************************************************/
static int
matches_me(struct Output *out, unsigned ip, unsigned port)
{
unsigned i;
for (i=0; i<8; i++) {
if (is_myself(&out->src[i], ip, port))
return 1;
}
return 0;
}
/***************************************************************************
***************************************************************************/
static int
parse_port_unreachable(const unsigned char *px, unsigned length,
unsigned *r_ip_me, unsigned *r_ip_them,
unsigned *r_port_me, unsigned *r_port_them,
unsigned *r_ip_proto)
{
if (length < 24)
return -1;
*r_ip_me = px[12]<<24 | px[13]<<16 | px[14]<<8 | px[15];
*r_ip_them = px[16]<<24 | px[17]<<16 | px[18]<<8 | px[19];
*r_ip_proto = px[9]; /* TCP=6, UDP=17 */
px += (px[0]&0xF)<<2;
length -= (px[0]&0xF)<<2;
if (length < 4)
return -1;
*r_port_me = px[0]<<8 | px[1];
*r_port_them = px[2]<<8 | px[3];
return 0;
}
/***************************************************************************
* This is where we handle all incoming ICMP packets. Some of these packets
* will be due to scans we are doing, like pings (echoes). Some will
* be inadvertent, such as "destination unreachable" messages.
***************************************************************************/
void
handle_icmp(struct Output *out, time_t timestamp,
const unsigned char *px, unsigned length,
struct PreprocessedInfo *parsed,
uint64_t entropy)
{
unsigned type = parsed->port_src;
unsigned code = parsed->port_dst;
unsigned seqno_me;
unsigned ip_me;
unsigned ip_them;
unsigned cookie;
ip_me = parsed->ip_dst[0]<<24 | parsed->ip_dst[1]<<16
| parsed->ip_dst[2]<< 8 | parsed->ip_dst[3]<<0;
ip_them = parsed->ip_src[0]<<24 | parsed->ip_src[1]<<16
| parsed->ip_src[2]<< 8 | parsed->ip_src[3]<<0;
seqno_me = px[parsed->transport_offset+4]<<24
| px[parsed->transport_offset+5]<<16
| px[parsed->transport_offset+6]<<8
| px[parsed->transport_offset+7]<<0;
switch (type) {
case 0: /* ICMP echo reply */
cookie = (unsigned)syn_cookie(ip_them, Templ_ICMP_echo, ip_me, 0, entropy);
if ((cookie & 0xFFFFFFFF) != seqno_me)
return; /* not my response */
//if (syn_hash(ip_them, Templ_ICMP_echo) != seqno_me)
// return; /* not my response */
/*
* Report "open" or "existence" of host
*/
output_report_status(
out,
timestamp,
PortStatus_Open,
ip_them,
1, /* ip proto */
0,
0,
parsed->ip_ttl,
parsed->mac_src);
break;
case 3: /* destination unreachable */
switch (code) {
case 0: /* net unreachable */
/* We get these a lot while port scanning, often a flood coming
* back from broken/misconfigured networks */
break;
case 1: /* host unreachable */
/* This means the router doesn't exist */
break;
case 2: /* protocol unreachable */
/* The host exists, but it doesn't support SCTP */
break;
case 3: /* port unreachable */
if (length - parsed->transport_offset > 8) {
unsigned ip_me2, ip_them2, port_me2, port_them2;
unsigned ip_proto;
int err;
err = parse_port_unreachable(
px + parsed->transport_offset + 8,
length - parsed->transport_offset + 8,
&ip_me2, &ip_them2, &port_me2, &port_them2,
&ip_proto);
if (err)
return;
if (!matches_me(out, ip_me2, port_me2))
return;
switch (ip_proto) {
case 6:
output_report_status(
out,
timestamp,
PortStatus_Closed,
ip_them2,
ip_proto,
port_them2,
0,
parsed->ip_ttl,
parsed->mac_src);
break;
case 17:
output_report_status(
out,
timestamp,
PortStatus_Closed,
ip_them2,
ip_proto,
port_them2,
0,
parsed->ip_ttl,
parsed->mac_src);
break;
case 132:
output_report_status(
out,
timestamp,
PortStatus_Closed,
ip_them2,
ip_proto,
port_them2,
0,
parsed->ip_ttl,
parsed->mac_src);
break;
}
}
}
break;
default:
;
}
}