#VERSION,2.00
# $Id: nikto_cookies.plugin 632 2011-02-19 02:49:31Z sullo $
###############################################################################
#  Copyright (C) 2010 CIRT, Inc.
#
#  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; version 2
#  of the License only.
#
#  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.
###############################################################################
# PURPOSE:
# General HTTP cookie checks
###############################################################################
sub nikto_cookies_init {
    my $id = {
         name        => "cookies",
         full_name   => "HTTP Cookie Internal IP",
         author      => "Sullo",
         description => "Looks for internal IP addresses in cookies returned from an HTTP request.",
         hooks       => { postfetch => { method => \&nikto_cookies_postfetch, }, },
         copyright   => "2010 CIRT Inc."
         };

    # if we need once per cookie
    #use vars qw/%CFOUND/;

    return $id;
}

sub nikto_cookies_postfetch {
    my ($mark, $parameters, $request, $result) = @_;

    if (!exists $result->{'set-cookie'}) {
        return $request, $result;
    }

    # if we need to limit to once per cookie...
    #my $key =
    #  LW2::md4(  $mark->{'ip'}
    #           . $mark->{'hostname'}
    #           . $mark->{'port'}
    #           . $mark->{'ssl'}
    #           . $result->{'whisker'}->{'method'}
    #           . $c);
    #if (!exists $CFOUND{$key}) {

    foreach my $c (@{ $result->{'whisker'}->{'cookies'} }) {
        if ($c =~ /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {
            my $ip = $1;
            if ($ip ne $mark->{'ip'}) {
                my $msg   = "";
                my $osvdb = 0;
                $c =~ /^([^=]+)/;
                my $cname = $1;

                if ($cname eq 'ARPT') {
                    $msg =
                      "Cisco content switch reveals internal IP address found in the '$cname' cookie. The IP is \"$ip\".";
                    $osvdb = 28121;
                }
                else {

                    # is it an internal, or just different?
                    my $int = "";
                    if ($ip =~ /^(10|172\.(1[6-9]|2[0-9]|3[01])|192\.168)\./) {
                        $int = "Internal ";
                    }

                    $msg = $int . "IP address found in the '$cname' cookie. The IP is \"$ip\".";
                }
                add_vulnerability($mark, $msg, 999991, $osvdb,);
            }
        }

        #$CFOUND{$key} = $c;

    }

    #}
}

1;
