Click here to Skip to main content
15,894,410 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.9K   10.1K   285  
An introduction to injection the code into Import Table of Portable Executable file format, which is called API redirection technique.
/* dataview2.cpp --

   This file is part of the "PE Maker".

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

   "PE Maker" 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 <windowsx.h>
#include <commctrl.h>
#include <stdio.h>
#include "dataview2.h"
#include "main.h"

DATAVIEW2		*szDataView2;
int				iDataView2;
HWND		hDataView2;
static DWORD	dwStyle;
static DWORD	dwExtendedStyle;

static int iCount;

//bFileOpen
BOOL DataView2_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam);
void DataView2_OnCommand (HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
void DataView2_Insert(DWORD ThunkRVA,
					DWORD ThunkOffset,
					DWORD ThunkValue,
					DWORD Hint,
					PCHAR ApiName);
void DataView2_ClearList();

#define C_COLUMNS 5
// InitListViewColumns - adds columns to a list-view
// control. 
// Returns TRUE if successful, or FALSE otherwise. 
// hWndListView - handle to the list-view control. 
static int iWidth[C_COLUMNS ]={90,90,90,75,255};
BOOL DataView2_InitListViewColumns(HWND hWndListView) 
{ 
    char szText[256];     // temporary buffer 
    LVCOLUMN lvc; 
    int iCol; 
	// Initialize the LVCOLUMN structure.
	// The mask specifies that the format, width, text, and
	// subitem members of the structure are valid. 
	lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; 	  
	// Add the columns. 
	for (iCol = 0; iCol < C_COLUMNS; iCol++) 
	{
		lvc.iSubItem = iCol;
		lvc.pszText = szText;
		lvc.cx = iWidth[iCol];     // width of column in pixels
		lvc.fmt = LVCFMT_LEFT;  // left-aligned column
		LoadString(hInst, IDS_D2_FIRSTCOLUMN + iCol, 
			szText, sizeof(szText)/sizeof(szText[0]));
		if (ListView_InsertColumn(hWndListView, iCol,&lvc) == -1) 
			return FALSE; 
	}
	return TRUE; 
} 

// This code snippet adds three items, each with three
// subitems, to a list-view control. 
// hWndListView - handle to the list-view control.
// The following application-specific structure, 
// PETINFO, is used in the snippet.

//static DATAVIEW DataView;

BOOL DataView2_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
	//SECINFO SectionInfo;
	hDataView2=GetDlgItem(hwnd,IDC_PROCNAMES);
	dwStyle = GetWindowLong(hDataView2, GWL_STYLE);
	dwStyle|= LVS_REPORT | LVS_SINGLESEL;
	dwStyle&=~LVS_NOSCROLL;
	SetWindowLong(hDataView2, GWL_STYLE, dwStyle);
	DataView2_InitListViewColumns(hDataView2);
	return FALSE;
}

void DataView2_Insert(DWORD ThunkRVA,
					DWORD ThunkOffset,
					DWORD ThunkValue,
					DWORD Hint,
					PCHAR ApiName, 
					DWORD Ordinal)
{
	CHAR stemp[127];
	LVITEM lvI;
	// Some code to create the list-view control.
	// Initialize LVITEM members that are common to all
	// items. 
	lvI.mask =  LVIF_TEXT | LVIF_STATE ; 
	lvI.state = 0; 
	lvI.stateMask = 0; 
	// Initialize LVITEM members that are different for each item. 
	lvI.iItem = iCount;
	lvI.iSubItem = 0;
	sprintf(stemp, "%08X", ThunkRVA);
	lvI.pszText = (LPSTR)stemp;
	if(ListView_InsertItem(hDataView2, &lvI) == -1) return;
	//ListView_SetItemText(hDataView1,i,1,szDataView1[i].szOffset);
	sprintf(stemp, "%08X", ThunkOffset);
	ListView_SetItemText(hDataView2,iCount,1,stemp);
	sprintf(stemp, "%08X", ThunkValue);
	ListView_SetItemText(hDataView2,iCount,2,stemp)
	if(ApiName!=NULL)
	{
		sprintf(stemp, "%04X", Hint);
	}
	else
	{
		sprintf(stemp, "-", Hint);
	}
	ListView_SetItemText(hDataView2,iCount,3,stemp);
	if(ApiName!=NULL)
	{
		ListView_SetItemText(hDataView2,iCount,4,ApiName);
	}
	else
	{
		sprintf(stemp, "Ordinal: %04X", Ordinal);
		ListView_SetItemText(hDataView2,iCount,4,stemp);
	}
	iCount++;
}

void DataView2_ClearList()
{
	iCount = 0;
	ListView_DeleteAllItems(hDataView2);
}

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