Click here to Skip to main content
15,886,258 members
Articles / Containers / Virtual Machine

Injective Code inside Import Table

Rate me:
Please Sign up or sign in to vote.
4.95/5 (119 votes)
29 Mar 2007GPL316 min read 239.3K   10.1K   285  
An introduction to injection the code into Import Table of Portable Executable file format, which is called API redirection technique.
/* DumpWin.cpp --

   This file is part of the "ZImport 0.1".

   Copyright (C) 2006-2007 Ashkbiz Danehkar
   All Rights Reserved.

   zero Dump library are free software; you can redistribute them
   and/or modify them under the terms of the GNU General Public License as
   published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYRIGHT.TXT.
   If not, write to the Free Software Foundation, Inc.,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   Ashkbiz Danehkar
   <ashkbiz@yahoo.com>
*/
#include "stdafx.h"
#include <stdio.h>
#include "main.h"
#include "DumpWin.h"

#ifdef _DEBUG
#define DEBUG_NEW
#endif

static int  iTitle=0;
static char *szTitle=NULL;
static char szClassName[64];
static char szStyle[64];
static char szDimension[128];
static DWORD dwStyle;
static DWORD dwExtendedStyle;
static UINT dwID;
static char szProcess[64];
static char szHandle[64];

static BOOL	m_bCarriageReturn;
static DWORD	m_dwIndentLevel;
static DWORD	m_dwIndentSize;
static DWORD	m_dwDetails;

bool SaveToRegistry(bool StayOnTop);
bool LoadFromRegistry(bool *StayOnTop);

static void DumpTitle(HWND hWnd);
static void DumpClassName(HWND hWnd);
static void DumpStyles(HWND hWnd);
static void DumpDimension(HWND hWnd);

void DumpWindow(HWND hWnd);
void GetWindowInfo(HWND hWnd);


//----------------------------------------------------------------------------
bool SaveToRegistry(bool StayOnTop)
{
    char KeyMain[127];
	HKEY RootKey=HKEY_CURRENT_USER;
    strcpy(KeyMain,"Software\\ZImport");
    bool Result;
	HKEY hKeyOption;
	DWORD dwDispos;
	if(ERROR_SUCCESS!=::RegCreateKeyEx(RootKey,KeyMain,
									0,NULL,
									REG_OPTION_NON_VOLATILE	
									,KEY_ALL_ACCESS,NULL,
									&hKeyOption,&dwDispos))
	{
		return FALSE;
	}
    __try 
    {
		if(::RegOpenKeyEx(RootKey,KeyMain,0,KEY_ALL_ACCESS,&hKeyOption)== ERROR_SUCCESS) 
        {	
			::RegSetValueEx(hKeyOption,
						"StayOnTop",0,
						REG_DWORD,
						reinterpret_cast<BYTE *>(&StayOnTop),
						4);
			Result=TRUE;
        }
		else Result=FALSE;
    }
	__finally
    {
       ::RegCloseKey(hKeyOption);
    }
    return(Result);
}
//----------------------------------------------------------------------------
bool LoadFromRegistry(bool *StayOnTop)
{
    char KeyMain[127];
	HKEY RootKey=HKEY_CURRENT_USER;
    strcpy(KeyMain,"Software\\ZImport");
	HKEY hKeyOption;
	bool Result;
	DWORD dwSize     = 0; 
	DWORD dwDataType = 0; 
	DWORD dwValue    = 0; 
	if(::RegOpenKeyEx(RootKey,KeyMain,0,KEY_ALL_ACCESS,&hKeyOption) != ERROR_SUCCESS) 
	{ 
		return FALSE;
	}
    __try 
	{
		if(::RegOpenKeyEx(RootKey,KeyMain,0,KEY_ALL_ACCESS,&hKeyOption) == ERROR_SUCCESS) 
		{ 

			dwSize = sizeof(StayOnTop); 
			::RegQueryValueEx(hKeyOption,
						"StayOnTop",NULL,
						&dwDataType,
						reinterpret_cast<BYTE *>(StayOnTop),
						&dwSize);
			Result=TRUE;
        }
		else Result=FALSE;
    }
	__finally
    {
		::RegCloseKey(hKeyOption);
    }
    return(Result);
}

//---------------------------------------------------------------------------
static void DumpTitle(HWND hWnd)
{
	int iLen;
	GetClassName(hWnd, szClassName, sizeof(szClassName)/sizeof(TCHAR));
	CharUpperBuff(szClassName,(DWORD)strlen(szClassName));
	if((strstr(szClassName,"EDIT")==NULL)&&(strstr(szClassName,"BOX")==NULL))
	{
		iLen = GetWindowTextLength(hWnd);
		szTitle=new TCHAR[iLen+1];
		memset(szTitle,0,iLen+1);
		GetWindowText(hWnd,szTitle,iLen+1);
	}
	else
	{
		iLen=128;
		szTitle=new TCHAR[iLen+1];
		memset(szTitle,0,iLen+1);
		HWND hWndParent=GetParent(hWnd);
		SendDlgItemMessage(hWndParent,dwID,
						   WM_GETTEXT, 
						   iLen+1, (LPARAM)szTitle);
		szTitle[127]=0;
	}
	SetDlgItemText(hwndMain,IDC_CAPTION,szTitle);
}

//---------------------------------------------------------------------------
static void DumpClassName(HWND hWnd)
{
	GetClassName(hWnd, szClassName, sizeof(szClassName)/sizeof(TCHAR));
	SetDlgItemText(hwndMain,IDC_CLASS,szClassName);
}

//---------------------------------------------------------------------------
static void DumpStyles(HWND hWnd)
{
	sprintf(szStyle, "%08X", dwStyle);
	SetDlgItemText(hwndMain,IDC_STYLE,szStyle);
}

//---------------------------------------------------------------------------
static void DumpDimension(HWND hWnd)
{
	RECT wndRect;
	GetWindowRect(hWnd,&wndRect);
	sprintf(szDimension, "(%d, %d)-(%d, %d) %dx%d",wndRect.left, wndRect.top, wndRect.right, wndRect.bottom, wndRect.right-wndRect.left, wndRect.bottom-wndRect.top);
	SetDlgItemText(hwndMain,IDC_RECT,szDimension);
}

//---------------------------------------------------------------------------
void DumpWindow(HWND hWnd)
{
	if (!IsWindow(hWnd)) return;
	//WINDOWINFO wininfo=GetWindowInfo(hWnd,&wininfo);
	dwID = GetWindowLong(hWnd, GWL_ID);
	dwStyle =GetWindowLong(hWnd, GWL_STYLE);
	dwExtendedStyle =GetWindowLong(hWnd, GWL_EXSTYLE);
	DumpTitle(hWnd);
	DumpClassName(hWnd);
	DumpStyles(hWnd);
	DumpDimension(hWnd);
}

//---------------------------------------------------------------------------
void GetWindowInfo(HWND hWnd)
{	
	DumpWindow(hWnd);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Germany Germany
Ashkbiz Danehkar studied electrical engineering and computational science at the University of Rostock, Germany, where he obtained a Master of Science in Computational Engineering in the special field of Electrical Engineering in 2007. He worked as a software and hardware developer for some private limited companies until 2005, mostly focusing on industrial automation and microcontroller programming. During 2005–2006, he worked part-time remotely as a software reverse engineer for Panda Security (Bilbao, Spain). His master's thesis in 2007 was about the development of a microcontroller-based measurement system using an embedded system equipped with a real-time operating system (RTOS) and an AVR microcontroller to monitor the neuromuscular blockade and control the anesthesia.

Comments and Discussions