###############################################################################
# OpenVAS Vulnerability Test
# $Id$
#
# Drupal Detection
#
# Authors:
# Michael Meyer
#
# Copyright:
# Copyright (c) 2009 Greenbone Networks GmbH
#
# Modified by: Nikita MR <rnikita@secpod.com> On 13th July 2009
# Note: Upgrade to detect for Multiple KB setting
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# (or any later version), as published by the Free Software Foundation.
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
###############################################################################

# need desc here to modify it later in script.
desc = "
  Overview:
  This host is running Drupal, an open source content management platform.

  See also:
  http://drupal.org/

  Risk factor : None";

if(description)
{
  script_id(100169);
  script_version ("1.1");
 script_tag(name:"risk_factor", value:"None");
  script_name("Drupal Detection");
  script_description(desc);
  script_summary("Checks for the presence of Drupal");
  script_category(ACT_GATHER_INFO);
  script_family("General");
  script_copyright("This script is Copyright (C) 2009 Greenbone Networks GmbH");
  script_dependencie("find_service.nes", "http_version.nasl");
  script_require_ports("Services/www", 80);
  script_exclude_keys("Settings/disable_cgi_scanning");
  exit(0);
}

include("http_func.inc");
include("http_keepalive.inc");
include("global_settings.inc");

port = get_http_port(default:80);

if(!get_port_state(port))exit(0);
if(!can_host_php(port:port))exit(0);

dirs = make_list("/drupal","/cms",cgi_dirs());

foreach dir (dirs)
{
  url = string(dir, "/update.php");
  req = http_get(item:url, port:port);
  buf = http_keepalive_send_recv(port:port, data:req, bodyonly:FALSE);
  if(buf == NULL)continue;

  if(egrep(pattern:"Location: .*update\.php\?op=info", string:buf, icase:TRUE)||
    (egrep(pattern:"Access denied", string:buf, icase:TRUE) &&
     egrep(pattern:"drupal", string:buf, icase:TRUE)        &&
     egrep(pattern:"\$access_check", string:buf, icase:TRUE)))
  {
    if(strlen(dir)>0){
       install=dir;
    }
    else{
      install=string("/");
    }

    vers = string("unknown");

    ### try to get version.
    url = string(dir, "/CHANGELOG.txt");
    req = http_get(item:url, port:port);
    buf = http_keepalive_send_recv(port:port, data:req, bodyonly:TRUE);

    version = eregmatch(pattern:'Drupal ([0-9.]+), [0-9]{4}-[0-9]{2}-[0-9]{2}',
                        string:buf, icase:TRUE);

    if(!isnull(version[1])){
      vers=chomp(version[1]);
    }

    set_kb_item(name:string("www/", port, "/drupal"),
                value:string(vers," under ",install));

    info = string("None\n\nDrupal Version '");
    info += string(vers);
    info += string("' was detected on the remote host in the following directory(s):\n\n");
    info += string(install, "\n");

    desc = ereg_replace(
        string:desc,
        pattern:"None$",
        replace:info
    );

    if(report_verbosity > 0){
      security_note(port:port,data:desc);
    }
  }
}
exit(0);
