转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Delphi Winsock Hooking Example by Aphex         ★★★★

Delphi Winsock Hooking Example by Aphex

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1155 更新时间:2009/4/23 18:41:47

{
  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.


[系统软件]InstallShield Express for delphi制作安装程序定…  [系统软件]Oracle导数据脚本exp_example.par
[常用软件]InstallShield Express制作Delphi数据库安装程序  [常用软件]在POWERBUILDER中使用WINSOCK控件的方法
[VB.NET程序]如何在vb 中用api函数代替winsock控件建立网络连接…  [Delphi程序]为什么选择Delphi.Net ?
[Delphi程序]《关于VisiBroker For Delphi的使用》(4)  [Delphi程序]Delphi 程序员代码编写标准指南
[Delphi程序]转贴:Conversion to Delphi 6: Missing unit Pro…  [Delphi程序]Borland Delphi 9 的新特性
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台