首页
社区
课程
招聘
C# hook connect 老是返回-1(已解决)
发表于: 2021-6-10 16:36 4669

C# hook connect 老是返回-1(已解决)

2021-6-10 16:36
4669

使用easyhook对ws2_32.dll中的connect进行hook,部分代码如下:

 

[DllImport("WS2_32.dll", SetLastError = true)]
public static extern int connect(IntPtr s, IntPtr addr, int addrsize);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    [UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
    delegate int Dconnect(IntPtr s, IntPtr addr, int addrsize);
 
    int connect_Hooked(IntPtr s, IntPtr addr, int addrsize)
    {
 
        sockaddr_in structure = (sockaddr_in)Marshal.PtrToStructure(addr, typeof(sockaddr_in));
        string remoteIp = new System.Net.IPAddress(structure.sin_addr.S_addr).ToString();
        if (remoteIp.Contains("121.138.16.15"))
        {
 
               SetAddr(addr, "127.0.0.1", "6448");
 
        }
 
 
        var res = connect(s, addr, addrsize);
         //res始终是-1,不知何解?????
 
        return res;
 
    }
public struct sockaddr_in
    {
        public const int Size = 16;
        public short sin_family;
        public ushort sin_port;
        public struct in_addr
        {
            public uint S_addr;
            public struct _S_un_b
            {
                public byte s_b1, s_b2, s_b3, s_b4;
            }
            public _S_un_b S_un_b;
            public struct _S_un_w
            {
                public ushort s_w1, s_w2;
            }
            public _S_un_w S_un_w;
        }
        public in_addr sin_addr;
    }
 
    private void SetAddr(IntPtr addr, string ip, string port)
    {
        sockaddr_in structure = (sockaddr_in)Marshal.PtrToStructure(addr, typeof(sockaddr_in));
        structure.sin_addr.S_addr = inet_addr(ip);
        structure.sin_port = htons(Convert.ToUInt16(port));
        Marshal.StructureToPtr(structure, addr, true);
        structure = (sockaddr_in)Marshal.PtrToStructure(addr, typeof(sockaddr_in));
    }

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

最后于 2021-6-11 12:07 被yzslly编辑 ,原因:
收藏
免费 1
支持
分享
最新回复 (1)
雪    币: 136
活跃值: (429)
能力值: ( LV9,RANK:170 )
在线值:
发帖
回帖
粉丝
2
https://blog.csdn.net/a923169932/article/details/105159383/
2021-6-11 12:08
0
游客
登录 | 注册 方可回帖
返回
//