{"id":126,"date":"2022-04-01T19:33:33","date_gmt":"2022-04-01T11:33:33","guid":{"rendered":"http:\/\/www.aqwu.net\/wp\/?p=126"},"modified":"2022-04-10T21:58:08","modified_gmt":"2022-04-10T13:58:08","slug":"ntsockets-%e4%bd%bf%e7%94%a8-ntcreatefile-%e5%92%8c-ntdeviceiocontrolfile-%e7%b3%bb%e7%bb%9f%e8%b0%83%e7%94%a8%e9%80%9a%e8%bf%87-http-%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=126","title":{"rendered":"NTSockets &#8211; \u4f7f\u7528 NtCreateFile \u548c NtDeviceIoControlFile \u7cfb\u7edf\u8c03\u7528\u901a\u8fc7 HTTP \u4e0b\u8f7d\u6587\u4ef6"},"content":{"rendered":"\n<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/www.x86matthew.com\/view_post?id=ntsockets<\/p>\n\n\n\n<p>\u8fd9\u7bc7\u6587\u7ae0\u6f14\u793a\u4e86\u5982\u4f55\u4ec5\u4f7f\u7528 ntdll \u5bfc\u51fa\u6765\u521b\u5efa TCP \u5957\u63a5\u5b57\u548c\u4f20\u8f93\/\u63a5\u6536\u6570\u636e\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.x86matthew.com\/img\/ntsockets.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u6211\u4eec\u5c06\u4f7f\u7528\u7684\u51fd\u6570\u662fNtCreateFile\u548cNtDeviceIoControlFile\u3002Winsock \u5e93\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u4e0e AFD \u9a71\u52a8\u7a0b\u5e8f\u8fdb\u884c\u901a\u4fe1\uff0c\u4f46\u6211\u4eec\u5c06\u7ed5\u8fc7 Winsock \u5e76\u76f4\u63a5\u8c03\u7528\u5b83\u4eec\u3002\u8fd9\u4e9b\u51fd\u6570\u4e5f\u53ef\u4ee5\u4f7f\u7528\u76f4\u63a5\u7cfb\u7edf\u8c03\u7528\u6307\u4ee4\u6765\u8c03\u7528\uff0c\u4ee5\u589e\u52a0\u5bf9\u7528\u6237\u6a21\u5f0f\u6302\u94a9\u7684\u9690\u853d\u6027\uff0c\u5c3d\u7ba1\u5728\u5185\u6838\u7ea7\u522b\u68c0\u6d4b\u7f51\u7edc\u6d41\u91cf\u4ecd\u7136\u5f88\u5bb9\u6613\u3002\u4e3a\u4e86\u6f14\u793a\u8fd9\u4e2a\u6982\u5ff5\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u901a\u8fc7 HTTP \u4e0b\u8f7d\u6587\u4ef6\u7684\u5de5\u5177\u3002<\/p>\n\n\n\n<p>\u6211\u53ea\u5bf9\u8fd9\u4e2a\u6982\u5ff5\u9a8c\u8bc1\u6240\u9700\u7684\u5185\u90e8 AFD \u6570\u636e\u7ed3\u6784\u8fdb\u884c\u4e86\u9006\u5411\u5de5\u7a0b\u3002\u6211\u5e0c\u671b\u53ef\u4ee5\u5728\u5176\u4ed6\u5730\u65b9\u627e\u5230\u6709\u5173 AFD \u7ed3\u6784\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57\uff0c\u6211\u4eec\u8c03\u7528NtCreateFile\u6765\u6253\u5f00\\Device\\Afd\\Endpoint\u5bf9\u8c61\u3002\u5957\u63a5\u5b57\u5c5e\u6027\uff08\u5730\u5740\u65cf\u3001\u534f\u8bae\u7c7b\u578b\u7b49\uff09\u662f\u4f7f\u7528\u672a\u8bb0\u5f55\u7684\u7ed3\u6784\u6307\u5b9a\u7684\uff0c\u8be5\u7ed3\u6784\u4f5c\u4e3a\u201c\u6269\u5c55\u5c5e\u6027\u201d\u4f20\u9012\u7ed9NtCreateFile \u3002\u6211\u5df2\u7ecf\u786c\u7f16\u7801\u8fd9\u4e9b\u5c5e\u6027\u6765\u521b\u5efa\u4e00\u4e2a IPv4 TCP \u5957\u63a5\u5b57\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DWORD NTSockets_CreateTcpSocket(NTSockets_SocketDataStruct *pSocketData)\n{\n\tIO_STATUS_BLOCK IoStatusBlock;\n\tHANDLE hEvent = NULL;\n\tHANDLE hSocket = NULL;\n\tOBJECT_ATTRIBUTES ObjectAttributes;\n\tNTSockets_SocketDataStruct SocketData;\n\tUNICODE_STRING ObjectFilePath;\n\tDWORD dwStatus = 0;\n\tBYTE bExtendedAttributes&#91;] =\n\t{\n\t\t0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1E, 0x00, 0x41, 0x66, 0x64, 0x4F, 0x70, 0x65, 0x6E, 0x50,\n\t\t0x61, 0x63, 0x6B, 0x65, 0x74, 0x58, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n\t\t0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n\t\t0x00, 0x00, 0x00, 0x00, 0x60, 0xEF, 0x3D, 0x47, 0xFE\n\t};\n\n\t\/\/ create status event\n\thEvent = CreateEvent(NULL, 0, 0, NULL);\n\tif(hEvent == NULL)\n\t{\n\t\t\/\/ error\n\t\treturn 1;\n\t}\n\n\t\/\/ set afd endpoint path\n\tmemset((void*)&amp;ObjectFilePath, 0, sizeof(ObjectFilePath));\n\tObjectFilePath.Buffer = L\"\\\\Device\\\\Afd\\\\Endpoint\";\n\tObjectFilePath.Length = wcslen(ObjectFilePath.Buffer) * sizeof(wchar_t);\n\tObjectFilePath.MaximumLength = ObjectFilePath.Length;\n\n\t\/\/ initialise object attributes\n\tmemset((void*)&amp;ObjectAttributes, 0, sizeof(ObjectAttributes));\n\tObjectAttributes.Length = sizeof(ObjectAttributes);\n\tObjectAttributes.ObjectName = &amp;ObjectFilePath;\n\tObjectAttributes.Attributes = 0x40;\n\n\t\/\/ create socket handle\n\tIoStatusBlock.Status = 0;\n\tIoStatusBlock.Information = NULL;\n\tdwStatus = NtCreateFile(&amp;hSocket, 0xC0140000, &amp;ObjectAttributes, &amp;IoStatusBlock, NULL, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 1, 0, bExtendedAttributes, sizeof(bExtendedAttributes));\n\tif(dwStatus != 0)\n\t{\n\t\t\/\/ error\n\t\tCloseHandle(hEvent);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ initialise SocketData object\n\tmemset((void*)&amp;SocketData, 0, sizeof(SocketData));\n\tSocketData.hSocket = hSocket;\n\tSocketData.hStatusEvent = hEvent;\n\n\t\/\/ store socket data\n\tmemcpy((void*)pSocketData, (void*)&amp;SocketData, sizeof(SocketData));\n\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u6709\u4e86\u4e00\u4e2a\u6709\u6548\u7684\u5957\u63a5\u5b57\u53e5\u67c4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NtDeviceIoControlFile\u4e0e AFD \u9a71\u52a8\u7a0b\u5e8f\u8fdb\u884c\u901a\u4fe1\u3002\u6211\u521b\u5efa\u4e86\u4ee5\u4e0b\u5904\u7406 AFD \u9a71\u52a8\u7a0b\u5e8f\u6d88\u606f\u7684\u901a\u7528\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DWORD NTSockets_SocketDriverMsg(NTSockets_SocketDataStruct *pSocketData, DWORD dwIoControlCode, BYTE *pData, DWORD dwLength, DWORD *pdwOutputInformation)\n{\n\tIO_STATUS_BLOCK IoStatusBlock;\n\tDWORD dwStatus = 0;\n\tBYTE bOutputBlock&#91;0x10];\n\n\t\/\/ reset status event\n\tResetEvent(pSocketData->hStatusEvent);\n\n\t\/\/ send device control request\n\tIoStatusBlock.Status = 0;\n\tIoStatusBlock.Information = NULL;\n\tdwStatus = NtDeviceIoControlFile(pSocketData->hSocket, pSocketData->hStatusEvent, NULL, NULL, &amp;IoStatusBlock, dwIoControlCode, (void*)pData, dwLength, bOutputBlock, sizeof(bOutputBlock));\n\tif(dwStatus == STATUS_PENDING)\n\t{\n\t\t\/\/ response pending - wait for event\n\t\tif(WaitForSingleObject(pSocketData->hStatusEvent, INFINITE) != WAIT_OBJECT_0)\n\t\t{\n\t\t\t\/\/ error\n\t\t\treturn 1;\n\t\t}\n\n\t\t\/\/ complete - get final status code\n\t\tdwStatus = IoStatusBlock.Status;\n\t}\n\n\t\/\/ check for errors\n\tif(dwStatus != 0)\n\t{\n\t\t\/\/ error\n\t\treturn 1;\n\t}\n\n\tif(pdwOutputInformation != NULL)\n\t{\n\t\t\/\/ store output info\n\t\t*pdwOutputInformation = (DWORD)IoStatusBlock.Information;\n\t}\n\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684dwIoControlCode\u503c\u8c03\u7528NTSockets_SocketDriverMsg\u6765\u6267\u884c\u6211\u4eec\u60f3\u8981\u6267\u884c\u7684\u64cd\u4f5c &#8211; \u8fde\u63a5\u3001\u53d1\u9001\u3001\u63a5\u6536\u7b49\u3002\u5982\u679c\u4e8b\u4ef6\u5bf9\u8c61\u8fd4\u56de\u4e00\u4e2a\u6302\u8d77\u7684\u72b6\u6001\u4ee3\u7801\uff0c\u5219\u7b49\u5f85\u51fd\u6570\u5b8c\u6210\u3002\u00a0\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528CloseHandle\uff08\u6216NtClose\uff09\u5173\u95ed\u5957\u63a5\u5b57\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DWORD NTSockets_CloseSocket(NTSockets_SocketDataStruct *pSocketData)\n{\n\t\/\/ close handles\n\tCloseHandle(pSocketData->hSocket);\n\tCloseHandle(pSocketData->hStatusEvent);\n\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p>\u6211\u521b\u5efa\u4e86\u4ee5\u4e0b\u51fd\u6570\u5e93\uff0c\u8fd9\u4e9b\u51fd\u6570\u5e93\u6267\u884c\u6b64\u6982\u5ff5\u9a8c\u8bc1\u6240\u9700\u7684\u6240\u6709\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<p>NTSockets_CreateTcpSocket&nbsp;&#8211; \u521b\u5efa TCP \u5957\u63a5\u5b57\uff08\u76f8\u5f53\u4e8esocket()\uff09<br>NTSockets_ConvertIP&nbsp;&#8211; \u5c06 IP \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5730\u5740\uff08\u76f8\u5f53\u4e8eto inet_addr&nbsp;()&nbsp;)<br>NTSockets_Swap16BitByteOrder&nbsp;&#8211; \u5c06 16 \u4f4d\u6574\u6570\u8f6c\u6362\u4e3a\/\u4ece\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\uff08\u76f8\u5f53\u4e8ehtons()&nbsp;\/&nbsp;ntohs()\uff09<br>NTSockets_Connect&nbsp;&#8211; \u8fde\u63a5\u5230\u8fdc\u7a0b\u4e3b\u673a\uff08\u76f8\u5f53\u4e8econnect()\uff09<br>NTSockets_Send&nbsp;&#8211; \u5c06\u6570\u636e\u53d1\u9001\u5230\u5957\u63a5\u5b57\uff08\u76f8\u5f53\u4e8e\u53d1\u9001\uff08\uff09- \u6ce8\u610f\uff1a\u5728\u53d1\u9001\u5b8c\u6240\u6709\u5b57\u8282\u540e\uff0c\u8be5\u51fd\u6570\u4e0d\u4f1a\u8fd4\u56de\uff09<br>NTSockets_Recv&nbsp;&#8211; \u4ece\u5957\u63a5\u5b57\u63a5\u6536\u8bf7\u6c42\u7684\u5b57\u8282\u6570\uff08\u76f8\u5f53\u4e8erecv()&nbsp;&#8211; \u6ce8\u610f\uff1a\u5728\u63a5\u6536\u5230\u6240\u6709\u5b57\u8282\u4e4b\u524d\uff0c\u8be5\u51fd\u6570\u4e0d\u4f1a\u8fd4\u56de\uff09<br>NTSockets_CloseSocket&nbsp;&#8211; \u5173\u95ed\u5957\u63a5\u5b57\uff08\u76f8\u5f53\u4e8eclosesocket()&nbsp;)<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u8fd9\u4e2a\u6982\u5ff5\u9a8c\u8bc1\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684 HTTP \u5ba2\u6237\u7aef\u6765\u4ece\u8fdc\u7a0b Web \u670d\u52a1\u5668\u4e0b\u8f7d\u6587\u4ef6\u3002\u8fd9\u4e0d\u652f\u6301 HTTPS \u6216 301\/302 \u72b6\u6001\u91cd\u5b9a\u5411\u7b49<\/p>\n\n\n\n<p>\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u8fd8\u521b\u5efa\u4e86\u4e00\u4e2a\u57fa\u672c\u7684 DNS \u5ba2\u6237\u7aef\u6765\u6267\u884c\u540d\u79f0\u67e5\u627e &#8211; \u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528gethostbyname()\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd9\u662f\u6e29\u7d22\u514b\u56fe\u4e66\u9986\u3002DNS \u670d\u52a1\u5668\u5f53\u524d\u88ab\u786c\u7f16\u7801\u4e3a8.8.8.8\uff0c\u4f46\u5982\u679c\u613f\u610f\uff0c\u60a8\u53ef\u4ee5\u4ece\u6ce8\u518c\u8868\u4e2d\u8bfb\u53d6\u9ed8\u8ba4 DNS \u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;windows.h>\n\nstruct IO_STATUS_BLOCK\n{\n\tunion\n\t{\n\t\tDWORD Status;\n\t\tPVOID Pointer;\n\t};\n\n\tDWORD *Information;\n};\n\nstruct UNICODE_STRING\n{\n\tUSHORT Length;\n\tUSHORT MaximumLength;\n\tPWSTR Buffer;\n};\n\nstruct OBJECT_ATTRIBUTES\n{\n\tULONG Length;\n\tHANDLE RootDirectory;\n\tUNICODE_STRING *ObjectName;\n\tULONG Attributes;\n\tPVOID SecurityDescriptor;\n\tPVOID SecurityQualityOfService;\n};\n\nstruct NTSockets_ConnectDataStruct\n{\n\tDWORD dwUnknown1;\n\tDWORD dwUnknown2;\n\tDWORD dwUnknown3;\n\tsockaddr_in SockAddr;\n};\n\nstruct NTSockets_BindDataStruct\n{\n\tDWORD dwUnknown1;\n\tsockaddr_in SockAddr;\n};\n\nstruct NTSockets_DataBufferStruct\n{\n\tDWORD dwDataLength;\n\tBYTE *pData;\n};\n\nstruct NTSockets_SendRecvDataStruct\n{\n\tNTSockets_DataBufferStruct *pBufferList;\n\tDWORD dwBufferCount;\n\tDWORD dwUnknown1;\n\tDWORD dwUnknown2;\n};\n\nstruct NTSockets_SocketDataStruct\n{\n\tHANDLE hSocket;\n\tHANDLE hStatusEvent;\n};\n\nstruct DNSClient_HeaderStruct\n{\n\tWORD wTransID;\n\tWORD wFlags;\n\tWORD wQuestionCount;\n\tWORD wAnswerRecordCount;\n\tWORD wAuthorityRecordCount;\n\tWORD wAdditionalRecordCount;\n};\n\nstruct DNSClient_RequestQueryDetailsStruct\n{\n\tWORD wType;\n\tWORD wClass;\n};\n\nstruct DNSClient_ResponseAnswerHeaderStruct\n{\n\tWORD wName;\n\tWORD wType;\n\tWORD wClass;\n\tWORD wTTL&#91;2];\n\tWORD wLength;\n};\n\nDWORD (WINAPI *NtDeviceIoControlFile)(HANDLE FileHandle, HANDLE Event, VOID *ApcRoutine, PVOID ApcContext, IO_STATUS_BLOCK *IoStatusBlock, ULONG IoControlCode, PVOID InputBuffer, ULONG InputBufferLength, PVOID OutputBuffer, ULONG OutputBufferLength);\nDWORD (WINAPI *NtCreateFile)(PHANDLE FileHandle, ACCESS_MASK DesiredAccess, OBJECT_ATTRIBUTES *ObjectAttributes, IO_STATUS_BLOCK *IoStatusBlock, LARGE_INTEGER *AllocationSize, ULONG FileAttributes, ULONG ShareAccess, ULONG CreateDisposition, ULONG CreateOptions, PVOID EaBuffer, ULONG EaLength);\n\nDWORD NTSockets_CreateTcpSocket(NTSockets_SocketDataStruct *pSocketData)\n{\n\tIO_STATUS_BLOCK IoStatusBlock;\n\tHANDLE hEvent = NULL;\n\tHANDLE hSocket = NULL;\n\tOBJECT_ATTRIBUTES ObjectAttributes;\n\tNTSockets_SocketDataStruct SocketData;\n\tUNICODE_STRING ObjectFilePath;\n\tDWORD dwStatus = 0;\n\tBYTE bExtendedAttributes&#91;] =\n\t{\n\t\t0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x1E, 0x00, 0x41, 0x66, 0x64, 0x4F, 0x70, 0x65, 0x6E, 0x50,\n\t\t0x61, 0x63, 0x6B, 0x65, 0x74, 0x58, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n\t\t0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n\t\t0x00, 0x00, 0x00, 0x00, 0x60, 0xEF, 0x3D, 0x47, 0xFE\n\t};\n\n\t\/\/ create status event\n\thEvent = CreateEvent(NULL, 0, 0, NULL);\n\tif(hEvent == NULL)\n\t{\n\t\t\/\/ error\n\t\treturn 1;\n\t}\n\n\t\/\/ set afd endpoint path\n\tmemset((void*)&amp;ObjectFilePath, 0, sizeof(ObjectFilePath));\n\tObjectFilePath.Buffer = L\"\\\\Device\\\\Afd\\\\Endpoint\";\n\tObjectFilePath.Length = wcslen(ObjectFilePath.Buffer) * sizeof(wchar_t);\n\tObjectFilePath.MaximumLength = ObjectFilePath.Length;\n\n\t\/\/ initialise object attributes\n\tmemset((void*)&amp;ObjectAttributes, 0, sizeof(ObjectAttributes));\n\tObjectAttributes.Length = sizeof(ObjectAttributes);\n\tObjectAttributes.ObjectName = &amp;ObjectFilePath;\n\tObjectAttributes.Attributes = 0x40;\n\n\t\/\/ create socket handle\n\tIoStatusBlock.Status = 0;\n\tIoStatusBlock.Information = NULL;\n\tdwStatus = NtCreateFile(&amp;hSocket, 0xC0140000, &amp;ObjectAttributes, &amp;IoStatusBlock, NULL, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 1, 0, bExtendedAttributes, sizeof(bExtendedAttributes));\n\tif(dwStatus != 0)\n\t{\n\t\t\/\/ error\n\t\tCloseHandle(hEvent);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ initialise SocketData object\n\tmemset((void*)&amp;SocketData, 0, sizeof(SocketData));\n\tSocketData.hSocket = hSocket;\n\tSocketData.hStatusEvent = hEvent;\n\n\t\/\/ store socket data\n\tmemcpy((void*)pSocketData, (void*)&amp;SocketData, sizeof(SocketData));\n\n\treturn 0;\n}\n\nDWORD NTSockets_SocketDriverMsg(NTSockets_SocketDataStruct *pSocketData, DWORD dwIoControlCode, BYTE *pData, DWORD dwLength, DWORD *pdwOutputInformation)\n{\n\tIO_STATUS_BLOCK IoStatusBlock;\n\tDWORD dwStatus = 0;\n\tBYTE bOutputBlock&#91;0x10];\n\n\t\/\/ reset status event\n\tResetEvent(pSocketData->hStatusEvent);\n\n\t\/\/ send device control request\n\tIoStatusBlock.Status = 0;\n\tIoStatusBlock.Information = NULL;\n\tdwStatus = NtDeviceIoControlFile(pSocketData->hSocket, pSocketData->hStatusEvent, NULL, NULL, &amp;IoStatusBlock, dwIoControlCode, (void*)pData, dwLength, bOutputBlock, sizeof(bOutputBlock));\n\tif(dwStatus == STATUS_PENDING)\n\t{\n\t\t\/\/ response pending - wait for event\n\t\tif(WaitForSingleObject(pSocketData->hStatusEvent, INFINITE) != WAIT_OBJECT_0)\n\t\t{\n\t\t\t\/\/ error\n\t\t\treturn 1;\n\t\t}\n\n\t\t\/\/ complete - get final status code\n\t\tdwStatus = IoStatusBlock.Status;\n\t}\n\n\t\/\/ check for errors\n\tif(dwStatus != 0)\n\t{\n\t\t\/\/ error\n\t\treturn 1;\n\t}\n\n\tif(pdwOutputInformation != NULL)\n\t{\n\t\t\/\/ store output info\n\t\t*pdwOutputInformation = (DWORD)IoStatusBlock.Information;\n\t}\n\n\treturn 0;\n}\n\nDWORD NTSockets_ConvertIP(char *pIP, DWORD *pdwAddr)\n{\n\tchar szCurrOctet&#91;8];\n\tDWORD dwCurrOctetIndex = 0;\n\tDWORD dwCompletedOctetCount = 0;\n\tchar *pCurrByte = NULL;\n\tDWORD dwEndOfOctet = 0;\n\tDWORD dwEndOfString = 0;\n\tDWORD dwOctet = 0;\n\tBYTE bOctets&#91;4];\n\tDWORD dwAddr = 0;\n\n\t\/\/ read IP string\n\tmemset(szCurrOctet, 0, sizeof(szCurrOctet));\n\tdwCurrOctetIndex = 0;\n\tpCurrByte = pIP;\n\tfor(;;)\n\t{\n\t\t\/\/ process current character\n\t\tdwEndOfOctet = 0;\n\t\tif(*pCurrByte == '\\0')\n\t\t{\n\t\t\t\/\/ end of string\n\t\t\tdwEndOfOctet = 1;\n\t\t\tdwEndOfString = 1;\n\t\t}\n\t\telse if(*pCurrByte == '.')\n\t\t{\n\t\t\t\/\/ end of octet\n\t\t\tdwEndOfOctet = 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\t\/\/ ensure this character is a number\n\t\t\tif(*pCurrByte >= '0' &amp;&amp; *pCurrByte &lt;= '9')\n\t\t\t{\n\t\t\t\tif(dwCurrOctetIndex > 2)\n\t\t\t\t{\n\t\t\t\t\t\/\/ invalid ip\n\t\t\t\t\treturn 1;\n\t\t\t\t}\n\n\t\t\t\t\/\/ store current character\n\t\t\t\tszCurrOctet&#91;dwCurrOctetIndex] = *pCurrByte;\n\t\t\t\tdwCurrOctetIndex++;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t\/\/ invalid ip\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t}\n\n\t\t\/\/ check if the current octet is complete\n\t\tif(dwEndOfOctet != 0)\n\t\t{\n\t\t\tif(dwCurrOctetIndex == 0)\n\t\t\t{\n\t\t\t\t\/\/ invalid ip\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\t\/\/ convert octet string to integer\n\t\t\tdwOctet = atoi(szCurrOctet);\n\t\t\tif(dwOctet > 255)\n\t\t\t{\n\t\t\t\t\/\/ invalid ip\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\t\/\/ already read 4 octets\n\t\t\tif(dwCompletedOctetCount >= 4)\n\t\t\t{\n\t\t\t\t\/\/ invalid ip\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\t\/\/ store current octet\n\t\t\tbOctets&#91;dwCompletedOctetCount] = (BYTE)dwOctet;\n\n\t\t\t\/\/ current octet complete\n\t\t\tdwCompletedOctetCount++;\n\n\t\t\tif(dwEndOfString != 0)\n\t\t\t{\n\t\t\t\t\/\/ end of string\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\t\/\/ reset szCurrOctet string\n\t\t\tmemset(szCurrOctet, 0, sizeof(szCurrOctet));\n\t\t\tdwCurrOctetIndex = 0;\n\t\t}\n\n\t\t\/\/ move to the next character\n\t\tpCurrByte++;\n\t}\n\n\t\/\/ ensure 4 octets were found\n\tif(dwCompletedOctetCount != 4)\n\t{\n\t\t\/\/ invalid string\n\t\treturn 1;\n\t}\n\n\t\/\/ store octets in dword value\n\tmemcpy((void*)&amp;dwAddr, bOctets, 4);\n\n\t\/\/ store value\n\t*pdwAddr = dwAddr;\n\n\treturn 0;\n}\n\nWORD NTSockets_Swap16BitByteOrder(WORD wValue)\n{\n\tWORD wNewValue = 0;\n\n\t\/\/ swap byte order - this assumes we are running on an x86-based chip\n\t*(BYTE*)((DWORD)&amp;wNewValue + 0) = *(BYTE*)((DWORD)&amp;wValue + 1);\n\t*(BYTE*)((DWORD)&amp;wNewValue + 1) = *(BYTE*)((DWORD)&amp;wValue + 0);\n\n\treturn wNewValue;\n}\n\nDWORD NTSockets_Connect(NTSockets_SocketDataStruct *pSocketData, char *pIP, WORD wPort)\n{\n\tNTSockets_BindDataStruct NTSockets_BindData;\n\tNTSockets_ConnectDataStruct NTSockets_ConnectData;\n\tWORD wConnectPort = 0;\n\tDWORD dwConnectAddr = 0;\n\n\t\/\/ bind to local port\n\tmemset((void*)&amp;NTSockets_BindData, 0, sizeof(NTSockets_BindData));\n\tNTSockets_BindData.dwUnknown1 = 2;\n\tNTSockets_BindData.SockAddr.sin_family = AF_INET;\n\tNTSockets_BindData.SockAddr.sin_addr.s_addr = INADDR_ANY;\n\tNTSockets_BindData.SockAddr.sin_port = 0;\n\tif(NTSockets_SocketDriverMsg(pSocketData, 0x00012003, (BYTE*)&amp;NTSockets_BindData, sizeof(NTSockets_BindData), NULL) != 0)\n\t{\n\t\t\/\/ error\n\t\treturn 1;\n\t}\n\n\t\/\/ read connection ip\n\tif(NTSockets_ConvertIP(pIP, &amp;dwConnectAddr) != 0)\n\t{\n\t\t\/\/ error\n\t\treturn 1;\n\t}\n\n\t\/\/ use network byte order for connection port\n\twConnectPort = NTSockets_Swap16BitByteOrder(wPort);\n\n\t\/\/ connect to remote port\n\tmemset((void*)&amp;NTSockets_ConnectData, 0, sizeof(NTSockets_ConnectData));\n\tNTSockets_ConnectData.dwUnknown1 = 0;\n\tNTSockets_ConnectData.dwUnknown2 = 0;\n\tNTSockets_ConnectData.dwUnknown3 = 0;\n\tNTSockets_ConnectData.SockAddr.sin_family = AF_INET;\n\tNTSockets_ConnectData.SockAddr.sin_addr.s_addr = dwConnectAddr;\n\tNTSockets_ConnectData.SockAddr.sin_port = wConnectPort;\n\tif(NTSockets_SocketDriverMsg(pSocketData, 0x00012007, (BYTE*)&amp;NTSockets_ConnectData, sizeof(NTSockets_ConnectData), NULL) != 0)\n\t{\n\t\t\/\/ error\n\t\treturn 1;\n\t}\n\n\treturn 0;\n}\n\nDWORD NTSockets_Send(NTSockets_SocketDataStruct *pSocketData, BYTE *pData, DWORD dwLength)\n{\n\tNTSockets_SendRecvDataStruct NTSockets_SendRecvData;\n\tNTSockets_DataBufferStruct NTSockets_DataBuffer;\n\tDWORD dwBytesSent = 0;\n\tBYTE *pCurrSendPtr = NULL;\n\tDWORD dwBytesRemaining = 0;\n\n\t\/\/ set initial values\n\tpCurrSendPtr = pData;\n\tdwBytesRemaining = dwLength;\n\n\t\/\/ send data\n\tfor(;;)\n\t{\n\t\tif(dwBytesRemaining == 0)\n\t\t{\n\t\t\t\/\/ finished\n\t\t\tbreak;\n\t\t}\n\n\t\t\/\/ set data buffer values\n\t\tmemset((void*)&amp;NTSockets_DataBuffer, 0, sizeof(NTSockets_DataBuffer));\n\t\tNTSockets_DataBuffer.dwDataLength = dwBytesRemaining;\n\t\tNTSockets_DataBuffer.pData = pCurrSendPtr;\n\n\t\t\/\/ send current block\n\t\tmemset((void*)&amp;NTSockets_SendRecvData, 0, sizeof(NTSockets_SendRecvData));\n\t\tNTSockets_SendRecvData.pBufferList = &amp;NTSockets_DataBuffer;\n\t\tNTSockets_SendRecvData.dwBufferCount = 1;\n\t\tNTSockets_SendRecvData.dwUnknown1 = 0;\n\t\tNTSockets_SendRecvData.dwUnknown2 = 0;\n\t\tif(NTSockets_SocketDriverMsg(pSocketData, 0x0001201F, (BYTE*)&amp;NTSockets_SendRecvData, sizeof(NTSockets_SendRecvData), &amp;dwBytesSent) != 0)\n\t\t{\n\t\t\t\/\/ error\n\t\t\treturn 1;\n\t\t}\n\n\t\tif(dwBytesSent == 0)\n\t\t{\n\t\t\t\/\/ socket disconnected\n\t\t\treturn 1;\n\t\t}\n\n\t\t\/\/ update values\n\t\tpCurrSendPtr += dwBytesSent;\n\t\tdwBytesRemaining -= dwBytesSent;\n\t}\n\n\treturn 0;\n}\n\nDWORD NTSockets_Recv(NTSockets_SocketDataStruct *pSocketData, BYTE *pData, DWORD dwLength)\n{\n\tNTSockets_SendRecvDataStruct NTSockets_SendRecvData;\n\tNTSockets_DataBufferStruct NTSockets_DataBuffer;\n\tDWORD dwBytesReceived = 0;\n\tBYTE *pCurrRecvPtr = NULL;\n\tDWORD dwBytesRemaining = 0;\n\n\t\/\/ set initial values\n\tpCurrRecvPtr = pData;\n\tdwBytesRemaining = dwLength;\n\n\t\/\/ send data\n\tfor(;;)\n\t{\n\t\tif(dwBytesRemaining == 0)\n\t\t{\n\t\t\t\/\/ finished\n\t\t\tbreak;\n\t\t}\n\n\t\t\/\/ set data buffer values\n\t\tmemset((void*)&amp;NTSockets_DataBuffer, 0, sizeof(NTSockets_DataBuffer));\n\t\tNTSockets_DataBuffer.dwDataLength = dwBytesRemaining;\n\t\tNTSockets_DataBuffer.pData = pCurrRecvPtr;\n\n\t\t\/\/ recv current block\n\t\tmemset((void*)&amp;NTSockets_SendRecvData, 0, sizeof(NTSockets_SendRecvData));\n\t\tNTSockets_SendRecvData.pBufferList = &amp;NTSockets_DataBuffer;\n\t\tNTSockets_SendRecvData.dwBufferCount = 1;\n\t\tNTSockets_SendRecvData.dwUnknown1 = 0;\n\t\tNTSockets_SendRecvData.dwUnknown2 = 0x20;\n\t\tif(NTSockets_SocketDriverMsg(pSocketData, 0x00012017, (BYTE*)&amp;NTSockets_SendRecvData, sizeof(NTSockets_SendRecvData), &amp;dwBytesReceived) != 0)\n\t\t{\n\t\t\t\/\/ error\n\t\t\treturn 1;\n\t\t}\n\n\t\tif(dwBytesReceived == 0)\n\t\t{\n\t\t\t\/\/ socket disconnected\n\t\t\treturn 1;\n\t\t}\n\n\t\t\/\/ update values\n\t\tpCurrRecvPtr += dwBytesReceived;\n\t\tdwBytesRemaining -= dwBytesReceived;\n\t}\n\n\treturn 0;\n}\n\nDWORD NTSockets_CloseSocket(NTSockets_SocketDataStruct *pSocketData)\n{\n\t\/\/ close handles\n\tCloseHandle(pSocketData->hSocket);\n\tCloseHandle(pSocketData->hStatusEvent);\n\n\treturn 0;\n}\n\nDWORD DNSClient_Query(char *pDNSClient_IP, char *pTargetHost, char *pOutput, DWORD dwOutputMaxLength)\n{\n\tNTSockets_SocketDataStruct SocketData;\n\tDNSClient_HeaderStruct DNSClient_RequestHeader;\n\tDNSClient_RequestQueryDetailsStruct DNSClient_RequestQueryDetails;\n\tDNSClient_HeaderStruct *pDNSClient_ResponseHeader = NULL;\n\tDNSClient_ResponseAnswerHeaderStruct *pDNSClient_ResponseAnswerHeader = NULL;\n\tDWORD dwIpAddrIndex = 0;\n\tDWORD dwFoundRecord = 0;\n\tDWORD dwCurrAnswerEntryStartIndex = 0;\n\tDWORD dwHostLength = 0;\n\tDWORD dwCurrLabelLength = 0;\n\tWORD wRequestLength = 0;\n\tWORD wResponseLength = 0;\n\tWORD wBlockLength = 0;\n\tWORD wAnswerCount = 0;\n\tBYTE bIP&#91;4];\n\tBYTE bResponseBuffer&#91;4096];\n\tchar szConvertedHost&#91;1024];\n\tchar *pCurrDot = NULL;\n\tchar szIP&#91;32];\n\t\n\t\/\/ convert target host name to dns format\n\tmemset(szConvertedHost, 0, sizeof(szConvertedHost));\n\t_snprintf(szConvertedHost, sizeof(szConvertedHost) - 1, \".%s\", pTargetHost);\n\tdwHostLength = strlen(szConvertedHost) + 1;\n\tfor(DWORD i = 0; i &lt; dwHostLength; i++)\n\t{\n\t\t\/\/ process domain labels\n\t\tif(szConvertedHost&#91;i] == '.' || szConvertedHost&#91;i] == '\\0')\n\t\t{\n\t\t\t\/\/ check if a previous separator exists\n\t\t\tif(pCurrDot != NULL)\n\t\t\t{\n\t\t\t\t\/\/ calculate current label length\n\t\t\t\tdwCurrLabelLength = (DWORD)(&amp;szConvertedHost&#91;i] - pCurrDot);\n\t\t\t\tdwCurrLabelLength--;\n\t\t\t\tif(dwCurrLabelLength == 0 || dwCurrLabelLength >= 64)\n\t\t\t\t{\n\t\t\t\t\treturn 1;\n\t\t\t\t}\n\n\t\t\t\t\/\/ insert label length\n\t\t\t\t*pCurrDot = (char)dwCurrLabelLength;\n\t\t\t}\n\n\t\t\t\/\/ store current dot position\n\t\t\tpCurrDot = &amp;szConvertedHost&#91;i];\n\t\t}\n\t}\n\n\t\/\/ create socket handle\n\tif(NTSockets_CreateTcpSocket(&amp;SocketData) != 0)\n\t{\n\t\t\/\/ error\n\t\treturn 1;\n\t}\n\n\t\/\/ connect to DNS server\n\tif(NTSockets_Connect(&amp;SocketData, pDNSClient_IP, 53) != 0)\n\t{\n\t\t\/\/ error\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ calculate request length\n\twRequestLength = sizeof(DNSClient_HeaderStruct) + dwHostLength + sizeof(DNSClient_RequestQueryDetails);\n\twBlockLength = NTSockets_Swap16BitByteOrder(wRequestLength);\n\n\t\/\/ set request header details\n\tmemset((void*)&amp;DNSClient_RequestHeader, 0, sizeof(DNSClient_RequestHeader));\n\tDNSClient_RequestHeader.wTransID = NTSockets_Swap16BitByteOrder(1);\n\tDNSClient_RequestHeader.wFlags = NTSockets_Swap16BitByteOrder(0x100);\n\tDNSClient_RequestHeader.wQuestionCount = NTSockets_Swap16BitByteOrder(1);\n\n\t\/\/ type A dns request\n\tmemset((void*)&amp;DNSClient_RequestQueryDetails, 0, sizeof(DNSClient_RequestQueryDetails));\n\tDNSClient_RequestQueryDetails.wType = NTSockets_Swap16BitByteOrder(1);\n\tDNSClient_RequestQueryDetails.wClass = NTSockets_Swap16BitByteOrder(1);\n\n\t\/\/ send request length\n\tif(NTSockets_Send(&amp;SocketData, (BYTE*)&amp;wBlockLength, sizeof(WORD)) != 0)\n\t{\n\t\t\/\/ error\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ send request header\n\tif(NTSockets_Send(&amp;SocketData, (BYTE*)&amp;DNSClient_RequestHeader, sizeof(DNSClient_RequestHeader)) != 0)\n\t{\n\t\t\/\/ error\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ send host name\n\tif(NTSockets_Send(&amp;SocketData, (BYTE*)szConvertedHost, dwHostLength) != 0)\n\t{\n\t\t\/\/ error\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ send host query details\n\tif(NTSockets_Send(&amp;SocketData, (BYTE*)&amp;DNSClient_RequestQueryDetails, sizeof(DNSClient_RequestQueryDetails)) != 0)\n\t{\n\t\t\/\/ error\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ receive response length\n\tif(NTSockets_Recv(&amp;SocketData, (BYTE*)&amp;wBlockLength, sizeof(WORD)) != 0)\n\t{\n\t\t\/\/ error\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ swap byte order\n\twResponseLength = NTSockets_Swap16BitByteOrder(wBlockLength);\n\n\t\/\/ validate response length\n\tif(wResponseLength &lt; sizeof(DNSClient_HeaderStruct) || wResponseLength > sizeof(bResponseBuffer))\n\t{\n\t\t\/\/ error\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ receive response data\n\tmemset((void*)bResponseBuffer, 0, sizeof(bResponseBuffer));\n\tif(NTSockets_Recv(&amp;SocketData, bResponseBuffer, wResponseLength) != 0)\n\t{\n\t\t\/\/ error\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ set response header ptr\n\tpDNSClient_ResponseHeader = (DNSClient_HeaderStruct*)bResponseBuffer;\n\n\t\/\/ check flags (expect response, no error)\n\tif(pDNSClient_ResponseHeader->wFlags != NTSockets_Swap16BitByteOrder(0x8180))\n\t{\n\t\t\/\/ error\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ validate question count\n\tif(pDNSClient_ResponseHeader->wQuestionCount != NTSockets_Swap16BitByteOrder(1))\n\t{\n\t\t\/\/ error\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ get response answer count\n\twAnswerCount = NTSockets_Swap16BitByteOrder(pDNSClient_ResponseHeader->wAnswerRecordCount);\n\n\t\/\/ read DNS response answers\n\tdwCurrAnswerEntryStartIndex = wRequestLength;\n\tfor(i = 0; i &lt; (DWORD)wAnswerCount; i++)\n\t{\n\t\t\/\/ validate start index\n\t\tif((dwCurrAnswerEntryStartIndex + sizeof(DNSClient_ResponseAnswerHeaderStruct)) > (DWORD)wResponseLength)\n\t\t{\n\t\t\t\/\/ error\n\t\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\t\treturn 1;\n\t\t}\n\n\t\t\/\/ get current response answer header ptr\n\t\tpDNSClient_ResponseAnswerHeader = (DNSClient_ResponseAnswerHeaderStruct*)&amp;bResponseBuffer&#91;dwCurrAnswerEntryStartIndex];\n\n\t\t\/\/ check if this is a type A record\n\t\tif(pDNSClient_ResponseAnswerHeader->wType == NTSockets_Swap16BitByteOrder(1) &amp;&amp; pDNSClient_ResponseAnswerHeader->wClass == NTSockets_Swap16BitByteOrder(1))\n\t\t{\n\t\t\t\/\/ ensure value length is 4 (ipv4 addr)\n\t\t\tif(pDNSClient_ResponseAnswerHeader->wLength != NTSockets_Swap16BitByteOrder(4))\n\t\t\t{\n\t\t\t\t\/\/ error\n\t\t\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\t\/\/ validate ip addr index\n\t\t\tdwIpAddrIndex = dwCurrAnswerEntryStartIndex + sizeof(DNSClient_ResponseAnswerHeaderStruct);\n\t\t\tif((dwIpAddrIndex + 4) > (DWORD)wResponseLength)\n\t\t\t{\n\t\t\t\t\/\/ error\n\t\t\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\t\/\/ store IP addr\n\t\t\tmemcpy((void*)bIP, (void*)&amp;bResponseBuffer&#91;dwIpAddrIndex], 4);\n\n\t\t\t\/\/ set flag\n\t\t\tdwFoundRecord = 1;\n\n\t\t\tbreak;\n\t\t}\n\t\telse\n\t\t{\n\t\t\t\/\/ check next entry\n\t\t\tdwCurrAnswerEntryStartIndex += sizeof(DNSClient_ResponseAnswerHeaderStruct);\n\t\t\tdwCurrAnswerEntryStartIndex += NTSockets_Swap16BitByteOrder(pDNSClient_ResponseAnswerHeader->wLength);\n\t\t}\n\t}\n\n\t\/\/ close socket\n\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\/\/ ensure a valid record was found\n\tif(dwFoundRecord == 0)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ generate IP string\n\tmemset(szIP, 0, sizeof(szIP));\n\t_snprintf(szIP, sizeof(szIP) - 1, \"%u.%u.%u.%u\", bIP&#91;0], bIP&#91;1], bIP&#91;2], bIP&#91;3]);\n\n\t\/\/ store value\n\tstrncpy(pOutput, szIP, dwOutputMaxLength);\n\n\treturn 0;\n}\n\nDWORD DownloadFile(char *pURL, BYTE **pOutput, DWORD *pdwOutputLength)\n{\n\tchar szProtocol&#91;16];\n\tchar szHostName&#91;256];\n\tchar szRequestHeader&#91;2048];\n\tchar szResponseHeader&#91;2048];\n\tchar *pStartOfHostName = NULL;\n\tchar *pEndOfHostName = NULL;\n\tchar *pRequestPath = NULL;\n\tDWORD dwAddr = 0;\n\tchar *pHostNamePort = NULL;\n\tDWORD dwPort = 0;\n\tchar szResolvedIP&#91;32];\n\tNTSockets_SocketDataStruct SocketData;\n\tDWORD dwFoundEndOfResponseHeader = 0;\n\tchar szEndOfResponseHeader&#91;8];\n\tchar szResponseSuccessStatus&#91;32];\n\tchar szContentLengthParamName&#91;16];\n\tchar *pContentLength = NULL;\n\tchar *pEndOfContentLength = NULL;\n\tDWORD dwOutputLength = 0;\n\tDWORD dwOutputAllocLength = 0;\n\tBYTE *pOutputBuffer = NULL;\n\tBYTE *pNewOutputBuffer = NULL;\n\tBYTE bCurrByte = 0;\n\n\t\/\/ ensure url starts with 'http:\/\/'\n\tmemset(szProtocol, 0, sizeof(szProtocol));\n\tstrncpy(szProtocol, \"http:\/\/\", sizeof(szProtocol) - 1);\n\tif(strncmp(pURL, szProtocol, strlen(szProtocol)) != 0)\n\t{\n\t\t\/\/ error\n\t\tprintf(\"Error: Invalid protocol\\n\");\n\n\t\treturn 1;\n\t}\n\n\t\/\/ copy host name\n\tpStartOfHostName = pURL;\n\tpStartOfHostName += strlen(szProtocol);\n\tmemset(szHostName, 0, sizeof(szHostName));\n\tstrncpy(szHostName, pStartOfHostName, sizeof(szHostName) - 1);\n\n\t\/\/ remove request path from host name\n\tpEndOfHostName = strstr(szHostName, \"\/\");\n\tif(pEndOfHostName == NULL)\n\t{\n\t\t\/\/ error\n\t\tprintf(\"Error: Invalid URL\\n\");\n\n\t\treturn 1;\n\t}\n\t*pEndOfHostName = '\\0';\n\n\t\/\/ check if the host name contains a custom port number\n\tpHostNamePort = strstr(szHostName, \":\");\n\tif(pHostNamePort == NULL)\n\t{\n\t\t\/\/ no port specified - use port 80\n\t\tdwPort = 80;\n\t}\n\telse\n\t{\n\t\t\/\/ terminate string\n\t\t*pHostNamePort = '\\0';\n\n\t\t\/\/ extract port number\n\t\tpHostNamePort++;\n\t\tdwPort = atoi(pHostNamePort);\n\t\tif(dwPort == 0)\n\t\t{\n\t\t\t\/\/ error\n\t\t\tprintf(\"Error: Invalid URL\\n\");\n\n\t\t\treturn 1;\n\t\t}\n\t}\n\n\t\/\/ get start of request path\n\tpRequestPath = pStartOfHostName;\n\tpRequestPath += strlen(szHostName);\n\n\t\/\/ check if the host name is a valid ipv4 address\n\tmemset(szResolvedIP, 0, sizeof(szResolvedIP));\n\tif(NTSockets_ConvertIP(szHostName, &amp;dwAddr) != 0)\n\t{\n\t\t\/\/ not ipv4 - try to resolve host using DNS\n\t\tif(DNSClient_Query(\"8.8.8.8\", szHostName, szResolvedIP, sizeof(szResolvedIP) - 1) != 0)\n\t\t{\n\t\t\t\/\/ error\n\t\t\tprintf(\"Error: Failed to resolve host name\\n\");\n\n\t\t\treturn 1;\n\t\t}\n\t}\n\telse\n\t{\n\t\t\/\/ copy original ip\n\t\tstrncpy(szResolvedIP, szHostName, sizeof(szResolvedIP) - 1);\n\t}\n\n\t\/\/ create socket handle\n\tif(NTSockets_CreateTcpSocket(&amp;SocketData) != 0)\n\t{\n\t\t\/\/ error\n\t\tprintf(\"Error: Failed to create TCP socket\\n\");\n\n\t\treturn 1;\n\t}\n\n\t\/\/ connect to server\n\tif(NTSockets_Connect(&amp;SocketData, szResolvedIP, (WORD)dwPort) != 0)\n\t{\n\t\t\/\/ error\n\t\tprintf(\"Error: Failed to connect to server\\n\");\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ send HTTP request\n\tmemset(szRequestHeader, 0, sizeof(szRequestHeader));\n\t_snprintf(szRequestHeader, sizeof(szRequestHeader) - 1, \"GET %s HTTP\/1.0\\r\\nHost: %s\\r\\n\\r\\n\", pRequestPath, szHostName);\n\tif(NTSockets_Send(&amp;SocketData, (BYTE*)szRequestHeader, strlen(szRequestHeader)) != 0)\n\t{\n\t\t\/\/ error\n\t\tprintf(\"Error: Failed to send data to server\\n\");\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\tprintf(\"Sent HTTP request:\\n%s\", szRequestHeader);\n\n\t\/\/ get response header\n\tmemset(szEndOfResponseHeader, 0, sizeof(szEndOfResponseHeader));\n\tstrncpy(szEndOfResponseHeader, \"\\r\\n\\r\\n\", sizeof(szEndOfResponseHeader) - 1);\n\tmemset(szResponseHeader, 0, sizeof(szResponseHeader));\n\tfor(DWORD i = 0; i &lt; sizeof(szResponseHeader) - 1; i++)\n\t{\n\t\t\/\/ get next byte\n\t\tif(NTSockets_Recv(&amp;SocketData, (BYTE*)&amp;szResponseHeader&#91;i], 1) != 0)\n\t\t{\n\t\t\t\/\/ error\n\t\t\tprintf(\"Error: Failed to read HTTP response header\\n\");\n\t\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\t\treturn 1;\n\t\t}\n\n\t\t\/\/ check if this is the end of the response header\n\t\tif((i + 1) >= strlen(szEndOfResponseHeader))\n\t\t{\n\t\t\tif(strncmp(&amp;szResponseHeader&#91;(i + 1) - strlen(szEndOfResponseHeader)], szEndOfResponseHeader, strlen(szEndOfResponseHeader)) == 0)\n\t\t\t{\n\t\t\t\t\/\/ found end of response header\n\t\t\t\tdwFoundEndOfResponseHeader = 1;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\t\/\/ ensure the end of the response header was found\n\tif(dwFoundEndOfResponseHeader == 0)\n\t{\n\t\t\/\/ error\n\t\tprintf(\"Error: Failed to read HTTP response header\\n\");\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\tprintf(\"Received HTTP response:\\n%s\", szResponseHeader);\n\n\t\/\/ convert response header to upper-case (for the content-length value search below)\n\tfor(i = 0; i &lt; strlen(szResponseHeader); i++)\n\t{\n\t\t\/\/ convert to upper-case (for the content-length value search below)\n\t\tszResponseHeader&#91;i] = toupper(szResponseHeader&#91;i]);\n\t}\n\n\t\/\/ check status code\n\tmemset(szResponseSuccessStatus, 0, sizeof(szResponseSuccessStatus));\n\tstrncpy(szResponseSuccessStatus, \"HTTP\/1.0 200 OK\\r\\n\", sizeof(szResponseSuccessStatus) - 1);\n\tif(strncmp(szResponseHeader, szResponseSuccessStatus, strlen(szResponseSuccessStatus)) != 0)\n\t{\n\t\t\/\/ error\n\t\tprintf(\"Error: Invalid response status code\\n\");\n\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ get content-length value\n\tmemset(szContentLengthParamName, 0, sizeof(szContentLengthParamName));\n\tstrncpy(szContentLengthParamName, \"CONTENT-LENGTH: \", sizeof(szContentLengthParamName) - 1);\n\tpContentLength = strstr(szResponseHeader, szContentLengthParamName);\n\tif(pContentLength != NULL)\n\t{\n\t\t\/\/ content-length field exists\n\t\tpContentLength += strlen(szContentLengthParamName);\n\t\tpEndOfContentLength = strstr(pContentLength, \"\\r\\n\");\n\t\tif(pEndOfContentLength == NULL)\n\t\t{\n\t\t\t\/\/ error\n\t\t\tprintf(\"Error: Invalid response header\\n\");\n\t\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\t\treturn 1;\n\t\t}\n\t\t*pEndOfContentLength = '\\0';\n\t\tdwOutputLength = atoi(pContentLength);\n\n\t\t\/\/ process response data\n\t\tif(dwOutputLength != 0)\n\t\t{\n\t\t\t\/\/ allocate output data\n\t\t\tpOutputBuffer = (BYTE*)malloc(dwOutputLength);\n\t\t\tif(pOutputBuffer == NULL)\n\t\t\t{\n\t\t\t\t\/\/ error\n\t\t\t\tprintf(\"Error: Failed to allocate memory\\n\");\n\t\t\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\t\/\/ read output data\n\t\t\tif(NTSockets_Recv(&amp;SocketData, pOutputBuffer, dwOutputLength) != 0)\n\t\t\t{\n\t\t\t\t\/\/ error\n\t\t\t\tprintf(\"Error: Failed to read HTTP response data\\n\");\n\t\t\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t}\n\t}\n\telse\n\t{\n\t\t\/\/ no content-length field - read until socket closes\n\t\tfor(;;)\n\t\t{\n\t\t\t\/\/ read output data\n\t\t\tif(NTSockets_Recv(&amp;SocketData, &amp;bCurrByte, 1) != 0)\n\t\t\t{\n\t\t\t\t\/\/ finished\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\t\/\/ check if the output buffer is large enough\n\t\t\tif(dwOutputLength >= dwOutputAllocLength)\n\t\t\t{\n\t\t\t\t\/\/ reallocate output buffer - add 8kb\n\t\t\t\tdwOutputAllocLength += 8192;\n\t\t\t\tif(pOutputBuffer == NULL)\n\t\t\t\t{\n\t\t\t\t\t\/\/ first buffer\n\t\t\t\t\tpOutputBuffer = (BYTE*)malloc(dwOutputAllocLength);\n\t\t\t\t\tif(pOutputBuffer == NULL)\n\t\t\t\t\t{\n\t\t\t\t\t\t\/\/ error\n\t\t\t\t\t\tprintf(\"Error: Failed to allocate memory\\n\");\n\t\t\t\t\t\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\t\t\t\t\treturn 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\t\/\/ reallocate existing buffer\n\t\t\t\t\tpNewOutputBuffer = (BYTE*)realloc(pOutputBuffer, dwOutputAllocLength);\n\t\t\t\t\tif(pNewOutputBuffer == NULL)\n\t\t\t\t\t{\n\t\t\t\t\t\t\/\/ error\n\t\t\t\t\t\tprintf(\"Error: Failed to allocate memory\\n\");\n\t\t\t\t\t\tNTSockets_CloseSocket(&amp;SocketData);\n\t\t\t\t\t\tfree(pOutputBuffer);\n\n\t\t\t\t\t\treturn 1;\n\t\t\t\t\t}\n\n\t\t\t\t\t\/\/ update ptr\n\t\t\t\t\tpOutputBuffer = pNewOutputBuffer;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t\/\/ store current byte\n\t\t\t*(BYTE*)(pOutputBuffer + dwOutputLength) = bCurrByte;\n\t\t\tdwOutputLength++;\n\t\t}\n\t}\n\n\t\/\/ close socket\n\tNTSockets_CloseSocket(&amp;SocketData);\n\n\t\/\/ store data\n\t*pOutput = pOutputBuffer;\n\t*pdwOutputLength = dwOutputLength;\n\n\treturn 0;\n}\n\nint main(int argc, char *argv&#91;])\n{\n\tBYTE *pOutput = NULL;\n\tDWORD dwLength = 0;\n\tchar *pURL = NULL;\n\tchar *pOutputPath = NULL;\n\tHANDLE hOutputFile = NULL;\n\tDWORD dwBytesWritten = 0;\n\n\tprintf(\"NTSockets - File Download PoC - www.x86matthew.com\\n\\n\");\n\n\tif(argc != 3)\n\t{\n\t\tprintf(\"Usage: %s &#91;url] &#91;output_file_path]\\n\\n\", argv&#91;0]);\n\n\t\treturn 1;\n\t}\n\n\t\/\/ get param\n\tpURL = argv&#91;1];\n\tpOutputPath = argv&#91;2];\n\n\t\/\/ get NtDeviceIoControlFile function ptr\n\tNtDeviceIoControlFile = (unsigned long (__stdcall *)(void *,void *,void *,void *,struct IO_STATUS_BLOCK *,unsigned long,void *,unsigned long,void *,unsigned long))GetProcAddress(GetModuleHandle(\"ntdll.dll\"), \"NtDeviceIoControlFile\");\n\tif(NtDeviceIoControlFile == NULL)\n\t{\n\t\treturn 1;\n\t}\n\n\t\/\/ get NtCreateFile function ptr\n\tNtCreateFile = (unsigned long (__stdcall *)(void ** ,unsigned long,struct OBJECT_ATTRIBUTES *,struct IO_STATUS_BLOCK *,union _LARGE_INTEGER *,unsigned long,unsigned long,unsigned long,unsigned long,void *,unsigned long))GetProcAddress(GetModuleHandle(\"ntdll.dll\"), \"NtCreateFile\");\n\tif(NtCreateFile == NULL)\n\t{\n\t\treturn 1;\n\t}\n\n\tprintf(\"Downloading file: %s\\n\\n\", pURL);\n\n\t\/\/ download file\n\tif(DownloadFile(pURL, &amp;pOutput, &amp;dwLength) != 0)\n\t{\n\t\tprintf(\"Failed to download file\\n\");\n\n\t\treturn 1;\n\t}\n\n\tprintf(\"Downloaded %u bytes successfully\\n\\n\", dwLength);\n\n\tprintf(\"Creating output file: %s\\n\", pOutputPath);\n\n\t\/\/ create output file\n\thOutputFile = CreateFile(pOutputPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);\n\tif(hOutputFile == INVALID_HANDLE_VALUE)\n\t{\n\t\tprintf(\"Failed to create output file\\n\");\n\n\t\treturn 1;\n\t}\n\n\t\/\/ write output data to file\n\tif(WriteFile(hOutputFile, pOutput, dwLength, &amp;dwBytesWritten, NULL) == 0)\n\t{\n\t\tprintf(\"Failed to write output data to file\\n\");\n\n\t\treturn 1;\n\t}\n\n\t\/\/ close output file\n\tCloseHandle(hOutputFile);\n\n\tif(dwLength != 0)\n\t{\n\t\t\/\/ free buffer\n\t\tfree(pOutput);\n\t}\n\n\tprintf(\"\\nFinished\\n\");\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/www.x86matthew.com\/view_post?id=ntsockets  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[45,37,43],"tags":[34,35,36],"class_list":["post-126","post","type-post","status-publish","format-standard","hentry","category-x86matthew-com","category-samples","category-infoarticle","tag-ntcreatefile","tag-ntdeviceiocontrolfile","tag-ntsockets"],"views":2044,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=126"}],"version-history":[{"count":1,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/126\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/126\/revisions\/127"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}