RBOOL hbs_markAsRelated ( rSequence parent, rSequence toMark ) { RBOOL isSuccess = FALSE; RPCHAR invId = NULL; if( rpal_memory_isValid( parent ) && rpal_memory_isValid( toMark ) ) { isSuccess = TRUE; if( rSequence_getSTRINGA( parent, RP_TAGS_HBS_INVESTIGATION_ID, &invId ) ) { isSuccess = FALSE; if( rSequence_addSTRINGA( toMark, RP_TAGS_HBS_INVESTIGATION_ID, invId ) ) { isSuccess = TRUE; } } } return isSuccess; }
RPVOID rpal_blob_arrElem ( rBlob blob, RU32 elemSize, RU32 elemIndex ) { RPVOID pElem = NULL; _prBlob b = (_prBlob)blob; RU32 offset = 0; if( rpal_memory_isValid( b ) ) { offset = elemSize * elemIndex; if( rpal_memory_isValid( b->pData ) && IS_WITHIN_BOUNDS( b->pData + ( offset ), elemSize, b->pData, b->sizeUsed ) ) { pElem = b->pData + offset; } } return pElem; }
//============================================================================= // rRefCount API //============================================================================= rRefCount rRefCount_create ( rRefCount_freeFunc freeFunc, RPVOID pElem, RU32 elemSize ) { _rRefCount* ref = NULL; ref = rpal_memory_alloc( sizeof( *ref ) ); if( rpal_memory_isValid( ref ) ) { ref->mutex = rMutex_create(); if( rpal_memory_isValid( ref->mutex ) ) { ref->count = 1; ref->freeFunc = freeFunc; ref->pElem = pElem; ref->elemSize = elemSize; } else { rpal_memory_free( ref ); ref = NULL; } } return (rRefCount)ref; }
RBOOL rpal_blob_remove ( rBlob blob, RU32 startOffset, RU32 size ) { RBOOL isSuccess = FALSE; _prBlob pBlob = (_prBlob)blob; if( NULL != pBlob && IS_WITHIN_BOUNDS( pBlob->pData + startOffset, size, pBlob->pData, pBlob->sizeUsed ) && rpal_memory_isValid( pBlob->pData ) ) { rpal_memory_memcpy( pBlob->pData + startOffset, pBlob->pData + startOffset + size, pBlob->sizeUsed - size - startOffset ); pBlob->sizeUsed -= size; if( rpal_memory_isValid( pBlob->pData ) ) { pBlob->pData = rpal_memory_realloc_from( pBlob->pData, pBlob->sizeUsed + sizeof( RWCHAR ), pBlob->from ); pBlob->currentSize = pBlob->sizeUsed; if( NULL != pBlob->pData ) { // We allocate WCHAR more than we need always to ensure that if the buffer contains a wide string it will be terminated rpal_memory_zero( pBlob->pData + pBlob->sizeUsed, sizeof( RWCHAR ) ); isSuccess = TRUE; } } } return isSuccess; }
//============================================================================= // rRwLock API //============================================================================= rRwLock rRwLock_create ( ) { _rRwLock* lock; lock = rpal_memory_alloc( sizeof( *lock ) ); if( rpal_memory_isValid( lock ) ) { lock->evtCanRead = rEvent_create( TRUE ); lock->evtCanWrite = rEvent_create( TRUE ); lock->stateLock = rMutex_create(); lock->readCount = 0; if( !rpal_memory_isValid( lock->evtCanRead ) || !rpal_memory_isValid( lock->evtCanWrite ) || !rpal_memory_isValid( lock->stateLock ) ) { rEvent_free( lock->evtCanRead ); rEvent_free( lock->evtCanWrite ); rMutex_free( lock->stateLock ); rpal_memory_free( lock ); lock = NULL; } else { rEvent_set( lock->evtCanRead ); } } return (rRwLock)lock; }
RBOOL rpal_blob_insert ( rBlob blob, RPVOID pData, RU32 size, RU32 offset ) { RBOOL isAdded = FALSE; _prBlob pBlob = (_prBlob)blob; if( rpal_memory_isValid( blob ) && NULL != pData && 0 != size ) { if( pBlob->currentSize - pBlob->sizeUsed < size ) { pBlob->currentSize += ( 0 != pBlob->growBy ? pBlob->growBy : size ); pBlob->pData = rpal_memory_realloc_from( pBlob->pData, pBlob->currentSize + sizeof( RWCHAR ), pBlob->from ); } if( rpal_memory_isValid( pBlob->pData ) ) { // Relocate existing data rpal_memory_memmove( pBlob->pData + offset + size, pBlob->pData + offset, pBlob->sizeUsed - offset ); rpal_memory_memcpy( pBlob->pData + offset, pData, size ); pBlob->sizeUsed += size; // We allocate WCHAR more than we need always to ensure that if the buffer contains a wide string it will be terminated rpal_memory_zero( pBlob->pData + pBlob->sizeUsed, sizeof( RWCHAR ) ); isAdded = TRUE; } } else if( NULL == pData || 0 == size ) { isAdded = TRUE; } if( isAdded ) { if( !rpal_memory_isValid( pBlob->pData ) ) { isAdded = FALSE; rpal_debug_break(); } } return isAdded; }
RBOOL rpal_blob_add ( rBlob blob, RPVOID pData, RU32 size ) { RBOOL isAdded = FALSE; if( rpal_memory_isValid( blob ) && NULL != pData && 0 != size ) { if( ((_prBlob)blob)->currentSize - ((_prBlob)blob)->sizeUsed < size ) { ((_prBlob)blob)->currentSize += ( ( 0 != ((_prBlob)blob)->growBy && size < ((_prBlob)blob)->growBy ) ? ((_prBlob)blob)->growBy : size ); ((_prBlob)blob)->pData = rpal_memory_realloc_from( ((_prBlob)blob)->pData, ((_prBlob)blob)->currentSize + sizeof( RWCHAR ), ((_prBlob)blob)->from ); } if( rpal_memory_isValid( ((_prBlob)blob)->pData ) ) { rpal_memory_memcpy( ((_prBlob)blob)->pData + ((_prBlob)blob)->sizeUsed, pData, size ); ((_prBlob)blob)->sizeUsed += size; // We allocate WCHAR more than we need always to ensure that if the buffer contains a wide string it will be terminated rpal_memory_zero( ((_prBlob)blob)->pData + ((_prBlob)blob)->sizeUsed, sizeof( RWCHAR ) ); isAdded = TRUE; } } else if( NULL == pData || 0 == size ) { isAdded = TRUE; } if( isAdded ) { if( !rpal_memory_isValid( ((_prBlob)blob)->pData ) ) { isAdded = FALSE; rpal_debug_break(); } } return isAdded; }
rBlob rpal_blob_create_from ( RU32 initialSize, RU32 growBy, RU32 from ) { rBlob blob = NULL; blob = rpal_memory_alloc_from( sizeof( _rBlob ), from ); if( rpal_memory_isValid( blob ) ) { if( 0 != initialSize ) { ((_prBlob)blob)->pData = rpal_memory_alloc_from( initialSize + sizeof( RWCHAR ), from ); if( !rpal_memory_isValid( ((_prBlob)blob)->pData ) ) { rpal_memory_free( blob ); blob = NULL; } else { ((_prBlob)blob)->currentSize = initialSize; (((_prBlob)blob)->pData)[ initialSize ] = 0; (((_prBlob)blob)->pData)[ initialSize + 1 ] = 0; } } else { ((_prBlob)blob)->currentSize = 0; ((_prBlob)blob)->pData = NULL; } if( NULL != blob ) { ((_prBlob)blob)->sizeUsed = 0; ((_prBlob)blob)->growBy = growBy; ((_prBlob)blob)->from = from; } } else { blob = NULL; } return blob; }
static RVOID processGenericSnapshot ( rpcm_tag notifType, rSequence event ) { rList entityList = NULL; rSequence entity = NULL; UNREFERENCED_PARAMETER( notifType ); if( rpal_memory_isValid( event ) ) { if( rSequence_getLIST( event, RP_TAGS_AUTORUNS, &entityList ) || rSequence_getLIST( event, RP_TAGS_SVCS, &entityList ) || rSequence_getLIST( event, RP_TAGS_PROCESSES, &entityList ) ) { // Go through the elements, whatever tag while( rList_getSEQUENCE( entityList, RPCM_INVALID_TAG, &entity ) ) { processHashedEvent( notifType, entity ); } } } }
static RPVOID lookForHiddenModules ( rEvent isTimeToStop, RPVOID ctx ) { processLibProcEntry* procs = NULL; processLibProcEntry* proc = NULL; UNREFERENCED_PARAMETER( ctx ); if( NULL != ( procs = processLib_getProcessEntries( TRUE ) ) ) { proc = procs; while( 0 != proc->pid && rpal_memory_isValid( isTimeToStop ) && !rEvent_wait( isTimeToStop, 0 ) ) { lookForHiddenModulesIn( isTimeToStop, proc->pid ); proc++; } rpal_memory_free( procs ); } return NULL; }
static RBOOL findDoc ( rSequence doc, DocSearchContext* ctx ) { RBOOL isMatch = FALSE; RPCHAR filePathA = NULL; RPWCHAR filePathW = NULL; CryptoLib_Hash* pHash = NULL; RU32 hashSize = 0; if( rpal_memory_isValid( doc ) && NULL != ctx ) { rSequence_getSTRINGA( doc, RP_TAGS_FILE_PATH, &filePathA ); rSequence_getSTRINGW( doc, RP_TAGS_FILE_PATH, &filePathW ); rSequence_getBUFFER( doc, RP_TAGS_HASH, (RPU8*)&pHash, &hashSize ); if( ( NULL == filePathA || NULL == ctx->exprA || rpal_string_match( ctx->exprA, filePathA, FALSE ) ) && ( NULL == filePathW || NULL == ctx->exprW || rpal_string_matchw( ctx->exprW, filePathW, FALSE ) ) && ( NULL == pHash || NULL == ctx->pHash || 0 == rpal_memory_memcmp( pHash, ctx->pHash, hashSize ) ) ) { isMatch = TRUE; } } return isMatch; }
//============================================================================= // rEvent API //============================================================================= rEvent rEvent_create ( RBOOL isManualReset ) { _rEvent* evt = NULL; evt = rpal_memory_alloc( sizeof( *evt ) ); if( rpal_memory_isValid( evt ) ) { #ifdef RPAL_PLATFORM_WINDOWS evt->waitCount = 0; evt->hEvent = CreateEvent( NULL, isManualReset, FALSE, NULL ); if( NULL == evt->hEvent ) { rpal_memory_free( evt ); evt = NULL; } #elif defined( RPAL_PLATFORM_LINUX ) || defined( RPAL_PLATFORM_MACOSX ) pthread_mutex_init( &evt->hMutex, NULL ); pthread_cond_init( &evt->hCond, NULL ); evt->isManualReset = isManualReset; evt->isOn = FALSE; #endif } return (rEvent)evt; }
RVOID rpal_blob_free ( rBlob blob ) { if( rpal_memory_isValid( blob ) ) { if( rpal_memory_isValid( ((_prBlob)blob)->pData ) ) { rpal_memory_free( ((_prBlob)blob)->pData ); } rpal_memory_free( blob ); } }
RBOOL rRwLock_read_unlock ( rRwLock lock ) { RBOOL isSuccess = FALSE; _rRwLock* lck = (_rRwLock*)lock; if( rpal_memory_isValid( lock ) ) { if( rMutex_lock( lck->stateLock ) ) { lck->readCount--; if( 0 == lck->readCount ) { rEvent_set( lck->evtCanWrite ); } rMutex_unlock( lck->stateLock ); isSuccess = TRUE; } } return isSuccess; }
//============================================================================= // rMutex API //============================================================================= rMutex rMutex_create ( ) { rMutex mutex = NULL; mutex = rpal_memory_alloc( sizeof( _rMutex ) ); if( rpal_memory_isValid( mutex ) ) { #ifdef RPAL_PLATFORM_WINDOWS ((_rMutex*)mutex)->hMutex = CreateMutex( NULL, FALSE, NULL ); if( NULL == ((_rMutex*)mutex)->hMutex ) { rpal_memory_free( mutex ); mutex = NULL; } #elif defined( RPAL_PLATFORM_LINUX ) || defined( RPAL_PLATFORM_MACOSX ) pthread_mutex_init( &((_rMutex*)mutex)->hMutex, NULL ); #endif } return mutex; }
//============================================================================= // Public API //============================================================================= HObs obsLib_new ( RU32 nMaxMem, ObsType searchType ) { _PHObs obs = NULL; obs = rpal_memory_alloc( sizeof( _HObs ) ); if( rpal_memory_isValid( obs ) ) { obs->root = newNode(); if( NULL != obs->root ) { obs->currentOffset = 0; obs->maxDepth = 0; obs->maxMem = nMaxMem; obs->searchType = searchType; obs->targetBuffer = NULL; obs->currentState = NULL; obs->curHits = NULL; obs->curDepth = 0; obs->nPatterns = 0; } else { rpal_memory_free( obs ); obs = NULL; } } return (HObs)obs; }
RBOOL rEvent_unset ( rEvent ev ) { RBOOL isSuccess = FALSE; _rEvent* evt = (_rEvent*)ev; if( rpal_memory_isValid( ev ) ) { #ifdef RPAL_PLATFORM_WINDOWS isSuccess = ResetEvent( evt->hEvent ); #elif defined( RPAL_PLATFORM_LINUX ) || defined( RPAL_PLATFORM_MACOSX ) if( 0 == pthread_mutex_lock( &evt->hMutex ) ) { evt->isOn = FALSE; pthread_mutex_unlock( &evt->hMutex ); isSuccess = TRUE; } #endif } return isSuccess; }
RBOOL rEvent_set ( rEvent ev ) { RBOOL isSuccess = FALSE; _rEvent* evt = (_rEvent*)ev; if( rpal_memory_isValid( ev ) ) { #ifdef RPAL_PLATFORM_WINDOWS isSuccess = SetEvent( evt->hEvent ); #elif defined( RPAL_PLATFORM_LINUX ) || defined( RPAL_PLATFORM_MACOSX ) if( 0 == pthread_mutex_lock( &evt->hMutex ) ) { evt->isOn = TRUE; if( evt->isManualReset ) { pthread_cond_broadcast( &evt->hCond ); } else { pthread_cond_signal( &evt->hCond ); } pthread_mutex_unlock( &evt->hMutex ); isSuccess = TRUE; } #endif } return isSuccess; }
RPVOID rpal_blob_getBuffer ( rBlob blob ) { RPVOID pData = NULL; if( rpal_memory_isValid( blob ) && rpal_memory_isValid( ((_prBlob)blob)->pData ) ) { pData = ((_prBlob)blob)->pData; } return pData; }
static RPVOID lookForHiddenModulesConstantly ( rEvent isTimeToStop, RPVOID ctx ) { rSequence originalRequest = (rSequence)ctx; processLibProcEntry* procs = NULL; processLibProcEntry* proc = NULL; LibOsPerformanceProfile perfProfile = { 0 }; perfProfile.enforceOnceIn = 4; perfProfile.sanityCeiling = MSEC_FROM_SEC( 20 ); perfProfile.lastTimeoutValue = 200; perfProfile.targetCpuPerformance = 0; perfProfile.globalTargetCpuPerformance = GLOBAL_CPU_USAGE_TARGET; perfProfile.timeoutIncrementPerSec = 50; while( rpal_memory_isValid( isTimeToStop ) && !rEvent_wait( isTimeToStop, 0 ) ) { if( NULL != ( procs = processLib_getProcessEntries( TRUE ) ) ) { proc = procs; while( 0 != proc->pid && rpal_memory_isValid( isTimeToStop ) && !rEvent_wait( isTimeToStop, _TIMEOUT_BETWEEN_CONSTANT_PROCESSS ) ) { if( hbs_whenCpuBelow( _CPU_WATERMARK, _MAX_CPU_WAIT, isTimeToStop ) ) { lookForHiddenModulesIn( isTimeToStop, proc->pid, originalRequest, &perfProfile ); } proc++; } rpal_memory_free( procs ); } } return NULL; }
RBOOL addHitToNode ( PObsNode node, PObsSig sig ) { RBOOL isSuccess = FALSE; PObsSig* tmp = NULL; RU32 numHits = 0; if( rpal_memory_isValid( node ) && rpal_memory_isValid( sig ) ) { tmp = node->pSigsHit; if( NULL != tmp ) { while( NULL != *tmp ) { numHits++; tmp++; } } node->pSigsHit = rpal_memory_realloc( node->pSigsHit, ( ( numHits + 2 ) * sizeof( PObsSig ) ) ); tmp = node->pSigsHit; if( NULL != tmp ) { rpal_memory_zero( (RPU8)tmp + ( numHits * sizeof( PObsSig ) ), 2 * sizeof( PObsSig ) ); while( NULL != *tmp ) { tmp++; } *tmp = sig; isSuccess = TRUE; } } return isSuccess; }
void test_CreateAndDestroy(void) { HObs hObs = NULL; hObs = obsLib_new( 0, 0 ); CU_ASSERT_TRUE_FATAL( rpal_memory_isValid( hObs ) ); obsLib_free( hObs ); }
static RBOOL notifyOfProcess ( RU32 pid, RU32 ppid, RBOOL isStarting ) { RBOOL isSuccess = FALSE; rSequence info = NULL; rSequence parentInfo = NULL; if( !isStarting || NULL == ( info = processLib_getProcessInfo( pid ) ) ) { info = rSequence_new(); } if( rpal_memory_isValid( info ) ) { rSequence_addRU32( info, RP_TAGS_PROCESS_ID, pid ); rSequence_addRU32( info, RP_TAGS_PARENT_PROCESS_ID, ppid ); rSequence_addTIMESTAMP( info, RP_TAGS_TIMESTAMP, rpal_time_getGlobal() ); if( isStarting ) { if( NULL != ( parentInfo = processLib_getProcessInfo( ppid ) ) && !rSequence_addSEQUENCE( info, RP_TAGS_PARENT, parentInfo ) ) { rSequence_free( parentInfo ); } } if( isStarting ) { if( notifications_publish( RP_TAGS_NOTIFICATION_NEW_PROCESS, info ) ) { isSuccess = TRUE; rpal_debug_info( "new process starting: %d", pid ); } } else { if( notifications_publish( RP_TAGS_NOTIFICATION_TERMINATE_PROCESS, info ) ) { isSuccess = TRUE; rpal_debug_info( "new process terminating: %d", pid ); } } rSequence_free( info ); } return isSuccess; }
//============================================================================= // Internal Routines //============================================================================= RVOID freeSig ( PObsSig sig ) { if( rpal_memory_isValid( sig ) ) { rpal_memory_free( sig ); } }
RVOID rpal_stringbuffer_freeWrapper ( rString pStringBuffer ) { if( rpal_memory_isValid( pStringBuffer ) ) { rpal_blob_freeWrapperOnly( ( (_rString*)pStringBuffer )->blob ); rpal_memory_free( pStringBuffer ); } }
rBlob rpal_blob_duplicate_from ( rBlob original, RU32 from ) { _prBlob newBlob = NULL; _prBlob originalBlob = (_prBlob)original; if( NULL != original ) { if( NULL != ( newBlob = rpal_memory_alloc_from( sizeof( _rBlob ), from ) ) ) { newBlob->currentSize = originalBlob->currentSize; newBlob->growBy = originalBlob->growBy; newBlob->sizeUsed = originalBlob->sizeUsed; newBlob->from = from; if( 0 != originalBlob->currentSize && NULL != originalBlob->pData ) { newBlob->pData = rpal_memory_alloc_from( newBlob->currentSize + sizeof( RWCHAR ), from ); if( NULL != newBlob->pData ) { rpal_memory_memcpy( newBlob->pData, originalBlob->pData, newBlob->currentSize + sizeof( RWCHAR ) ); if( !rpal_memory_isValid( newBlob->pData ) ) { rpal_memory_free( newBlob ); newBlob = NULL; rpal_debug_break(); } } else { rpal_memory_free( newBlob ); newBlob = NULL; rpal_debug_break(); } } else { newBlob->pData = NULL; } } } return newBlob; }
RPCHAR liburl_encode ( RPCHAR str ) { RPCHAR encoded = NULL; RU32 index = 0; RU32 numToEncode = 0; RU32 len = 0; RCHAR escaped[ 3 ] = {0}; if( NULL != str ) { len = rpal_string_strlen( str ); for( index = 0; index < len; index++ ) { if( !isValidUrlChar( *(str + index) ) ) { numToEncode++; } } encoded = rpal_memory_alloc( len + sizeof(RCHAR) + numToEncode ); numToEncode = 0; if( rpal_memory_isValid( encoded ) ) { for( index = 0; index < len; index++ ) { if( !isValidUrlChar( *(str + index) ) ) { sprintf( (RPCHAR)&escaped, "%02X", *(str + index) ); *(encoded + index + numToEncode) = escaped[ 0 ]; numToEncode++; *(encoded + index + numToEncode) = escaped[ 1 ]; } else { *(encoded + index + numToEncode) = *(str + index); } } } } return encoded; }
void test_addPattern(void) { HObs hObs = NULL; RU8 pattern[] = { 0x01, 0x02, 0x03, 0x04 }; RU32 context = 0; hObs = obsLib_new( 0, 0 ); CU_ASSERT_TRUE_FATAL( rpal_memory_isValid( hObs ) ); CU_ASSERT_TRUE_FATAL( obsLib_addPattern( hObs, (RPU8)&pattern, sizeof( pattern ), &context ) ); obsLib_free( hObs ); }
static RBOOL assembleRanges ( rList mods, _MemRange** pRanges, RU32* pNRanges ) { RBOOL isSuccess = FALSE; _MemRange* memRanges = NULL; rSequence mod = NULL; RU64 base = 0; RU64 size = 0; RU32 i = 0; if( rpal_memory_isValid( mods ) && NULL != pRanges && NULL != pNRanges ) { if( NULL != ( memRanges = rpal_memory_alloc( sizeof( _MemRange ) * rList_getNumElements( mods ) ) ) ) { rList_resetIterator( mods ); while( rList_getSEQUENCE( mods, RP_TAGS_DLL, &mod ) ) { if( rSequence_getPOINTER64( mod, RP_TAGS_BASE_ADDRESS, &base ) && rSequence_getRU64( mod, RP_TAGS_MEMORY_SIZE, &size ) ) { memRanges[ i ].base = base; memRanges[ i ].size = size; i++; } } if( rpal_sort_array( memRanges, i, sizeof( _MemRange ), (rpal_ordering_func)rpal_order_RU64 ) ) { isSuccess = TRUE; *pRanges = memRanges; *pNRanges = i; } } } return isSuccess; }
static RBOOL updateCollectorConfigs ( rList newConfigs ) { RBOOL isSuccess = FALSE; RU8 unused = 0; RU32 i = 0; rSequence tmpConf = NULL; RU32 confId = 0; if( rpal_memory_isValid( newConfigs ) ) { rpal_debug_info( "updating collector configurations." ); for( i = 0; i < ARRAY_N_ELEM( g_collectors ); i++ ) { if( NULL != g_collectors[ i ].conf ) { rpal_debug_info( "freeing collector %d config.", i ); rSequence_free( g_collectors[ i ].conf ); g_collectors[ i ].conf = NULL; } } while( rList_getSEQUENCE( newConfigs, RP_TAGS_HBS_CONFIGURATION, &tmpConf ) ) { if( rSequence_getRU32( tmpConf, RP_TAGS_HBS_CONFIGURATION_ID, &confId ) && confId < ARRAY_N_ELEM( g_collectors ) ) { if( rSequence_getRU8( tmpConf, RP_TAGS_IS_DISABLED, &unused ) ) { g_collectors[ confId ].isEnabled = FALSE; } else { g_collectors[ confId ].isEnabled = TRUE; g_collectors[ confId ].conf = rSequence_duplicate( tmpConf ); rpal_debug_info( "set new collector %d config.", confId ); } } } isSuccess = TRUE; } return isSuccess; }