@@ -13,6 +13,28 @@ const unsigned char popmask[] = "\xBD\xE8";
1313const unsigned char bxlrmask[] = " \x1E\xFF\x2F\xE1 " ;
1414const unsigned char parammask[] = " \xCC\xCC\xCC\xCC " ;
1515
16+ const char * pattern_scan (const char * pattern, const char * mask, const char * data, size_t data_len) {
17+ size_t mask_len = strlen (mask) - 1 ; // Subtract 1 to exclude the null terminator
18+
19+ for (size_t i = 0 ; i <= data_len - mask_len; i++) {
20+ int match = 1 ;
21+ for (size_t j = 0 ; j < mask_len; j++) {
22+ if (mask[j] == ' ?' ) {
23+ continue ;
24+ }
25+ if (data[i + j] != pattern[j]) {
26+ match = 0 ;
27+ break ;
28+ }
29+ }
30+ if (match) {
31+ return &data[i]; // Return the address where the pattern is found
32+ }
33+ }
34+
35+ return nullptr ; // Pattern not found
36+ }
37+
1638#define GetBit (buff, pos ) (((buff) & (0x1 << (pos)))) ? true : false
1739
1840/* *
@@ -178,7 +200,7 @@ uintptr_t ProcessManager::FindDMAddy(uintptr_t base, std::vector<uintptr_t> offs
178200
179201 for (int i = 0 ; i < offsets.size (); i++)
180202 {
181- result = ReadProcessMemory <uintptr_t >(result);
203+ result = ReadMemoryWrapper <uintptr_t >(result);
182204 result += offsets[i];
183205 }
184206
@@ -193,13 +215,18 @@ uintptr_t ProcessManager::GetLocalModBaseAddr(const char* modName)
193215
194216 int localMapsfd = open (" /proc/self/maps" , O_RDONLY);
195217
218+ if (localMapsfd < 0 )
219+ return 0 ;
220+
196221 SegmentInfo tSegment;
197222 ZeroMemory (&tSegment, sizeof (tSegment));
198223
199- if (!localMapsfd)
224+ if (GetLineSegmentFromName (localMapsfd, modName, tSegment) == false )
225+ {
226+ close (localMapsfd);
200227 return 0 ;
228+ }
201229
202- GetLineSegmentFromName (localMapsfd, modName, tSegment);
203230 close (localMapsfd);
204231
205232 return tSegment.start ;
@@ -244,59 +271,52 @@ bool ProcessManager::FindExternalSymbol(const char* modName, const char* symbolN
244271 return symbolFound;
245272}
246273
247- void ProcessManager::memcpy ( unsigned char * source, uintptr_t destination, int size)
274+ bool ProcessManager::WriteMemory ( const void * source, uintptr_t destination, int size)
248275{
249- for (int i = 0 ; i < size; i++)
250- WriteProcessMemory (destination + i, source[i]);
276+ if (lseek64 (memfd, destination, SEEK_SET) < 0 )
277+ return false ;
278+
279+ write (memfd, source, size);
280+
281+ return true ;
251282}
252283
253- bool ProcessManager::memcpyBackwrd (uintptr_t source, unsigned char * destination, int size)
284+ bool ProcessManager::ReadMemory (uintptr_t source, void * destination, int size)
254285{
255- for (int i = 0 ; i < size; i++)
256- destination[i] = ReadProcessMemory<unsigned char >(source + i);
286+ if (lseek64 (memfd, source, SEEK_SET) < 0 )
287+ return false ;
288+
289+ read (memfd, destination, size);
257290
258291 return true ;
259292}
260293
261- bool ProcessManager::EnumSegments (std::vector<SegmentInfo> & segments, int prot )
294+ bool ProcessManager::EnumSegments (std::vector<SegmentInfo> & segments, int protection )
262295{
263296 #ifdef DEBUG
264297 printf (" ProcessManager::EnumSegments(&)\n " );
265298 #endif
266299
267- char mask[5 ];
268-
269- switch (prot)
270- {
271- case READ:
272- strcpy (mask, " r--p" );
273- break ;
274-
275- case READ_WRITE:
276- strcpy (mask, " rw-p" );
277- break ;
300+ char protectionBuff[] = " ---p" ;
278301
279- case EXECUTE_READ:
280- strcpy (mask, " r-xp" );
281- break ;
302+ if (protection & PROT_READ)
303+ protectionBuff[0 ] = ' r' ;
282304
283- case EXECUTE_READ_WRITE:
284- strcpy (mask, " rwxp" );
285- break ;
305+ if (protection & PROT_WRITE)
306+ protectionBuff[1 ] = ' w' ;
286307
287- default :
288- return false ;
289- break ;
290- }
308+ if (protection & PROT_EXEC)
309+ protectionBuff[2 ] = ' x' ;
291310
292311 if (ForEachLine (mapsfd, [&](const std::string currLine){
293- if (strstr (currLine.c_str (), mask ) == nullptr )
312+ if (strstr (currLine.c_str (), protectionBuff ) == nullptr )
294313 return true ;
295314
296- SegmentInfo currSegment ;
315+ segments. push_back ({}) ;
297316
298- ParseMapLineSegment (currLine.c_str (), currSegment);
299- segments.push_back (currSegment);
317+ SegmentInfo& segmentInfo = segments[segments.size () - 1 ];
318+
319+ ParseMapLineSegment (currLine.c_str (), segmentInfo);
300320
301321 return true ;
302322 }) == false )
@@ -305,59 +325,89 @@ bool ProcessManager::EnumSegments(std::vector<SegmentInfo> & segments, int prot)
305325 return true ;
306326}
307327
308- void ProcessManager::DisablePtrace ()
309- {
310- #ifdef DEBUG
311- printf (" ProcessManager::DisablePtrace()\n " );
312- #endif
313- uintptr_t ptraceAddr = ptraceAddr = FindExternalSymbol (" libc.so" , " ptrace" );
328+ // void ProcessManager::DisablePtrace()
329+ // {
330+ // #ifdef DEBUG
331+ // printf("ProcessManager::DisablePtrace()\n");
332+ // #endif
333+ // uintptr_t ptraceAddr = FindExternalSymbol("libc.so", "ptrace");
314334
315- while (!ptraceAddr){
316- printf (" ProcessManager : ptrace not found\n " );
317- ptraceAddr = FindExternalSymbol (" libc.so" , " ptrace" );
318- };
335+ // while(!ptraceAddr){
336+ // printf("ProcessManager : ptrace not found\n");
337+ // ptraceAddr = FindExternalSymbol("libc.so", "ptrace");
338+ // };
319339
320- unsigned char buff[] = " \x00\x00\xA0\xE3\x1E\xFF\x2F\xE1 " ; // mov r0, 0
321- // bx lr
322- memcpy (buff, ptraceAddr, sizeof (buff));
323- }
340+ // unsigned char buff[] = "\x00\x00\xA0\xE3\x1E\xFF\x2F\xE1"; // mov r0, 0
341+ // // bx lr
342+ // WriteMemory (buff, ptraceAddr, sizeof(buff));
343+ // }
324344
325345uintptr_t ProcessManager::FindCodeCave (uintptr_t size, uintptr_t prot)
326346{
327347 #ifdef DEBUG
328348 printf (" ProcessManager::FindCodeCave(%08X, %d)\n " , size, prot);
329349 #endif
330350
351+ int exp = 1 ;
352+ void * tmpBuff = malloc (exp * exp);
353+
354+ if (tmpBuff == nullptr )
355+ return 0 ;
356+
331357 std::vector<SegmentInfo> segments;
358+
332359 if (!EnumSegments (segments, prot))
360+ {
361+ free (tmpBuff);
333362 return 0 ;
363+ }
334364
335- size = ((size / 4 ) + 1 ) * 4 ;
365+ size = ((size / 4 ) + 1 ) * 4 ; // Aligning the size
366+
367+ std::vector<unsigned char > pattern;
368+ std::string mask = " " ;
369+
370+ for (int i = 0 ; i < size; i++)
371+ {
372+ pattern.push_back (0x0 );
373+ mask.push_back (' x' );
374+ }
375+
376+ uintptr_t result = 0 ;
377+
336378 for (int i = 0 ; i < segments.size (); i++)
337379 {
338- uintptr_t lastMatchIndex = 0 ;
339- for (uintptr_t currAddr = segments[i].start ; currAddr + size < segments[i].end ; currAddr += 1 + lastMatchIndex)
380+ while (exp * exp < segments[i].size )
340381 {
341- if (ReadProcessMemory<unsigned char >(currAddr) == 0x0 )
342- {
343- bool found = true ;
344- for (int j = 1 ; j <= size; j++)
345- {
346- if (ReadProcessMemory<unsigned char >(currAddr + j) != 0x0 )
347- {
348- found = false ;
349- break ;
350- } else
351- lastMatchIndex = j;
352- }
353-
354- if (found)
355- return ((currAddr / 4 ) + 1 ) * 4 ;
356- }
382+ free (tmpBuff);
383+ exp++; tmpBuff = malloc (exp * exp);
384+
385+ if (tmpBuff == nullptr )
386+ return 0 ;
357387 }
388+ // At this point we have enought memory to store the entire current segment
389+
390+ if (ReadMemory (segments[i].start , tmpBuff, segments[i].size ) == false )
391+ continue ;
392+
393+ // We sucessfully Readed the current segment
394+ const char * codeCave = pattern_scan ((const char *) pattern.data (), mask.c_str (), (const char *)tmpBuff, segments[i].size );
395+
396+ if (codeCave == nullptr )
397+ continue ;
398+
399+ // At this point, we have found a codecaves
400+ // Lets calculate its position in the remote area
401+
402+ size_t offset = (uintptr_t )codeCave - (uintptr_t )tmpBuff;
403+
404+ result = segments[i].start + offset;
405+ break ;
358406 }
359407
360- return 0 ;
408+ free (tmpBuff);
409+
410+ return ((result / 4 ) + 1 ) * 4 ;
361411}
362412
363413bool ProcessManager::Hook (uintptr_t src, uintptr_t dst, uintptr_t size)
@@ -383,7 +433,7 @@ bool ProcessManager::Hook(uintptr_t src, uintptr_t dst, uintptr_t size)
383433 *(uintptr_t *)(detour + 1 ) = relativeAddr;
384434 #endif
385435
386- memcpy (detourPtr, src, tSize);
436+ WriteMemory (detourPtr, src, tSize);
387437
388438 return true ;
389439}
@@ -448,7 +498,7 @@ bool ProcessManager::LoadToMemoryAndHook(uintptr_t targetSrc, void* targetDst, u
448498
449499 #endif
450500
451- memcpy ((unsigned char *)targetDst, DstAddrinTargetMemory, localDstSize);
501+ WriteMemory ((unsigned char *)targetDst, DstAddrinTargetMemory, localDstSize);
452502 return Hook (targetSrc, DstAddrinTargetMemory);
453503
454504}
0 commit comments