averlon has asked for the wisdom of the Perl Monks concerning the following question:

Hi - I am not a good programmer in perl.

I use the module Masscan::Scanner. But I have not found a way to process the modules result and would like to have some help on this

# Returns the scan results my $res = $mas->scan_results if ($scan);

Any help how I can access e.g. the IP-Address in the result data structure or even the array (seems to be an array) of ports would be welcome

Regards Karl-Heinz

Replies are listed 'Best First'.
Re: processing a module result
by GrandFather (Saint) on May 18, 2022 at 05:56 UTC

    Reading the documentation for Masscan::Scanner may help. If it doesn't you might try showing the code you have tried using to process the results and tell us how it has failed.

    You say you are not a "good programmer in perl", but I suspect that means you are not an experienced Perl programmer. If you show us some code that will help us asses what you do know and the level of help you need. Read I know what I mean. Why don't you? for some hints about how you should go about writing something to show us. Most of us will not easily be able to use the Masscan::Scanner module!

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      Hi GrandFather,

      in this case I have tried to understand the documentation but failed so far.

      $av_obj_MAS = Masscan::Scanner->new(); $av_obj_MAS->add_host($av_loc_IP); $av_obj_MAS->add_port(@av_arr_PORTS); $av_obj_MAS->sudo(1); $av_obj_MAS->verbose(1); $av_tmp_SCAN = $av_obj_MAS->scan; if ($av_tmp_SCAN) { $av_obj_SCANRESULT = $av_obj_MAS->scan_results; $av_obj_RESULT = %$av_obj_SCANRESULT{scan_results}; @av_arr_SCANRESULT = @{%$av_obj_SCANRESULT{scan_results}}; # print "Test1: " . $%av_obj_RESULT{ip}; }

      The first line to get the result from the object obviously works. It is according to the documentation.

      It looks like the second line also brings one part of the result to a variable.

      But to access the IP (and print it) and the array of port is still not working.

      The documentation of the module shows some example data structure provided as result.

      { 'scan_results' => [ { 'timestamp' => '1584816181', 'ip' => '10.0.0.1', 'ports' => [ { 'status' => 'open', 'reason' => 'syn-ack', 'port' => 443, 'proto' => 'tcp', 'ttl' => 60 } ] }, { 'timestamp' => '1584816181', 'ip' => '10.0.0.2', 'ports' => [ { 'reason' => 'syn-ack', 'status' => 'open', 'port' => 443, 'ttl' => 60, 'proto' => 'tcp' } ] }, { 'ports' => [ { 'port' => 80, 'ttl' => 60, 'proto' => 'tcp', 'reason' => 'syn-ack', 'status' => 'open' } ], 'ip' => '10.0.0.1', 'timestamp' => '1584816181' }, { 'ip' => '10.0.0.2', 'timestamp' => '1584816181', 'ports' => [ { 'port' => 80, 'ttl' => 60, 'proto' => 'tcp', 'status' => 'open', 'reason' => 'syn-ack' } ] }, { 'timestamp' => '1584816181', 'ip' => '10.0.0.3', 'ports' => [ { 'reason' => 'syn-ack', 'status' => 'open', 'proto' => 'tcp', 'ttl' => 111, 'port' => 80 } ] }, { 'ports' => [ { 'ttl' => 111, 'proto' => 'tcp', 'port' => 443, 'reason' => 'syn-ack', 'status' => 'open' } ], 'timestamp' => '1584816181', 'ip' => '10.0.0.3' } ], 'masscan' => { 'scan_stats' => { 'total_hosts' => 4, 'up_hosts' => 3 }, 'command_line' => '/usr/bin/masscan --rate 100000 + --banners -p 22,80,443,61222,25 10.0.0.2,10.0.0.1,10.0.0.3,10.0.0.4' } };

      with:

      $av_obj_SCANRESULT = $av_obj_MAS->scan_results;

      I geht the data structure into a scalar - so far so good

      now I would like e.g. to print the IP-Address as part of the data structure or move the whole result into an array or just run a foreach ... to process the results list.

      but I don't know how

      Regards Kallewirsch
        Hello averlon,

        mainly our goal is to help you in learning, not providing solutions, so I'd start with:

        • use strict; use warnings; always in your program
        this will save a lot of trouble and has not drawbacks, so let perl help you: it is amazing in this

        • Then, regarding your code:
        # doc my $mas = Masscan::Scanner->new(hosts => \@hosts, ports => \@ports, ar +guments => \@arguments); #your code $av_obj_MAS->add_port(@av_arr_PORTS);

        They are sligltly different... where? \@ports is a reference to an array while @av_arr_PORTS is an array. Look carefully at the documentation: arguments are always passed as a single scalar like in $mas->add_port(25) or as array reference like in $mas->ports(['22', '80', '443']); but never as list or array.

        • about accessing the results
        The doc says it: SCAN-RESULTS. I invite you to inspect yourself this result using the core module Data::Dumper or the CPAN one Data::Dump (I prefer the latter, but in some edge case the former is more accurate: get used to both).

        Consider the following oneliner (pay attention to windows double quotes around the code, single quote needed for Linux):

        perl -MData::Dumper -e "$hash{lvl1}[3]{lvl3}[0] = 42; print Dumper \%h +ash" $VAR1 = { 'lvl1' => [ undef, undef, undef, { 'lvl3' => [ 42 ] } ] };

        You are in a similar situation: perldsc is definitevely a good read.

        Modify your program and share your achievements: they will probably bring up new questions

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        Hi

        I guess, I found a solution myself

        I will post it once it is completely working

        Regards Kallewirsch
Re: processing a module result
by Anonymous Monk on Apr 17, 2023 at 07:25 UTC

    Here the code

    my $scan = $mas->scan; if ($scan) { my $res = $mas->scan_results; my $res_result = $res->{scan_results}; foreach my $rec (@{$res_result}){ foreach my $port (@{$rec->{ports}}) { if (($port->{port})) { print "Adding [", $rec->{ip}.":".$port->{port},"]\ +n"; } } } }