script/nf-trace.pl untested

filter: Statementstmt 0.0% 0 / 119 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 244 CC 14 SCAR 39.9
CC
14
Cov
0%
tracePath
132.0
getProbeFrame
20.0
END
2.0
CRAP 53.8
1 #!/usr/local/bin/perl
2 #
3 # $Id: nf-trace.pl 349 2015-01-23 06:44:44Z gomor $
4 #
5 use strict;
6 use warnings;
7  
8 our $VERSION = '0.10';
9  
10 my $target = shift || die("Specify target IPv4 address\n");
11 my $port = shift || die("Specify target open port\n");
12 my $data = shift || die("Specify application layer data to send\n");
13 my $proto = shift || 'tcp';
14  
15 use Net::Frame::Device;
16 use Net::Write::Layer3;
17 use IO::Socket;
18 use Net::Frame::Simple;
19 use Net::Frame::Layer::IPv4;
20 use Net::Frame::Layer::TCP qw(:consts);
21 use Net::Frame::Layer::UDP;
22 use Net::Frame::Dump::Online;
23  
24 my $oDevice = Net::Frame::Device->new(target => $target);
25  
26 my $oDump = getDump($oDevice, $target, $port)
27 or die("Unable to open Dump object\n");
28  
29 my $s = connectTo($target, $port, $proto)
30 or die("Unable to connect()\n");
31  
32 sendData($s, $data)
33 or die("Unable to send data to target\n");
34  
35 my $probeFrame = getProbeFrame($oDump, $oDevice, $target, $port)
36 or die("Unable to get probe frame\n");
37  
38 #
39 # Once we have the probe frame, we can build frames used to identify the
40 # path to the target. Thanks to this probe frame, a state entry has been
41 # created through the target, and our tracing frame should go unfiltered
42 # to the target
43 #
44  
45 my $result = tracePath($probeFrame, $oDevice, $oDump);
46 #sleep(10);
47  
48 #use Data::Dumper;
49 #print Dumper($result)."\n";
50  
51 END { $oDump && $oDump->isRunning && $oDump->stop }
52  
53 #
54 # Subs
55 #
56  
57 sub getDump {
58 my ($oDevice, $target, $port) = @_;
59 my $oDump = Net::Frame::Dump::Online->new(
60 dev => $oDevice->dev,
61 filter =>
62 " (ip host @{[$oDevice->ip]} and port $port) ".
63 "or (dst host @{[$oDevice->ip]} and icmp)",
64 timeoutOnNext => 5,
65 keepTimestamp => 1,
66 );
67 $oDump->start;
68 $oDump;
69 }
70  
71 sub connectTo {
72 my ($target, $port, $proto) = @_;
73  
74 IO::Socket::INET->new(
75 PeerAddr => "$target:$port",
76 Proto => $proto,
77 ) or die("IO::Socket::INET::new: $!\n");
78 }
79  
80 sub sendData {
81 my ($s, $data) = @_;
82 $s->send($data) or die("send: $!\n");
83 }
84  
85 sub getProbeFrame {
86 my ($oDump, $oDevice, $target, $port) = @_;
87 my $probe;
88 until ($oDump->timeout) {
89 if (my $h = $oDump->next) {
90 unless ($probe) {
91 my $f = Net::Frame::Simple->newFromDump($h);
92 my $ip = $f->ref->{IPv4};
93 my $tcp = $f->ref->{TCP};
94 if (($ip && $ip->dst eq $target
95 && $ip->src eq $oDevice->ip)
96 && ($tcp && $tcp->dst eq $port && $tcp->payload)) {
97 $probe = $f;
98 }
99 }
100 }
101 }
102 $probe;
103 }
104  
105 sub tracePath {
106 my ($probe, $oDevice, $oDump) = @_;
107  
108 my $ip = $probe->ref->{IPv4}->cgClone;
109 my $tcp = $probe->ref->{TCP}->cgClone;
110  
111 my $baseTtl = $ip->id;
112 print "Base TTL: $baseTtl\n";
113  
114 my $frames;
115 for (1..17) {
116 $ip->checksum(0);
117 $ip->id($baseTtl + $_);
118 $ip->ttl($_);
119 my $send = Net::Frame::Simple->new(
120 layers => [ $ip->cgClone, $tcp->cgClone, ],
121 );
122 $frames->{$_} = $send;
123 }
124  
125 my $w = Net::Write::Layer3->new(dst => $probe->ref->{IPv4}->dst);
126 $w->open;
127  
128 my $last;
129 for (1..3) {
130 print "Try number: $_\n";
131 for my $thisId (sort { $a <=> $b } keys %$frames) {
132 if ($last && $thisId > $last) {
133 delete $frames->{$thisId};
134 next;
135 }
136 my $this = $frames->{$thisId};
137  
138 unless ($this->reply) {
139 $this->send($w);
140  
141 my $thisReply;
142  
143 $oDump->timeoutReset;
144 until ($oDump->timeout) {
145 if (my $h = $oDump->next) {
146 my $reply = Net::Frame::Simple->newFromDump($h);
147 if ($reply->timestamp > $this->timestamp) {
148 my $ip = $reply->ref->{IPv4};
149 my $icmp = $reply->ref->{ICMPv4};
150 my $tcp = $reply->ref->{TCP};
151 if ($icmp && $icmp->type == 11) {
152 for ($reply->layers) {
153 if ($_->layer eq 'IPv4') {
154 last if $_->dst ne $oDevice->ip;
155 my $hop = $ip->id - $baseTtl;
156 print "ICMP($hop): ".$_->src."\n";
157 $thisReply = $_->src;
158 }
159 }
160 }
161 elsif ($tcp && $ip->src eq $probe->ref->{IPv4}->dst) {
162 my $hop = $ip->id - $baseTtl;
163 print "TCP($hop): ".$reply->ref->{IPv4}->src."\n";
164 $thisReply = $reply->ref->{IPv4}->src;
165 $last = $hop;
166 }
167 }
168 }
169 if ($thisReply) {
170 $this->reply($thisReply);
171 last;
172 }
173 }
174 }
175 }
176 }
177  
178 for (sort { $a <=> $b } keys %$frames) {
179 if ($frames->{$_}->reply) {
180 print "HOP($_): ".$frames->{$_}->reply."\n";
181 }
182 else {
183 print "HOP($_): unknown\n";
184 }
185 }
186  
187 $w->close;
188 }
189  
190 __END__