Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- # requires system path to contain links to dig, sslscan and nmap.
- # autoflush the output by turning buffering off
- $|++;
- use strict;
- use warnings;
- use Switch;
- use Data::Dumper;
- my $debug = 0; # 1 outputs all to screen, 0 outputs to file
- my $output = 2; # (1 - all, 2 - error certs only, 3 - valid certs only) valid = certs meeting algo and keysize requirements
- my $date = `date +%Y-%m-%d_%H%M%S`;
- chomp($date);
- my $outfile = "/tmp/sslscan-$date.csv";
- if (! $debug) { print "Result file: $outfile\n"; }
- my $timeout = 5; # command execution timeout
- my @networks = ( # subnets to scan
- '10.10.1.0/24',
- );
- my @ports = ( # ports to scan for SSL certs
- '443',
- '8443',
- );
- my @goodalgos = (
- 'sha256WithRSAEncryption',
- );
- my %goodalgo = map { $_ => 0 } @goodalgos;
- my $minkeysize = 2048; # minimum valid key length
- my @ignoredips = ( # ips to ignore
- );
- my %ignored = map { $_ => 0 } @ignoredips;
- my @sslhosts;
- foreach my $network (@networks){
- if ($debug) { print "Scanning subnet $network\n"; }
- foreach my $port (@ports){
- my @nmapout = `nmap -p$port -Pn -oG - $network`;
- foreach my $line (@nmapout){
- chomp ($line);
- if ($line =~ /open/){
- my (undef, $ip, undef) = split (/ /, $line, 3);
- if (! exists($ignored{$ip})){
- push @sslhosts, "$ip:$port";
- }
- }
- }
- }
- }
- #print @sslhosts;
- my %certs = ();
- foreach my $host (@sslhosts){
- if ($debug) { print "Scaning Host: $host\n"; }
- my @sslscan = `timeout $timeout sslscan --no-failed $host`;
- my ($cn, $algo, $keysize, $start, $end, $issuer) = "";
- my ($ip, $port) = split (/:/, $host, 2);
- # if ($debug) { print "IP: $ip port: $port\n"; }
- $certs{$ip}{$port} = ();
- foreach my $line (@sslscan){
- chomp ($line);
- switch ($line){
- case /Public-Key:/ {
- $line =~ /(\d+)/;
- $keysize = $1;
- if ($debug) { print "Key: $keysize\n"; }
- $certs{$ip}{$port}{'keysize'} = $keysize;
- }
- case /Subject:/ {
- $line =~ /CN=(.*)/;
- $cn = $1;
- if (length $cn){
- }else{
- $cn = `timeout $timeout dig +short -x $ip`;
- chomp($cn);
- chop($cn);
- }
- if ($debug) { print "CN: $cn\n"; }
- $certs{$ip}{$port}{'cn'} = $cn;
- }
- case /Signature Algorithm:/ {
- $line =~ /Signature Algorithm:\W(.*)/;
- $algo = $1;
- if ($debug) { print "Algorithm: $algo\n"; }
- $certs{$ip}{$port}{'algo'} = $algo;
- }
- case /Issuer:/ {
- $line =~ /Issuer:\W(.*)/;
- $issuer = $1;
- if ($debug) { print "Issuer: $issuer\n"; }
- $certs{$ip}{$port}{'issuer'} = $issuer;
- }
- case /Not valid before:/ {
- $line =~ /Not valid before:\s(\w+\s+\d+).*(\d{4})/;
- my $date = "$1 $2";
- if ($debug) { print "Start Date: $date\n"; }
- $certs{$ip}{$port}{'start'} = $date;
- }
- case /Not valid after:/ {
- $line =~ /Not valid after:\s(\w+\s+\d+).*(\d{4})/;
- my $date = "$1 $2";
- if ($debug) { print "End Date: $date\n"; }
- $certs{$ip}{$port}{'end'} = $date;
- }
- }
- }
- }
- if ($debug) { print "Completed Scan\n"; }
- if ($debug) { print Dumper %certs; }
- if ($debug) { print "^IP^Port^Name^Keysize^Algorithm^Issuer^Start^End^Life^\n"; }
- else {
- open(FH, '>', $outfile);
- print FH "^IP^Port^Name^Keysize^Algorithm^Issuer^Start^End^Life^\n";
- }
- foreach my $ip (keys %certs){
- foreach my $port (keys %{$certs{$ip}}){
- if (defined $certs{$ip}{$port}){
- my $line = "";
- switch ($output) {
- case 1 { # all certs
- $line = "|$ip|$port|$certs{$ip}{$port}{'cn'}|$certs{$ip}{$port}{'keysize'}|$certs{$ip}{$port}{'algo'}|";
- $line = $line."$certs{$ip}{$port}{'issuer'}|$certs{$ip}{$port}{'start'}|$certs{$ip}{$port}{'end'}|";
- }
- case 2 { # error certs
- if ($certs{$ip}{$port}{'keysize'} < $minkeysize || ! exists $goodalgo{$certs{$ip}{$port}{'algo'}}){
- $line = "|$ip|$port|$certs{$ip}{$port}{'cn'}|$certs{$ip}{$port}{'keysize'}|$certs{$ip}{$port}{'algo'}|";
- $line = $line."$certs{$ip}{$port}{'issuer'}|$certs{$ip}{$port}{'start'}|$certs{$ip}{$port}{'end'}|";
- }
- }
- case 3 { # valid certs
- if ($certs{$ip}{$port}{'keysize'} >= $minkeysize && exists $goodalgo{$certs{$ip}{$port}{'algo'}}){
- $line = "|$ip|$port|$certs{$ip}{$port}{'cn'}|$certs{$ip}{$port}{'keysize'}|$certs{$ip}{$port}{'algo'}|";
- $line = $line."$certs{$ip}{$port}{'issuer'}|$certs{$ip}{$port}{'start'}|$certs{$ip}{$port}{'end'}|";
- }
- }
- }
- if (length $line){
- if ($debug) { print "$line\n"; }
- else { print FH "$line\n"; }
- }
- }
- }
- }
- if (! $debug){
- close(FH);
- }
Advertisement
Add Comment
Please, Sign In to add comment