Advertisement
Guest User

Untitled

a guest
Jul 12th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define fbc -dll -x memsearch.dll
  2.  
  3. #include "windows.bi"
  4. #include "crt.bi"
  5.  
  6. const sTitle = "Mysoft MemSearch"
  7.  
  8. static shared as integer iConsoleCreated
  9. static shared as HMODULE hModThis
  10.  
  11. sub CreateConsole() constructor
  12.   'Create a console
  13.   iConsoleCreated = AllocConsole()
  14.   'if iConsoleCreated then
  15.   'and reopen CRT to use that console
  16.   freopen("CONIN$", "r", stdin)
  17.   freopen("CONOUT$", "w", stdout)
  18.   freopen("CONOUT$", "w", stderr)
  19.   setvbuf(stdout,0,_IONBF,0)
  20. end sub
  21. sub DestroyConsole() destructor
  22.   Messagebox(null,"DLL will be unloaded...",sTitle,MB_ICONINFORMATION or MB_SYSTEMMODAL)
  23.   if iConsoleCreated then
  24.     iConsoleCreated=0
  25.     FreeConsole()
  26.   end if
  27. end sub
  28.  
  29. function GetModuleByPtr( pAddr as any ptr ) as zstring ptr
  30.   static as zstring*MAX_PATH zTemp = any
  31.   dim as HMODULE hModChk = any
  32.   const gmhFlags = 4 or 2
  33.   if GetModuleHandleEx(gmhFlags,pAddr,@hModChk) then      
  34.     GetModuleFilename(hModChk,@zTemp,MAX_PATH)
  35.     return @zTemp+instrrev(zTemp,!"\\")
  36.   end if
  37.   return 0
  38. end function
  39. function GetProt( dwProt as DWORD ) as string  
  40.   dim as string sResult
  41.   select case (dwProt and &hFF)
  42.   case PAGE_READONLY          : sResult = "R"
  43.   case PAGE_READWRITE         : sResult = "RW"
  44.   case PAGE_WRITECOPY         : sResult = "RC"
  45.   case PAGE_EXECUTE           : sResult = "E"
  46.   case PAGE_EXECUTE_READ      : sResult = "RE"
  47.   case PAGE_EXECUTE_READWRITE   : sResult = "RWE"
  48.   case PAGE_EXECUTE_WRITECOPY   : sResult = "RWEC"
  49.   case PAGE_NOACCESS            : sResult = "NA"
  50.   case else                   : return hex$(dwProt,8)
  51.   end select    
  52.   if (dwProt and PAGE_GUARD) then sResult += " G"
  53.   if (dwProt and PAGE_NOCACHE) then sResult += " N"
  54.   return sResult
  55. end function
  56. sub DumpInfo( tMem as MEMORY_BASIC_INFORMATION )
  57.   with tMem
  58.     var pzState = cast(zstring ptr,iif(.State=MEM_COMMIT,@"YES",@"NO"))        
  59.     var pzMod = GetModuleByPtr(cast(any ptr,tMem.BaseAddress))
  60.     if pzMod=0 then pzMod = @"???"
  61.    
  62.     select case .State
  63.     case MEM_COMMIT      
  64.       printf(!"%p %p %-8s %p %-8s %-8s %s\r\n", .BaseAddress,.AllocationBase, _
  65.       pzState,cast(any ptr,.RegionSize),GetProt(.Protect),GetProt(.AllocationProtect),pzMod)    
  66.     case MEM_RESERVE
  67.       printf(!"%p %p %-8s %p %-8s %-8s %s\r\n", .BaseAddress,.AllocationBase, _
  68.       pzState,cast(any ptr,.RegionSize),"-",GetProt(.AllocationProtect),pzMod)    
  69.     case MEM_FREE
  70.       printf(!"%p %-8s %-8s %p %-8s %-8s %s\r\n", .BaseAddress,"-", _
  71.       pzState,cast(any ptr,.RegionSize),"-","-",pzMod)    
  72.     end select
  73.   end with
  74. end sub
  75.  
  76. sub MemSearch()
  77.   printf(!"DLL loaded...")  
  78.   Messagebox(null,"DLL loaded...",sTitle,MB_ICONINFORMATION or MB_SYSTEMMODAL)
  79.   dim as MEMORY_BASIC_INFORMATION tMem
  80.   dim as any ptr pBlock = any
  81.  
  82.   #if 0  
  83.   color 15 : printf(!"Base     Alloc    Commit   Size     Prot     OrgProt  Type\r\n") : color 7  
  84.   dim as integer iTot
  85.   pBlock = cast(any ptr,&h10000)
  86.   do
  87.     if VirtualQuery(pBlock,@tMem,sizeof(tMem))=0 then exit do    
  88.     DumpInfo( tMem )    
  89.     if tMem.State = MEM_COMMIT then iTot += tMem.RegionSize    
  90.     pBlock += tMem.RegionSize
  91.   loop while pBlock
  92.   printf(!"Total: %i kb\r\n",clng(iTot\1024))
  93.   #endif
  94.  
  95.   type fbString
  96.     pzData as zstring ptr
  97.     uLen   as uinteger
  98.     uSize  as uinteger
  99.   end type
  100.  
  101.   var sHello = "Hello World", sHello2 = ""
  102.   dim as zstring*24 zStore
  103.   *cptr(fbString ptr,@sHello2) = type(@zStore,len(sHello)*2,24)          
  104.   for N as integer = 0 to len(sHello)-1
  105.     sHello2[N*2] = sHello[N]
  106.   next N
  107.  
  108.   pBlock = cast(any ptr,&h10000)
  109.   dim as string sTemp
  110.   'printf(!"Stack=%08X\r\n",clng(@sTemp) and &hFFFF0000)
  111.   do
  112.     if VirtualQuery(pBlock,@tMem,sizeof(tMem))=0 then exit do        
  113.     if tMem.State = MEM_COMMIT andalso (.tMem.Protect and PAGE_GUARD)=0 then
  114.       var lA = (cast(LONG_PTR,pBlock) and &hFFFFFFFFFF0000), lB = (cast(LONG_PTR,@sTemp) and &hFFFFFFFFFF0000)
  115.       'printf(!"%08X  ",lA,lB)
  116.       print hex$(pBlock),
  117.       if .tMem.RegionSize andalso lA <> lB then        
  118.         dim as dword dwOldProt
  119.         if ((.tMem.Protect and &hFF)=PAGE_NOACCESS) then continue do
  120.           'VirtualProtect(pBlock,tMem.RegionSize,PAGE_READONLY,@dwOldProt)
  121.         'end if
  122.         *cptr(fbString ptr,@sTemp) = type(pBlock,tMem.RegionSize,.tMem.RegionSize)        
  123.         var iPos = 1, iFound = 0
  124.         do
  125.           iPos = instr(iPos,sTemp,sHello2)
  126.           if iPos = 0 then exit do
  127.           var pFound = pBlock+iPos-1
  128.           lA = (cast(LONG_PTR,pFound) and &hFFFFFFFFFFFF0000)
  129.           if lA <> lB then
  130.             printf(!"\r\nfound at 0x%p\r\n",pFound)                    
  131.             sleep 100,1
  132.             VirtualProtect(pBlock,tMem.RegionSize,PAGE_READWRITE,@dwOldProt)
  133.             puts("protection changed... now changing value")
  134.             sleep 100,1
  135.             memcpy(pFound,@wstr("You are OK!"),22)
  136.             puts("changing protection back")
  137.             sleep 100,1
  138.             VirtualProtect(pBlock,tMem.RegionSize,dwOldProt,@dwOldProt)
  139.             printf(!"changed...\r\n")
  140.             sleep 100,1
  141.             'exit do,do
  142.             iFound = 1
  143.           end if
  144.           iPos += 1
  145.         loop
  146.         'if iFound=0 then
  147.         '  printf(!"nothing found on this block\r\n")
  148.         'end if
  149.         'if ((.tMem.Protect and &hFF)=PAGE_NOACCESS) then
  150.         '  VirtualProtect(pBlock,tMem.RegionSize,dwOldProt,@dwOldProt)
  151.         'end if
  152.        
  153.       end if
  154.     end if
  155.     pBlock = cast(any ptr,tMem.BaseAddress+tMem.RegionSize)
  156.   loop while pBlock
  157.  
  158.   puts(!"Done...")
  159.   zStore = space(23)
  160.   *cptr(fbString ptr,@sTemp) = type(0,0,0)
  161.   *cptr(fbString ptr,@sHello2) = type(0,0,0)
  162.   sHello = ""
  163.  
  164. end sub
  165. sub DllThread(ID as any ptr) export
  166.   MemSearch()
  167.   puts(!"exitting...")
  168.   FreeLibraryAndExitThread( hModThis , 0 )
  169. end sub
  170.  
  171. const gmhFlags = 4 or 2
  172. if GetModuleHandleEx(gmhFlags,cast(any ptr,@DllThread),@hModThis) then
  173.   'CreateThread(null,65535,
  174.   ThreadCreate(@DllThread,0)
  175. else
  176.   Messagebox(null,"Init failed",sTitle,MB_ICONERROR or MB_SYSTEMMODAL)
  177. end if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement