The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: six_L on May 27, 2006, 01:13:23 PM

Title: proxy DLL
Post by: six_L on May 27, 2006, 01:13:23 PM
hey to all
if you'v known this,plz talk about.
Title: Re: proxy DLL
Post by: mnemonic on May 27, 2006, 01:20:54 PM
http://www.masm32.com/board/index.php?topic=4526.0
Title: Re: proxy DLL
Post by: sluggy on May 27, 2006, 02:04:30 PM
What do you want to do?
Title: Re: proxy DLL
Post by: six_L on May 27, 2006, 02:19:37 PM
hey,mnemonic and sluggy
Thanks you for your respondence
Quotehttp://www.masm32.com/board/index.php?topic=4526.0
the information was too few to understand.
QuoteWhat do you want to do?
learning every technique which i touched.

Title: Re: proxy DLL
Post by: Ossa on May 27, 2006, 02:44:09 PM
Quote from: six_L on May 27, 2006, 02:19:37 PM
QuoteWhat do you want to do?
learning every technique which i touched.

erm... what do you want to do in this case?

1) write a DLL that wraps another DLL?
2) write a DLL that has functions for a proxy server?
3) something else?

Could you explain a bit more fully?

Ossa
Title: Re: proxy DLL
Post by: six_L on May 27, 2006, 02:58:18 PM
sometimes there isn't any aim on the learning. (i know nothing about this, so, how do i tell you what i'm going to do? )
                                                                                                                       
#include "stdafx.h"                                                                                                     
                                                                                                                       
//Forward declaration of Prototypes                                                                                     
BOOL PerformPatch();                                                                                                   
                                                                                                                       
//Module of the loaded Library, it's a global variable.                                                                 
HMODULE hDllMod=0;                                                                                                     
char b[1024]; //messages buffer   
                                                                                                                       
//////////////////////////////////////////////////////////////////////////                                             
//MAIN PROGRAM PATCH INFO:                                                                                             
//Patch Address info: # elements in following arrays must be synchronized
//for Address/scan/replace                     
DWORD AddressOfPatch[] = {0x10CA, 0x10CB};                                                                             
                                                                                                                       
//Patch byte info:                                                                                                     
//Search (read) byte. Original bytes read from the dll in memory (attn: #
//elements must be the same of AddressOfPatch) 
BYTE scanbyte[] = {0x75, 0x28};                                                                                         
//Found  (write) byte. New patch bytes to be written in memory (attn: #
//elements must be the same of AddressOfPatch)   
BYTE replbyte[] = {0x75, 0x18};                                                                                         
                                                                                                                       
char szDllName[]=".\\_RegistrationDll.dll";                                                                             
//////////////////////////////////////////////////////////////////////////                                             
                                                                                                                       
BOOL APIENTRY DllMain( HANDLE hModule,                                                                                 
                       DWORD  ul_reason_for_call,                                                                       
                       LPVOID lpReserved                                                                               
      )                                                                                                                 
{                                                                                                                       
    // Remove this if you use lpReserved                                                                               
UNREFERENCED_PARAMETER(lpReserved);                                                                                   
                                                                                                                       
    switch(ul_reason_for_call)                                                                                         
{                                                                                                                     
         case DLL_PROCESS_ATTACH:                                                                                       
  {                                                                                                                     
                                                                                                                       
             #ifdef _DEBUG                                                                                             
//Needed to be able to debug the program from the compiler.                                               
                   
   HideDebugger(GetCurrentThread(), GetCurrentProcess());                                                               
             #endif                                                                                                     
                                                                                                                       
   hDllMod=::LoadLibrary((LPCSTR)szDllName);                                                                           
             if(hDllMod==NULL) {                                                                                       
//Find the last '\\' to obtain a pointer to just the base module name part                           
//(i.e. mydll.dll w/o the path)                                                                       
    PSTR pszBaseName = strrchr( szDllName, '\\' );                                                                     
                  if ( pszBaseName ) { //We found a path, so advance to the base module name                           
     pszBaseName++;                                                                                                     
    }                                                                                                                   
                  else {                                                                                               
     pszBaseName = szDllName;  //No path.  Use the same name for both                                                   
    }                                                                                                                   
                                                                                                                       
    sprintf(b, "%s not found.\r\nHave you renamed it as _%s\r\n Is this dll into the same path?",                       
     pszBaseName, (pszBaseName+1));                                                                                     
    ::MessageBox(NULL, b, "Load Library Failed", MB_OK+MB_TASKMODAL+MB_ICONERROR);                                     
                                                                                                                       
                  return TRUE;                                                                                         
   }                                                                                                                   
  }                                                                                                                     
         break;                                                                                                         
                                                                                                                       
         case DLL_PROCESS_DETACH:                                                                                       
  {                                                                                                                     
             if(hDllMod!=NULL) {                                                                                       
    ::FreeLibrary(hDllMod);                                                                                             
    hDllMod=NULL;                                                                                                       
   }                                                                                                                   
  }                                                                                                                     
         break;                                                                                                         
                                                                                                                       
         case DLL_THREAD_ATTACH: {} break; //not used at the moment                                                     
  case DLL_THREAD_DETACH: {} break; //not used at the moment                                                           
                                                                                                                       
       }                                                                                                               
    return TRUE;                                                                                                       
                                                                                                                       
}                                                                                                                       
                                                                                                                       
BOOL PerformPatch() {                                                                                                   

int      i=0, j=0;                                                                                               
    int      nPatches=0;                                                                                             
DWORD dwRead=0;                                                                                                     
DWORD dwWritten=0;                                                                                                   
BYTE DataRead[] = {0};                                                                                               
    int      AppliedPatches=0;                                                                                       
                                                                                                                     
//////////////////////////////////////////////////////////////////////////                                       
//Apply the patches to the *.exe or *.dll module                                                                 
//Calculate number of patches / addresses (not always this thing works, but here it is)                           
nPatches = sizeof(AddressOfPatch) / sizeof(AddressOfPatch[0]);                                                       
                                                                                                                     
    for ( j = 0; j < nPatches; j++ ) {                                                                               
         LPVOID CurrentAddress= (LPVOID)((DWORD)hDllMod + (DWORD)AddressOfPatch[j]);                                 
                                                                                                                     
//Pay attention that you have to arrive to the patch address through the CurrentProcess                     
//HANDLE and not through the hDllmod, otherwise access to memory will be denied.                             
//The GetCurrentProcess API returns the HANDLE of the process owning the program.                           
  ReadProcessMemory(GetCurrentProcess(), CurrentAddress, DataRead, sizeof(BYTE), &dwRead);                           
         if(DataRead[0] == scanbyte[j])                                                                               
  {                                                                                                                   
   WriteProcessMemory (GetCurrentProcess(), CurrentAddress, &replbyte[j], sizeof(BYTE), &dwWritten);                 
             #ifdef _DEBUG                                                                                           
   sprintf ( b, "One Patch applied at address: %08X (%02X -> %02X)",                                                 
                  CurrentAddress, (LPVOID) scanbyte[j], (LPVOID) replbyte[j] );                                       
   MessageBox(0, b ,"Attention",MB_OK+MB_TASKMODAL);                                                                 
             #endif                                                                                                   
   AppliedPatches++;                                                                                                 
  }                                                                                                                   
}                                                                                                                   
                                                                                                                     
    //Have we successfully patched all the things? If yes clean what we did before.                                   
    if(AppliedPatches==nPatches)                                                                                     
             return TRUE;                                                                                             
                                                                                                                     
    return FALSE;                                                                                                     
}                                                                                                                     

BOOL CheckRegistrationNumber(char *a, char* b) {
                                                                                                                     
    //Performs the patch                                                                                             
PerformPatch();                                                                                                     
                                                                                                                     
    //Call the original method and returns to the caller the result.                                                 
BOOL (*fp)(char *, char*);                                                                                           
    fp = (BOOL (*)(char *, char*)) GetProcAddress(hDllMod,"CheckRegistrationNumber");                                 
                                                                                                                     
    if(fp!=NULL)                                                                                                     
         return fp(a,b);                                                                                             
    else                                                                                                             
         return FALSE;                                                                                               
}                                     
Title: Re: proxy DLL
Post by: Mark Jones on May 27, 2006, 03:12:34 PM
six_L you naughty boy! :naughty: ::)

According to Google your code is part of CodeBreakers Magazine, specifically the section on how to hijack protected DLL's.
Title: Re: proxy DLL
Post by: stanhebben on May 27, 2006, 03:16:12 PM
Indeed. This code changes a dll and could be used for hacking or similar purposes and has nothing to do with proxying dlls.
Title: Re: proxy DLL
Post by: six_L on May 27, 2006, 04:26:27 PM
 :naughty:
that's the law of thinking inertia.

why can't the technique use to protect the software? :(
Title: Re: proxy DLL
Post by: stanhebben on May 27, 2006, 04:51:53 PM
Making sure your code is read - only, and I don't know if that's possible, but you might also want to check which module loaded the dll.
Title: Re: proxy DLL
Post by: MichaelW on May 27, 2006, 05:25:09 PM
Quote from: six_L on May 27, 2006, 04:26:27 PM
why can't the technique use to protect the software? :(

It probably could be, but it's more likely to be used for bad things, like the sort of thing the code you posted was designed to do. I'm locking this thread.