| 1 | # | ||
| 2 | # $Id: Ubigraph.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $ | ||
| 3 | # | ||
| 4 | package Net::SinFP3::Output::Ubigraph; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use base qw(Net::SinFP3::Output); | ||
| 9 | our @AS = qw( | ||
| 10 | file | ||
| 11 | _data | ||
| 12 | _ug | ||
| 13 | _nodes | ||
| 14 | ); | ||
| 15 | __PACKAGE__->cgBuildIndices; | ||
| 16 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 17 | |||
| 18 | use Data::Dumper; | ||
| 19 | # Make it optional | ||
| 20 | eval("use Frontier::Client;"); | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $self = shift->SUPER::new( | ||
| 24 | @_, | ||
| 25 | ); | ||
| 26 | |||
| 27 | my $global = $self->global; | ||
| 28 | my $log = $global->log; | ||
| 29 | |||
| 30 | if (!defined($self->file)) { | ||
| 31 | $log->fatal("You must provide file attribute"); | ||
| 32 | } | ||
| 33 | |||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _parseCsvFile { | ||
| 38 | my $self = shift; | ||
| 39 | my ($fd) = @_; | ||
| 40 | |||
| 41 | my $data = {}; | ||
| 42 | while (my $line = <$fd>) { | ||
| 43 | chomp($line); | ||
| 44 | my @toks = split(';', $line); | ||
| 45 | my $next = $toks[0]; | ||
| 46 | my $ip = $toks[1]; | ||
| 47 | my $port = $toks[2]; | ||
| 48 | my $nOs = $toks[3]; | ||
| 49 | my $osList = $toks[4]; | ||
| 50 | $data->{$ip}->{$port} = { ip => $ip, port => $port, nOs => $nOs, osList => $osList }; | ||
| 51 | } | ||
| 52 | |||
| 53 | return $data; | ||
| 54 | } | ||
| 55 | |||
| 56 | my %Colors = ( | ||
| 57 | 'Linux' => "#00ff00", # Green | ||
| 58 | 'Windows' => "#0000ff", # Blue | ||
| 59 | 'FreeBSD' => "#ff0000", # Red | ||
| 60 | 'OpenBSD' => "#ff0000", # Red | ||
| 61 | 'NetBSD' => "#ff0000", # Red | ||
| 62 | 'Darwin' => "#ffff00", # Yellow | ||
| 63 | 'SunOS' => "#ff00ff", # Violet | ||
| 64 | ); | ||
| 65 | |||
| 66 | sub run { | ||
| 67 | my $self = shift->SUPER::run(@_) or return; | ||
| 68 | |||
| 69 | my $global = $self->global; | ||
| 70 | my $log = $global->log; | ||
| 71 | my @results = $global->result; | ||
| 72 | |||
| 73 | # Read file and create structure | ||
| 74 | open(my $in, '<', $self->file) | ||
| 75 | or $log->fatal("Cannot open file: ".$self->file); | ||
| 76 | my $data = $self->_parseCsvFile($in); | ||
| 77 | $self->_data($data); | ||
| 78 | close($in); | ||
| 79 | |||
| 80 | print Dumper($data),"\n"; | ||
| 81 | |||
| 82 | # Prepare Ubigraph API | ||
| 83 | my $ug = Frontier::Client->new(url => 'http://127.0.0.1:20738/RPC2'); | ||
| 84 | |||
| 85 | # Clean env | ||
| 86 | $ug->call('ubigraph.clear'); | ||
| 87 | |||
| 88 | # The scanning host | ||
| 89 | my $me = $ug->call('ubigraph.new_vertex'); | ||
| 90 | $ug->call('ubigraph.set_vertex_attribute', $me, shape => "sphere"); | ||
| 91 | $ug->call('ubigraph.set_vertex_attribute', $me, color => "#ffffff"); | ||
| 92 | $ug->call('ubigraph.set_vertex_attribute', $me, label => "sinfp3"); | ||
| 93 | $ug->call('ubigraph.set_vertex_attribute', $me, size => "1.0 "); | ||
| 94 | |||
| 95 | my $count = -1; | ||
| 96 | my $done = 0; | ||
| 97 | my $prevIp; | ||
| 98 | my $firstIp; | ||
| 99 | my $lastIp; | ||
| 100 | for my $ip (keys %$data) { | ||
| 101 | my $newIp = $ug->call('ubigraph.new_vertex'); | ||
| 102 | $ug->call('ubigraph.set_vertex_attribute', $newIp, shape => "cube"); | ||
| 103 | $ug->call('ubigraph.set_vertex_attribute', $newIp, color => "#ffffff"); | ||
| 104 | $ug->call('ubigraph.set_vertex_attribute', $newIp, label => $ip); | ||
| 105 | $ug->call('ubigraph.set_vertex_attribute', $newIp, fontsize => "6 "); | ||
| 106 | $ug->call('ubigraph.set_vertex_attribute', $newIp, size => "1.0 "); | ||
| 107 | |||
| 108 | if (! $done) { | ||
| 109 | $ug->call('ubigraph.new_edge', $me, $newIp); | ||
| 110 | $done++; | ||
| 111 | } | ||
| 112 | |||
| 113 | if (! $prevIp) { | ||
| 114 | $firstIp = $newIp; | ||
| 115 | } | ||
| 116 | if ($prevIp) { | ||
| 117 | $ug->call('ubigraph.new_edge', $prevIp, $newIp); | ||
| 118 | } | ||
| 119 | $prevIp = $newIp; | ||
| 120 | $lastIp = $newIp; | ||
| 121 | |||
| 122 | for my $port (keys %{$data->{$ip}}) { | ||
| 123 | my $os = $data->{$ip}->{$port}->{osList}; | ||
| 124 | my $color = $Colors{$os}; | ||
| 125 | if ($data->{$ip}->{$port}->{nOs} > 1) { | ||
| 126 | $os = 'Unknown'; | ||
| 127 | $color = "#ffffff"; | ||
| 128 | } | ||
| 129 | my $newPort = $ug->call('ubigraph.new_vertex'); | ||
| 130 | $ug->call('ubigraph.set_vertex_attribute', $newPort, shape => "octahedron"); | ||
| 131 | $ug->call('ubigraph.set_vertex_attribute', $newPort, color => $color); | ||
| 132 | $ug->call('ubigraph.set_vertex_attribute', $newPort, label => "$port/tcp"); | ||
| 133 | $ug->call('ubigraph.set_vertex_attribute', $newPort, fontsize => "3 "); | ||
| 134 | $ug->call('ubigraph.set_vertex_attribute', $newPort, size => "0.5 "); | ||
| 135 | |||
| 136 | $ug->call('ubigraph.new_edge', $newIp, $newPort); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | $ug->call('ubigraph.new_edge', $firstIp, $lastIp); | ||
| 141 | |||
| 142 | return 1; | ||
| 143 | } | ||
| 144 | |||
| 145 | 1; | ||
| 146 | |||
| 147 | __END__ |