[原创]NtSocket的稳定实现,Client与Server的简单封装,以及SocketAsyncSelect的一种APC实现
2022-9-8 23:46:0 Author: bbs.pediy.com(查看原文) 阅读量:5 收藏

最基础的内容,利用NtCreateFile创建socket句柄:

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

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

SOCKET WSPSocket(

int AddressFamily,

int SocketType,

int Protocol) {

/// <summary>

/// 类似于Socket函数,可以创建一个Socket文件句柄

/// </summary>

/// <param name="AddressFamily">Address family(Support IPv6)</param>

/// <param name="SocketType">Socket Type</param>

/// <param name="Protocol">Protocol type</param>

/// <returns>如果失败返回INVALID_SOCKET,成功返回Socket文件句柄</returns>

if (AddressFamily == AF_UNSPEC && SocketType == 0 && Protocol == 0) {

   return INVALID_SOCKET;

}

//进行基础数据设置

if (AddressFamily == AF_UNSPEC) {

   AddressFamily = AF_INET;

}

if (SocketType == 0)

{

   switch (Protocol)

   {

   case IPPROTO_TCP:

       SocketType = SOCK_STREAM;

       break;

   case IPPROTO_UDP:

       SocketType = SOCK_DGRAM;

       break;

   case IPPROTO_RAW:

       SocketType = SOCK_RAW;

       break;

   default:

       SocketType = SOCK_STREAM;

       break;

   }

}

if (Protocol == 0)

{

   switch (SocketType)

   {

   case SOCK_STREAM:

       Protocol = IPPROTO_TCP;

       break;

   case SOCK_DGRAM:

       Protocol = IPPROTO_UDP;

       break;

   case SOCK_RAW:

       Protocol = IPPROTO_RAW;

       break;

   default:

       Protocol = IPPROTO_TCP;

       break;

   }

}

byte EaBuffer[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1E, 0x00,

   0x41, 0x66, 0x64, 0x4F, 0x70, 0x65, 0x6E, 0x50,

   0x61, 0x63, 0x6B, 0x65, 0x74, 0x58, 0x58, 0x00,

   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

   0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,

   0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

   0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

memmove((PVOID)((__int64)EaBuffer + 32), &AddressFamily, 0x4);

memmove((PVOID)((__int64)EaBuffer + 36), &SocketType, 0x4);

memmove((PVOID)((__int64)EaBuffer + 40), &Protocol, 0x4);

if (Protocol == IPPROTO_UDP)

{

   memmove((PVOID)((__int64)EaBuffer + 24), &Protocol, 0x4);

}

//初始化UNICODE_STRING:

UNICODE_STRING AfdName;

AfdName.Buffer = L"\\Device\\Afd\\Endpoint";

AfdName.Length = 2 * wcslen(AfdName.Buffer);

AfdName.MaximumLength = AfdName.Length + 2;

OBJECT_ATTRIBUTES  Object;

IO_STATUS_BLOCK IOSB;

//初始化OBJECT_ATTRIBUTES

InitializeObjectAttributes(&Object,

   &AfdName,

   OBJ_CASE_INSENSITIVE | OBJ_INHERIT,

   0,

   0);

HANDLE MySock;

NTSTATUS Status;

//创建AfdSocket:

Status = ((NtCreateFile)MyNtCreateFile)(&MySock,

   GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,

   &Object,

   &IOSB,

   NULL,

   0,

   FILE_SHARE_READ | FILE_SHARE_WRITE,

   FILE_OPEN_IF,

   0,

   EaBuffer,

   sizeof(EaBuffer));

if (Status != STATUS_SUCCESS) {

   return INVALID_SOCKET;

}

else {

   return (SOCKET)MySock;

}

}


文章来源: https://bbs.pediy.com/thread-274332.htm
如有侵权请联系:admin#unsafe.sh