library/src/blockclass/subclasses/BPSC_SSHIntegrityChecker.cc
// check integrity of directory, store in db (requires mysql connection), prompt is for expect.
check integrity of directory, store in db (requires mysql connection), prompt is for expect.
check integrity of directory store in db ( requires mysql connection ) prompt is for expect .
BP_ERROR_T BPSC_SSHIntegrityChecker::checkMD5(char *remote_directory_root_path, char *prompt) {
	if (!this->mysqlSession) {
		this->AddErrorToStack("Error in BPSC_SSHIntegrityChecker::connectSSH: Cannot connect msyql and mysql unset.");
		return ERR_FAILURE;
	}
	if (!remote_directory_root_path) {
		this->AddErrorToStack("Error in BPSC_SSHIntegrityChecker::connectSSH(): no directory path set.");
		return ERR_FAILURE;
	}
	if (!this->cc_session) {
		this->AddErrorToStack("Error in BPSC_SSHIntegrityChecker::connectSSH(): no command and control session.");
		return ERR_FAILURE;
	}
	char dir_command[4096] = { 0 };
	snprintf((char *) &dir_command, sizeof(dir_command) - 1, this->md5_command, remote_directory_root_path);
	P_BP_CC_DECISION_TREE md5_tree = this->ccCommandExists("match_md5");
	if (md5_tree) {
		md5_tree->top_branch->try_this = (char *) &dir_command;
		md5_tree->top_branch->try_buff_size = bpstrlen(dir_command);
	} else 
	{
		this->ccAddTryExcept("match_md5", (char *) &dir_command, bpstrlen(dir_command), prompt, bpstrlen(prompt));
	}
	this->ccExecuteCommand("match_md5", NULL);
	char read_line[2048] = { 0 };
	char md5_buff[33] = { 0 };
	char timestamp_buff[64] = { 0 };
	size_t i = 0;
	size_t x = 0;
	size_t j = 0;
	size_t buff_length = bpstrlen(this->cc_session->last_matched_buffer);
	for (i = 0; i < buff_length; i += x) {
		if (this->cc_session->last_matched_buffer[i] == 0x00)
			break;
		for (x = 0; this->cc_session->last_matched_buffer[i + x] != 0x0a && this->cc_session->last_matched_buffer[i + x] != 0x00; x++) {
		}
		if (x > 32) {
			memset((char *) &read_line, 0x00, sizeof(read_line));
			memset((char *) &md5_buff, 0x00, sizeof(md5_buff));
			memset((char *) &timestamp_buff, 0x00, sizeof(timestamp_buff));
			snprintf((char *) &md5_buff, 33, "%s", (char *) &this->cc_session->last_matched_buffer[i]);
			for (j = 0; j < 33; j++) {
				if (md5_buff[j] != 'a' && md5_buff[j] != 'A' && md5_buff[j] != 'b' && md5_buff[j] != 'B' && md5_buff[j] != 'c' && md5_buff[j] != 'C'
						&& md5_buff[j] != 'd' && md5_buff[j] != 'D' && md5_buff[j] != 'e' && md5_buff[j] != 'E' && md5_buff[j] != 'f' && md5_buff[j] != 'F'
						&& md5_buff[j] != 'f' && md5_buff[j] != 'F' && md5_buff[j] != '0' && md5_buff[j] != '1' && md5_buff[j] != '2' && md5_buff[j] != '3'
						&& md5_buff[j] != '4' && md5_buff[j] != '5' && md5_buff[j] != '6' && md5_buff[j] != '7' && md5_buff[j] != '8' && md5_buff[j] != '9') {
					break;
				}
			}
			if (j == 32) {
				for (j = 0; this->cc_session->last_matched_buffer[i + 32 + j] == 0x20; j++) {
				}
				snprintf((char *) &read_line, x - (32 + j), "%s", (char *) &this->cc_session->last_matched_buffer[i + 32 + j]);
				snprintf((char *) &timestamp_buff, sizeof(timestamp_buff) - 1, "%u", time(NULL));
				this->mysqlPrepareQuery("INSERT INTO `ssh_integrity`.`host_info` (`name` ,`time` ,`host` ,`dir` ,`path` ,`md5`) VALUES (?, ?, ?, ?, ?, ?);");
				this->mysqlAddParameter(MYSQL_TYPE_STRING, this->session_name, bpstrlen(this->session_name));
				this->mysqlAddParameter(MYSQL_TYPE_STRING, (char *) &timestamp_buff, bpstrlen((char *) &timestamp_buff));
				this->mysqlAddParameter(MYSQL_TYPE_STRING, this->host_identifier, bpstrlen(this->host_identifier));
				this->mysqlAddParameter(MYSQL_TYPE_STRING, remote_directory_root_path, bpstrlen(remote_directory_root_path));
				this->mysqlAddParameter(MYSQL_TYPE_STRING, (char *) &read_line, bpstrlen((char*) &read_line));
				this->mysqlAddParameter(MYSQL_TYPE_STRING, (char *) &md5_buff, bpstrlen((char*) &md5_buff));
				this->mysqlBindParameters();
				this->mysqlExecuteStatement();
				this->mysqlDestroyStatement();
			}
		}
		x++;
	}
	return ERR_SUCCESS;
}
BP_ERROR_T BPSC_SSHIntegrityChecker :: checkMD5 ( char * remote_directory_root_path , char * prompt ) { if ( ! this -> mysqlSession ) { this -> AddErrorToStack ( " " ) ; return ERR_FAILURE ; } if ( ! remote_directory_root_path ) { this -> AddErrorToStack ( " " ) ; return ERR_FAILURE ; } if ( ! this -> cc_session ) { this -> AddErrorToStack ( " " ) ; return ERR_FAILURE ; } char dir_command [ 4096 ] = { 0 } ; snprintf ( ( char * ) & dir_command , sizeof ( dir_command ) - 1 , this -> md5_command , remote_directory_root_path ) ; P_BP_CC_DECISION_TREE md5_tree = this -> ccCommandExists ( " " ) ; if ( md5_tree ) { md5_tree -> top_branch -> try_this = ( char * ) & dir_command ; md5_tree -> top_branch -> try_buff_size = bpstrlen ( dir_command ) ; } else { this -> ccAddTryExcept ( " " , ( char * ) & dir_command , bpstrlen ( dir_command ) , prompt , bpstrlen ( prompt ) ) ; } this -> ccExecuteCommand ( " " , NULL ) ; char read_line [ 2048 ] = { 0 } ; char md5_buff [ 33 ] = { 0 } ; char timestamp_buff [ 64 ] = { 0 } ; size_t i = 0 ; size_t x = 0 ; size_t j = 0 ; size_t buff_length = bpstrlen ( this -> cc_session -> last_matched_buffer ) ; for ( i = 0 ; i < buff_length ; i += x ) { if ( this -> cc_session -> last_matched_buffer [ i ] == 0x00 ) break ; for ( x = 0 ; this -> cc_session -> last_matched_buffer [ i + x ] != 0x0a && this -> cc_session -> last_matched_buffer [ i + x ] != 0x00 ; x ++ ) { } if ( x > 32 ) { memset ( ( char * ) & read_line , 0x00 , sizeof ( read_line ) ) ; memset ( ( char * ) & md5_buff , 0x00 , sizeof ( md5_buff ) ) ; memset ( ( char * ) & timestamp_buff , 0x00 , sizeof ( timestamp_buff ) ) ; snprintf ( ( char * ) & md5_buff , 33 , " " , ( char * ) & this -> cc_session -> last_matched_buffer [ i ] ) ; for ( j = 0 ; j < 33 ; j ++ ) { if ( md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' && md5_buff [ j ] != ' ' ) { break ; } } if ( j == 32 ) { for ( j = 0 ; this -> cc_session -> last_matched_buffer [ i + 32 + j ] == 0x20 ; j ++ ) { } snprintf ( ( char * ) & read_line , x - ( 32 + j ) , " " , ( char * ) & this -> cc_session -> last_matched_buffer [ i + 32 + j ] ) ; snprintf ( ( char * ) & timestamp_buff , sizeof ( timestamp_buff ) - 1 , " " , time ( NULL ) ) ; this -> mysqlPrepareQuery ( " " ) ; this -> mysqlAddParameter ( MYSQL_TYPE_STRING , this -> session_name , bpstrlen ( this -> session_name ) ) ; this -> mysqlAddParameter ( MYSQL_TYPE_STRING , ( char * ) & timestamp_buff , bpstrlen ( ( char * ) & timestamp_buff ) ) ; this -> mysqlAddParameter ( MYSQL_TYPE_STRING , this -> host_identifier , bpstrlen ( this -> host_identifier ) ) ; this -> mysqlAddParameter ( MYSQL_TYPE_STRING , remote_directory_root_path , bpstrlen ( remote_directory_root_path ) ) ; this -> mysqlAddParameter ( MYSQL_TYPE_STRING , ( char * ) & read_line , bpstrlen ( ( char * ) & read_line ) ) ; this -> mysqlAddParameter ( MYSQL_TYPE_STRING , ( char * ) & md5_buff , bpstrlen ( ( char * ) & md5_buff ) ) ; this -> mysqlBindParameters ( ) ; this -> mysqlExecuteStatement ( ) ; this -> mysqlDestroyStatement ( ) ; } } x ++ ; } return ERR_SUCCESS ; }
check integrity of directory, store in db (requires mysql connection), prompt is for expect.
check integrity of directory store in db ( requires mysql connection ) prompt is for expect .
// check mysql session first // make sure we have a root directory path // make sure we have a command and control session attached // command to run // set command dir // add try/except for md5 generation // if the tree exists, replace arguments // create new // execute the command // -- Process Output ------------ // buffer for reading lines // buffer for holding temporary md5 (+null terminator) // buffer for holding timestamp // byte index // this loop actually walks the result data and extracts the md5/filename // results must be in the format {md5_hash}{n spaces}{path to file}{newline} // exit loop if character is a null terminator // read till newline (x contains bpstrlen after this) // if the length of the string is > 32 we have a potential hash match // zero out buffers // read md5 into md5 var // walk md5 buffer and check to make sure its contents are hexidecimal // check if its not hexideicmal // if we've matched all characters as hexidecimal, then save the remainder of the string as well // remove intermediate space // read line into local copy // store timestamp // --------- Log to DB Here ------------------------------- // prepare a query (statement) // `name` ,`time` ,`host` ,`dir` ,`path` ,`md5` // execute the statement // destroy the statement / result / parameters // moves past the newline in the string