王朝网络
分享
 
 
 

Delphi Winsock Hooking Example by Aphex

王朝delphi·作者佚名  2006-01-10
宽屏版  字体: |||超大  

{

Delphi Winsock Hooking Example by Aphex

http://www.iamaphex.cjb.net

unremote@knology.net

This example shows you how to hook winsock functions

of a target process and control incomming and outgoing

data. It is based on send and recv but it will work the

same way applied to sendto and recvfrom.

The output file is a CPL (Control Panel Extension) which

is simply a special DLL that is loaded when it is double

clicked. This saves us from having to write a seperate

loader for the hook library.

The example shows how to hook the needed functions and

perform some simple manipulation of the data, using two

different methods of accessing the data. The second, which

uses pointers, is more flexible but also more complex.

}

library Project1;

uses

Windows,

Winsock,

SysUtils,

madCodeHook;

{$R *.RES}

{$E CPL}

var

sendNextHook: function(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;

recvNextHook: function(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;

DataSocket: TSocket;

const

szTargetExe: string = 'GAME.EXE';

function ConvertDataToAscii(Buffer: pointer; Length: Word): string;

var

Iterator: integer;

AsciiBuffer: string;

begin

AsciiBuffer := '';

for Iterator := 0 to Length - 1 do

begin

if char(pointer(integer(Buffer) + Iterator)^) in [#32..#127] then

AsciiBuffer := AsciiBuffer + ' ' + char(pointer(integer(Buffer) + Iterator)^) + ' '

else

AsciiBuffer := AsciiBuffer + ' . ';

end;

Result := AsciiBuffer;

end;

function ConvertDataToHex(Buffer: pointer; Length: Word): string;

var

Iterator: integer;

HexBuffer: string;

begin

HexBuffer := '';

for Iterator := 0 to Length - 1 do

begin

HexBuffer := HexBuffer + IntToHex(Ord(char(pointer(integer(Buffer) + Iterator)^)), 2) + ' ';

end;

Result := HexBuffer;

end;

function recvHookProc(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;

var

AsciiBuffer: string;

HexBuffer: string;

DataBuffer: pchar;

begin

//call the real winsock function

Result := recvNextHook(s, Buf, len, flags);

//allocate memory for our copy of the data

GetMem(DataBuffer, Result);

try

//get our copy of the data

CopyMemory(DataBuffer, @Buf, Result);

//using the data as a byte array

DataBuffer[0] := chr(10); //changing first byte

DataBuffer[1] := chr(20); //changing second byte

DataBuffer[2] := chr(30); //changing thrid byte

//using the data as a pointer to other data sizes

word(pointer(DataBuffer)^) := 10; //changing first 2 bytes

dword(pointer(integer(DataBuffer) + 2)^) := 20; //changing next 4 bytes

word(pointer(integer(DataBuffer) + 6)^) := 30; //changing next 2 bytes

//overwrite the original data with our new data

CopyMemory(@Buf, DataBuffer, Result);

finally

FreeMem(DataBuffer);

end;

//convert data to readable ascii suitable for logging

AsciiBuffer := ConvertDataToAscii(@Buf, Result);

//convert data to readable hex suitable for logging

HexBuffer := ConvertDataToHex(@Buf, Result);

end;

function sendHookProc(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;

var

AsciiBuffer: string;

HexBuffer: string;

DataBuffer: pchar;

begin

Result := 0;

//save the socket so we can send data too

DataSocket := s;

//allocate memory for our copy of the data

GetMem(DataBuffer, Result);

try

//get our copy of the data

CopyMemory(DataBuffer, @Buf, Result);

//using the data as a byte array

DataBuffer[0] := chr(10); //changing first byte

DataBuffer[1] := chr(20); //changing second byte

DataBuffer[2] := chr(30); //changing thrid byte

//using the data as a pointer to other data sizes

word(pointer(DataBuffer)^) := 10; //changing first 2 bytes

dword(pointer(integer(DataBuffer) + 2)^) := 20; //changing next 4 bytes

word(pointer(integer(DataBuffer) + 6)^) := 30; //changing next 2 bytes

//overwrite the original data with our new data

CopyMemory(@Buf, DataBuffer, Result);

finally

FreeMem(DataBuffer);

end;

//convert data to readable ascii suitable for logging

AsciiBuffer := ConvertDataToAscii(@Buf, Result);

//convert data to readable hex suitable for logging

HexBuffer := ConvertDataToHex(@Buf, Result);

//call the real winsock function

Result := sendNextHook(s, Buf, len, flags);

end;

procedure EntryPoint(Reason: dword); stdcall;

var

lpFileName: array [0..MAX_PATH - 1] of char;

StartInfo: TStartupInfo;

ProcInfo: TProcessInformation;

begin

if Reason = DLL_PROCESS_ATTACH then

begin

//check if we are injected inside the target

if lstrcmpi(pchar(Copy(ParamStr(0), Length(ParamStr(0)) - Length(szTargetExe) + 1, Length(szTargetExe))), pchar(szTargetExe)) = 0 then

begin

//if we are then we hook the needed functions

DataSocket := 0;

HookCode(@send, @sendHookProc, @sendNextHook);

HookCode(@recv, @recvHookProc, @recvNextHook);

end

else

begin

//if not then load the target and inject ourself

GetModuleFileName(hInstance, @lpFileName, MAX_PATH);

ZeroMemory(@StartInfo, SizeOf(TStartupInfo));

ZeroMemory(@ProcInfo, SizeOf(TProcessInformation));

StartInfo.dwFlags := STARTF_USESHOWWINDOW;

StartInfo.wShowWindow := SW_SHOW;

CreateProcess(PChar(ExtractFilePath(lpFileName) + szTargetExe), nil, nil, nil, False, 0, nil, nil, StartInfo, ProcInfo);

Sleep(3000);

InjectLibrary(ProcInfo.hProcess, lpFileName);

end;

end;

end;

begin

DLLProc := @EntryPoint;

EntryPoint(DLL_PROCESS_ATTACH);

end.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有