diff --git a/c/meterpreter/source/extensions/stdapi/server/net/resolve.c b/c/meterpreter/source/extensions/stdapi/server/net/resolve.c new file mode 100644 index 00000000..9e8f0a17 --- /dev/null +++ b/c/meterpreter/source/extensions/stdapi/server/net/resolve.c @@ -0,0 +1,91 @@ +#include "precomp.h" +#include <stdio.h> + +#ifdef _WIN32 + #include <ws2tcpip.h> + #include <winsock2.h> +#else + #include <netdb.h> + #include <arpa/inet.h> +#endif + +DWORD resolve_host(LPCTSTR hostname, char* addr, u_short* addr_type, u_short* addr_length) +{ + struct hostent *he; + int i = 0; + + #ifdef _WIN32 + WSADATA data; + if (WSAStartup (MAKEWORD(1, 1), &data) != 0) + { + dprintf("Could not initialise Winsock.\n", stderr); + return 1; + } + #endif + + he = gethostbyname (hostname); + if (he == NULL) + { + return h_errno; + } + + for(i = 0; i < he->h_length; i++) + { + memcpy(addr+i, &(he->h_addr_list[0][i]), 1); + } + *addr_type = he->h_addrtype; + *addr_length = he->h_length; + + #ifdef _WIN32 + WSACleanup (); + #endif + + return h_errno; +} + +DWORD request_resolve_host(Remote *remote, Packet *packet) +{ + Packet *response = packet_create_response(packet); + LPCTSTR hostname = NULL; + char addr[16]; + u_short addr_type; + u_short addr_length; + DWORD result = NULL; + + hostname = packet_get_tlv_value_string(packet, TLV_TYPE_HOST_NAME); + + if (!hostname) + result = ERROR_INVALID_PARAMETER; + else + { + result = resolve_host(hostname, addr, &addr_type, &addr_length); + packet_add_tlv_raw(response, TLV_TYPE_IP, addr, addr_length); + packet_add_tlv_uint(response, TLV_TYPE_ADDR_TYPE, addr_type); + } + + packet_transmit_response(result, remote, response); + return ERROR_SUCCESS; +} + +DWORD request_resolve_hosts(Remote *remote, Packet *packet) +{ + Packet *response = packet_create_response(packet); + Tlv hostname = {0}; + DWORD result = NULL; + int index = 0; + + while( packet_enum_tlv( packet, index++, TLV_TYPE_HOST_NAME, &hostname ) == ERROR_SUCCESS ) + { + char addr[16] = {0}; + u_short addr_type = 0; + u_short addr_length = 0; + + resolve_host((LPCTSTR)hostname.buffer, addr, &addr_type, &addr_length); + + packet_add_tlv_raw(response, TLV_TYPE_IP, addr, addr_length); + packet_add_tlv_uint(response, TLV_TYPE_ADDR_TYPE, addr_type); + } + + packet_transmit_response(result, remote, response); + return ERROR_SUCCESS; +} diff --git a/c/meterpreter/workspace/ext_server_stdapi/ext_server_stdapi.vcxproj b/c/meterpreter/workspace/ext_server_stdapi/ext_server_stdapi.vcxproj index b2e8cf89..c26aa733 100644 --- a/c/meterpreter/workspace/ext_server_stdapi/ext_server_stdapi.vcxproj +++ b/c/meterpreter/workspace/ext_server_stdapi/ext_server_stdapi.vcxproj @@ -311,6 +311,7 @@ </ClCompile> <ClCompile Include="..\..\source\extensions\stdapi\server\net\config\arp.c" /> <ClCompile Include="..\..\source\extensions\stdapi\server\net\config\netstat.c" /> + <ClCompile Include="..\..\source\extensions\stdapi\server\net\resolve.c" /> <ClCompile Include="..\..\source\extensions\stdapi\server\stdapi.c"> <AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(PreprocessorDefinitions)</PreprocessorDefinitions> @@ -622,4 +623,4 @@ <ImportGroup Label="ExtensionTargets"> <Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" /> </ImportGroup> -</Project> +</Project> \ No newline at end of file