. EasyHook 라이브러리를 사용하여 C#에서 네이티브 소켓 API 호출을 후크해야합니다. connect, send 및 recv에 후크를 배치하면 Windows 기반 응용 프로그램에서 모든 트래픽을 연결할 수 있습니다. private IntPtr _socketsLib;
private LocalHook _createConnectHook;
private LocalHook _createRecvHook;
private LocalHook _createSendHook;
_socketsLib = NativeAPI.LoadLibrary("Ws2_32.dll");
_createConnectHook = LocalHook.Create(LocalHook.GetProcAddress("Ws2_32.dll", "connect"), new NativeSocketMethod.DConnect(connect_Hooked), this);
_createRecvHook = LocalHook.Create(LocalHook.GetProcAddress("Ws2_32.dll", "recv"),
new NativeSocketMethod.Drecv(recv_Hooked), this);
_createSendHook = LocalHook.Create(LocalHook.GetProcAddress("Ws2_32.dll", "send"),
new NativeSocketMethod.Dsend(send_Hooked), this);
_createConnectHook.ThreadACL.SetExclusiveACL(new int[1]);
_createRecvHook.ThreadACL.SetExclusiveACL(new int[1]);
_createSendHook.ThreadACL.SetExclusiveACL(new int[1]);
private static int connect_Hooked(IntPtr socketHandle, ref NativeSocketMethod.sockaddr name, ref int namelen)
{
// TODO: do something with data here
return NativeSocketMethod.connect(socketHandle, ref name, ref namelen);
}
private static int recv_Hooked(IntPtr socketHandle, IntPtr buf, int count, int socketFlags)
{
// TODO: do something with data here
return NativeSocketMethod.recv(socketHandle, buf, count, socketFlags);
}
private static int send_Hooked(IntPtr socketHandle, IntPtr buf, int count, int socketFlags)
{
// TODO: do something with data here
return NativeSocketMethod.send(socketHandle, buf, count, socketFlags);
}
그리고 NativeSocketMethod.cs
public static class NativeSocketMethod
{
[DllImport("Ws2_32.dll")]
public static extern int connect(IntPtr socketHandle, ref sockaddr Address, ref int Addresslen);
[DllImport("Ws2_32.dll")]
public static extern int getpeername(IntPtr s, ref sockaddr Address, ref int namelen);
[DllImport("ws2_32.dll")]
public static extern IntPtr inet_ntoa(in_addr a);
[DllImport("ws2_32.dll")]
public static extern ushort ntohs(ushort netshort);
[DllImport("Ws2_32.dll")]
public static extern int recv(IntPtr socketHandle, IntPtr buf, int Buffercount, int socketFlags);
[DllImport("Ws2_32.dll")]
public static extern int send(IntPtr socketHandle, IntPtr buf, int count, int socketFlags);
public enum AddressFamily
{
AppleTalk = 0x11,
BlueTooth = 0x20,
InterNetworkv4 = 2,
InterNetworkv6 = 0x17,
Ipx = 4,
Irda = 0x1a,
NetBios = 0x11,
Unknown = 0
}
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true)]
public delegate int DConnect(IntPtr socketHandle, ref NativeSocketMethod.sockaddr Address, ref int Addresslen);
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true)]
public delegate int Drecv(IntPtr socketHandle, IntPtr buf, int Buffercount, int socketFlags);
[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true)]
public delegate int Dsend(IntPtr socketHandle, IntPtr buf, int count, int socketFlags);
[StructLayout(LayoutKind.Sequential)]
public struct in_addr
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
public byte[] sin_addr;
}
public enum ProtocolType
{
BlueTooth = 3,
ReliableMulticast = 0x71,
Tcp = 6,
Udp = 0x11
}
[StructLayout(LayoutKind.Sequential)]
public struct sockaddr
{
public short sin_family;
public ushort sin_port;
public NativeSocketMethod.in_addr sin_addr;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public byte[] sin_zero;
}
public enum SocketType
{
Unknown,
Stream,
DGram,
Raw,
Rdm,
SeqPacket
}
}
와이어 샤크를 사용해보십시오 : 여기
은 예입니다? – Amy달성하려는 목표는 무엇입니까? 개발 중에 분석 할 패킷을 보려면 @ yodaj007이 (가) 가야합니다! 런타임 중에 뭔가를해야한다면 api-hooking으로 들어가기 전에 pcap 라이브러리를 고려해야합니다. – TCS
wireshark이 첫 번째 옵션 이었지만 모든 무선 카드가 promiscous 모드로 설정되지는 않았습니다. (예를 들어 광산입니다.) 모든 Windows PC에서 프로그램을 실행해야합니다. 다른 솔루션을 사용하고 싶지만 아무 것도 내 마음에 오지 않습니다 : 대안을 제안하는 것이 자유 롭습니다 :) – kaharas