Repository navigation
Expand file tree
/
Copy pathtcptrace.cpp
More file actions
306 lines (257 loc) · 9.88 KB
/
Copy pathtcptrace.cpp
File metadata and controls
306 lines (257 loc) · 9.88 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// Copyright (c) 2005 L.M.Witek
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from
// the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
#include <cstdlib>
#include "packet\PacketDefs.h"
#include "net\socket.h"
#include "neo\TimeOut.h"
#include "tcptrace.h"
#include "RawSocketPacketInterface.h"
#include "WinpcapPacketInterface.h"
using namespace packet;
enum ResponsePacketTypes
{
NOT_VALID, // the packet was not a response to something we sent.
ICMP_ECHO_RESP, // ping response
ICMP_TTL_EXP, // packet expired in transit.
ICMP_NO_ROUTE, // no route to host
ICMP_SOURCE_QU,
ICMP_REDIRECT,
TCP_RST,
TCP_SYNACK
};
// Compares what we sent to what we recieved back an determines if the packet is in reply to what we sent
ResponsePacketTypes validateResponse(TCPIPHeader& sent, neo::MemoryBlock& resp)
{
IPHeader* responseIP = (IPHeader*)(resp.getBlock());
if (responseIP->protocol == IPPROTO_ICMP)
{
// the response is ICMP - so find out the type of the response
ICMPHeader* icmpheader = (ICMPHeader*)(resp.getBlock() + responseIP->getHeaderSize());
switch (icmpheader->type)
{
case ICMPHeader::DEST_UNREACH:
case ICMPHeader::TTL_EXPIRED:
case ICMPHeader::SOURCE_QUENCH:
case ICMPHeader::REDIRECT:
{
ICMPErrorHeader* errorRep = (ICMPErrorHeader*)icmpheader;
IPHeader* returnedIP = &(errorRep->ipHeader);
// recover the the fist 64 bits of the original packet.
TCPHeader* returnedTCP = (TCPHeader*)(resp.getBlock() + responseIP->getHeaderSize() + errorRep->getHeaderSize());
// only src port, dest port & seq num will be present in the returned packet.
if (sent.ip.id == returnedIP->id &&
sent.ip.destIP == returnedIP->destIP &&
sent.tcp.destPort == returnedTCP->destPort &&
sent.tcp.seqNum == returnedTCP->seqNum)
{
// the error contained the packet we sent
switch (icmpheader->type)
{
case ICMPHeader::DEST_UNREACH: return ICMP_NO_ROUTE;
case ICMPHeader::TTL_EXPIRED: return ICMP_TTL_EXP;
case ICMPHeader::SOURCE_QUENCH: return ICMP_SOURCE_QU;
case ICMPHeader::REDIRECT: return ICMP_REDIRECT;
}
}
}
break;
}
}
else if (responseIP->protocol == IPPROTO_TCP)
{
// the response is TCP - is it a response to what we sent.
TCPHeader* responseTCP = (TCPHeader*)(resp.getBlock() + responseIP->getHeaderSize());
if (sent.ip.destIP == responseIP->sourceIP &&
sent.ip.sourceIP == responseIP->destIP &&
sent.tcp.destPort == responseTCP->sourcePort &&
sent.tcp.sourcePort == responseTCP->destPort &&
(sent.tcp.seqNum+1) == responseTCP->ackNum)
{
if (responseTCP->rst)
return TCP_RST;
if (responseTCP->ack && responseTCP->syn)
return TCP_SYNACK;
}
}
return NOT_VALID;
}
u_short ip_cksum(u_short *addr, int len);
void createTCPPacket (TCPIPHeader& h, const net::InetAddress& target, const net::InetAddress& source, int ttl)
{
struct fakeheader // used to calculate the checksum
{
FakeIPHeader fip;
TCPHeader tcp;
};
u_long seqNum = (rand() << 15) + rand();
// workout checksum
fakeheader fh;
fh.fip.clear();
fh.tcp.clear();
fh.fip.length = sizeof(TCPHeader);
fh.fip.destIP = target.getIPAddress();
fh.fip.sourceIP = source.getIPAddress();
fh.fip.protocol = IPPROTO_TCP;
fh.tcp.destPort = target.getPortNumber();
fh.tcp.sourcePort = rand() + 0x2000;
fh.tcp.syn = 1;
fh.tcp.seqNum = seqNum;
fh.tcp.ackNum = 0;
fh.tcp.dataOffset = sizeof(TCPHeader) / 4;
fh.tcp.window = 0x2000;
fh.tcp.mssOptionKind = 2;
fh.tcp.mssOptionLen = 4;
fh.tcp.mssOptionVal = 1460;
fh.tcp.optionNop1 = 1;
fh.tcp.winScaleOptionKind = 3;
fh.tcp.winScaleOptionLen = 3;
fh.tcp.winScaleOptionVal = 2;
fh.tcp.optionNop2 = 1;
fh.tcp.optionNop3 = 1;
fh.tcp.TCPSACKOptionKind = 4;
fh.tcp.TCPSACKOptionLen = 2;
fh.tcp.checksum = in_cksum((u_short*)&fh, sizeof(fh));
// create the ip& tcp header to send
h.ip.clear();
h.tcp = fh.tcp;
h.ip.length = 5;
h.ip.destIP = target.getIPAddress();
h.ip.sourceIP = source.getIPAddress();
h.ip.protocol = IPPROTO_TCP;
h.ip.version = 4;
h.ip.dontFrag = 1;
h.ip.moreFrags = 0;
h.ip.id = (u_short)GetCurrentProcessId();
h.ip.TTL = ttl;
h.ip.totLength = sizeof (h);
h.ip.headerChecksum = 0;
h.ip.headerChecksum = in_cksum((u_short*)&h.ip, sizeof(h.ip));
}
ResponsePacketTypes doTCPPing (neo::RefPtr<IRawPacketInterface> rawInterface, const net::InetAddress& target, const net::InetAddress& source, int ttl, net::InetAddress& respFrom, DWORD& pingTime, DWORD timeout)
{
TCPIPHeader h;
createTCPPacket (h, target, source, ttl);
neo::TimeOut timeOutCounter (timeout);
rawInterface->sendPacket ((const char*)&h, sizeof(h));
// get respose
neo::MemoryBlock recBuffer(0x10000);
ResponsePacketTypes respType = NOT_VALID;
do
{
if (rawInterface->recPacket (recBuffer, respFrom, timeOutCounter.getRemainingTime()))
{
respType = validateResponse (h, recBuffer);
}
}
while ((NOT_VALID == respType) && !timeOutCounter.hasTimedOut());
pingTime = timeOutCounter.getElapsedTime();
return respType;
}
void doTraceTCP (TCPTraceSettings& settings, ITraceOutput& out, TraceTerminator& terminator)
{
srand ((unsigned)time(NULL));
net::InetAddress target (settings.remoteHost.c_str());
if (target.getPortNumber() == 0)
target.setPortNumber(80);
if (!settings.portRange)
{
settings.startPort = target.getPortNumber();
settings.endPort = target.getPortNumber();
}
neo::RefPtr<IRawPacketInterface> rawInterface;
if (settings.useRawSockets)
rawInterface = new RawSocketPacketInterface();
else
rawInterface = new WinpcapPacketInterface();
if (settings.forceGW != "")
rawInterface->overrideGateway(net::InetAddress(settings.forceGW.c_str()));
rawInterface->initialise(target);
for (DWORD port = settings.startPort; port <= settings.endPort; port++)
{
target.setPortNumber ((u_short)port);
bool traceComplete = false;
out.startTrace (target, settings.noRDNS, settings.maxHops, false);
for (DWORD hop = settings.startHop; hop < (settings.startHop + settings.maxHops); hop++)
{
out.startHop(hop);
bool hopComplete = false;
for (DWORD m = 0; (m < settings.pingsPerHop) & !hopComplete; m++)
{
if (terminator.isTerminated())
throw std::string("Terminate Event Occurred.");
DWORD pingTime;
net::InetAddress resp;
ResponsePacketTypes respType = doTCPPing(rawInterface, target, rawInterface->getSourceAddress(), hop, resp, pingTime, settings.maxTimeout);
if (respType == TCP_RST) {
// destination reached but port is closed, this maybe due to some hosts not allowing connections with ttl = 0.
// resend ping with large ttl to see if its really closed.
respType = doTCPPing(rawInterface, target, rawInterface->getSourceAddress(), 127, resp, pingTime, settings.maxTimeout);
}
if (respType == NOT_VALID)
{
out.pingResultTimeout();
}
else if (respType == ICMP_NO_ROUTE)
{
out.pingResultBad (resp, "NO ROUTE");
traceComplete = true;
hopComplete = true;
}
else if (respType == ICMP_REDIRECT)
{
out.pingResultBad (resp, "ICMP REDIRECT recieved");
traceComplete = true;
hopComplete = true;
}
else if (respType == TCP_RST)
{
out.destinationReached (resp, pingTime, false);
traceComplete = true;
hopComplete = true;
}
else if (respType == TCP_SYNACK)
{
out.destinationReached (resp, pingTime, true);
traceComplete = true;
hopComplete = true;
}
else
{
out.pingResultGood (resp, pingTime);
}
// wait at least 0.5 second between pings....
// as sending the SYN packets too fast seems to trigger some form of flood detection on some hosts
if (!settings.noAntiFlood)
{
if ((pingTime < 500) && !hopComplete)
{
Sleep (500 - pingTime);
}
}
}
if (traceComplete)
break;
out.endHop ();
}
out.endTrace();
}
}