library/src/exploitation/Utilities/PHP/GadgetGenerators/BP-PHPGadgetGenerators.cc
// linear search through php gadgets to search by string.  String searching
// should be pretty fast.
linear search through php gadgets to search by string.  String searching
should be pretty fast.
linear search through php gadgets to search by string . String searching should be pretty fast .
P_BP_PHP_GADGET BP_PHPGadgetSearchByString
(
		P_BP_PHP_GADGET_LIBRARY gadget_lib,
		char *uuid,
		char *name,
		char *description
)
{
	if(!gadget_lib)
		return NULL;
	if(!gadget_lib->gadget_n)
		return NULL;
	size_t uuid_max_n = 36;
	size_t uuid_len = 0;
	P_BP_PHP_GADGET uuid_match        = NULL;
	P_BP_PHP_GADGET name_match        = NULL;
	P_BP_PHP_GADGET description_match = NULL;
	size_t n = 0;
	if(uuid)
	{
		if(strnlen(uuid, uuid_max_n+1) == uuid_max_n)
		{
			for(n=0; n < gadget_lib->gadget_n; n++)
			{
				if(memcmp(gadget_lib->gadgets[n].uuid, uuid, uuid_max_n) == 0)
				{
					if(!name && !description)
					{
						return &gadget_lib->gadgets[n];
					}
					else
					{
						uuid_match = &gadget_lib->gadgets[n];
						break;
					}
				}
			}
		}
		else
			return NULL;
	}
	if(!uuid_match && uuid)
	{
		return NULL;
	}
	if(name)
	{
		size_t name_len = strnlen(name, 512);
		for(n=0; n < gadget_lib->gadget_n; n++)
		{
			if(bpstrlen(gadget_lib->gadgets[n].name) == name_len)
			{
				printf("\n\n");
				if(memcmp(gadget_lib->gadgets[n].name, name, name_len) == 0)
				{
					if(!description && (&gadget_lib->gadgets[n] == uuid_match))
						return &gadget_lib->gadgets[n];
					else if(!description && !uuid)
						return &gadget_lib->gadgets[n];
					name_match = &gadget_lib->gadgets[n];
				}
			}
		}
	}
	if(uuid_match && name_match)
	if(uuid_match != name_match)
		return NULL;
	if(description)
	{
		size_t description_len = strnlen(description, 1024);
		for(n=0; n < gadget_lib->gadget_n; n++)
		{
			if(bpstrlen(gadget_lib->gadgets[n].description) == description_len)
			{
				if(memcmp(gadget_lib->gadgets[n].description, description, description_len) == 0)
				{
					description_match = &gadget_lib->gadgets[n];
					break;
				}
			}
		}
	}
	if(description_match && name_match && uuid_match)
	if(description_match == name_match)
	if(name_match == uuid_match)
	{
		return uuid_match;
	}
	return NULL;
}
P_BP_PHP_GADGET BP_PHPGadgetSearchByString ( P_BP_PHP_GADGET_LIBRARY gadget_lib , char * uuid , char * name , char * description ) { if ( ! gadget_lib ) return NULL ; if ( ! gadget_lib -> gadget_n ) return NULL ; size_t uuid_max_n = 36 ; size_t uuid_len = 0 ; P_BP_PHP_GADGET uuid_match = NULL ; P_BP_PHP_GADGET name_match = NULL ; P_BP_PHP_GADGET description_match = NULL ; size_t n = 0 ; if ( uuid ) { if ( strnlen ( uuid , uuid_max_n + 1 ) == uuid_max_n ) { for ( n = 0 ; n < gadget_lib -> gadget_n ; n ++ ) { if ( memcmp ( gadget_lib -> gadgets [ n ] . uuid , uuid , uuid_max_n ) == 0 ) { if ( ! name && ! description ) { return & gadget_lib -> gadgets [ n ] ; } else { uuid_match = & gadget_lib -> gadgets [ n ] ; break ; } } } } else return NULL ; } if ( ! uuid_match && uuid ) { return NULL ; } if ( name ) { size_t name_len = strnlen ( name , 512 ) ; for ( n = 0 ; n < gadget_lib -> gadget_n ; n ++ ) { if ( bpstrlen ( gadget_lib -> gadgets [ n ] . name ) == name_len ) { printf ( " \n \n " ) ; if ( memcmp ( gadget_lib -> gadgets [ n ] . name , name , name_len ) == 0 ) { if ( ! description && ( & gadget_lib -> gadgets [ n ] == uuid_match ) ) return & gadget_lib -> gadgets [ n ] ; else if ( ! description && ! uuid ) return & gadget_lib -> gadgets [ n ] ; name_match = & gadget_lib -> gadgets [ n ] ; } } } } if ( uuid_match && name_match ) if ( uuid_match != name_match ) return NULL ; if ( description ) { size_t description_len = strnlen ( description , 1024 ) ; for ( n = 0 ; n < gadget_lib -> gadget_n ; n ++ ) { if ( bpstrlen ( gadget_lib -> gadgets [ n ] . description ) == description_len ) { if ( memcmp ( gadget_lib -> gadgets [ n ] . description , description , description_len ) == 0 ) { description_match = & gadget_lib -> gadgets [ n ] ; break ; } } } } if ( description_match && name_match && uuid_match ) if ( description_match == name_match ) if ( name_match == uuid_match ) { return uuid_match ; } return NULL ; }
linear search through php gadgets to search by string.
linear search through php gadgets to search by string .
// ensure we have a gadget library containing gadgets // set uuid max to standard 32bytes + 4 separators // calculate uuid string length // these are set directly below // linear iterator // ================================================================= // search for uuid first // ensure that the uuid supplied is the correct length, if not exit // the if logic. // run uuid comparison, return value if we have a match // if we did not find a match, and we were supposed to, exit here. // ===================================================== // name search // search for uuid first // calculate name length // walk gadgets // do numeric length check before doing actual compare // run compare // check return conditions and return the gadget if either // check succeds. // if the return checks fail, set value and continue on to // description matching. // Check match pointers and exit if both are set. // ===================================================== // description search // calculate description length // do numeric length check before doing actual compare // run comparison // see if everything matches up return immediately // return null if we are unable to find the gadget we searched for