Api/Shodan.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Api/Geonames.pm ↑ Summary Api/Splunk.pm »
1 #
2 # $Id$
3 #
4 # api::shodan Brik
5 #
6 package Metabrik::Api::Shodan;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Client::Rest);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # API: https://developer.shodan.io/api
13  
14 sub brik_properties {
15 return {
16 revision => '$Revision$',
17 tags => [ qw(unstable rest) ],
18 author => 'GomoR <GomoR[at]metabrik.org>',
19 license => 'http://opensource.org/licenses/BSD-3-Clause',
20 attributes => {
21 output_mode => [ qw(json|xml) ],
22 apikey => [ qw(apikey) ],
23 uri => [ qw(shodan_uri) ],
24 },
25 attributes_default => {
26 output_mode => 'json',
27 ssl_verify => 0,
28 uri => 'https://api.shodan.io',
29 },
30 commands => {
31 myip => [ qw(apikey|OPTIONAL) ],
32 api_info => [ qw(apikey|OPTIONAL) ],
33 host_ip => [ qw(ip_address apikey|OPTIONAL) ],
34 },
35 require_modules => {
36 'Metabrik::Network::Address' => [ ],
37 'Metabrik::String::Json' => [ ],
38 'Metabrik::String::Xml' => [ ],
39 },
40 };
41 }
42  
43 sub myip {
44 my $self = shift;
45  
46 my $apikey = $self->apikey;
47 $self->brik_help_run_undef_arg('myip', $apikey) or return;
48  
49 my $uri = $self->uri;
50  
51 my $resp = $self->get($uri.'/tools/myip?key='.$apikey) or return;
52 my $content = $resp->{content};
53  
54 $content =~ s/"?//g;
55  
56 return $content;
57 }
58  
59 sub api_info {
60 my $self = shift;
61 my ($apikey) = @_;
62  
63 $apikey ||= $self->apikey;
64 $self->brik_help_run_undef_arg('api_info', $apikey) or return;
65  
66 my $uri = $self->uri;
67  
68 my $resp = $self->get($uri.'/api-info?key='.$apikey) or return;
69 my $content = $resp->{content};
70  
71 my $sj = Metabrik::String::Json->new_from_brik_init($self) or return;
72 my $decoded = $sj->decode($content) or return;
73  
74 return $decoded;
75 }
76  
77 sub host_ip {
78 my $self = shift;
79 my ($ip, $apikey) = @_;
80  
81 $apikey ||= $self->apikey;
82 $self->brik_help_run_undef_arg('host_ip', $ip) or return;
83 $self->brik_help_run_undef_arg('host_ip', $apikey) or return;
84  
85 my $na = Metabrik::Network::Address->new_from_brik_init($self) or return;
86 if (! $na->is_ip($ip)) {
87 return $self->log->error("host_ip: invalid format for IP [$ip]");
88 }
89  
90 my $uri = $self->uri;
91  
92 my $resp = $self->get($uri.'/shodan/host/'.$ip.'?key='.$apikey) or return;
93 my $content = $resp->{content};
94  
95 my $sj = Metabrik::String::Json->new_from_brik_init($self) or return;
96 my $decoded = $sj->decode($content) or return;
97  
98 return $decoded;
99 }
100  
101 1;
102  
103 __END__
« Api/Geonames.pm ↑ Summary Api/Splunk.pm »