#!/usr/bin/perl -w
#
# Copyright (C) 2010, Joshua D. Abraham (jabra@spl0it.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
# use strict;
#
use strict;
use LWP::UserAgent;
use Getopt::Long;
my $ua = LWP::UserAgent->new;
use vars qw( $PROG );
( $PROG = $0 ) =~ s/^.*[\/\\]//;    # Truncate calling path from the prog name

my $AUTH    = 'Joshua D. Abraham';  # author
my $EMAIL    = 'jabra@spl0it.org';  # email
my $VERSION = '1.0';                # version

my %options;
my $url;

#
# help: ->
# display help information
# side effect:  exits program
#
sub help {
    print "Usage: $PROG [Options]
   -u  --url                      Get URL
   
   -m  --month                    Month 1-12
   -y  --year                     Year (default: 2010)

   -v  --version                  Display version
   -h  --help                     Display this information

Send Comments to $AUTH ( $EMAIL )\n";
    exit;
}

#
# print_version ->
# displays version
# side effect: exits program
#
sub print_version {
    print "$PROG version $VERSION by $AUTH ( $EMAIL )\n";
    exit;
}

#
# get: value1 value2  ->
# get values to url
#
sub get {
    my %cves;
    my $response = $ua->get($url);
    # Check the outcome of the response
    if ($response->is_success) {
        foreach(split("<\/h4>",$response->content)) {
            if (/CVE-(\d{4})-(\d{4})<\/a>/) {
                my $year=$1;
                my $id=$2;
                $cves{"CVE-$year-$id\n"}++;
            }
        }
    }
    else {
        print "Failiure" . $response->status_line, "\n";
    }
    if ($url =~ /(MS\d{2}-\d{3})/i) {
        print "$1 " . "Total: " . scalar(keys(%cves)) . "\n";
    }
    print sort(keys(%cves));
}

GetOptions(
    \%options,
    'url|u=s','month|m=s','year|y=s','first|f=s','num|n=s',
    'help|h'    => sub { help(); },
    'version|v' => sub { print_version(); },
    )
    or exit 1;

my @months = qw( jan feb mar apr may jun jul aug sep oct nov dec);
my $year = '10';
if ( $options{month} ) {
    print $options{month} . "\n";
    print $months[$options{month}-1] . "\n";
}
if ( $options{year} ) {
    if ($options{year} =~ /^(\d{2})$/) {
        $year = $1;
    }
    elsif ($options{year} =~ /^20(\d{2})$/) {
        $year = $1;
    }
    else { } 
}

if ( $options{first} and $options{num} ) {
    my $i=$options{first};
    my $stop = $i+$options{num};
    while($i != $stop ){
        if ($i < 100) {
            $url = "http://www.microsoft.com/technet/security/Bulletin/MS$year-0$i.mspx";
        }
        else {
            $url = "http://www.microsoft.com/technet/security/Bulletin/MS$year-$i.mspx";
        }
        get();
        $i++;
    }
}
elsif ( $options{url} ) {
    $url = $options{url};
    get();
}
else {
    help();
}
