-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDNSQuery.ahk
138 lines (117 loc) · 5.19 KB
/
DNSQuery.ahk
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
; =============================================================================================================================================================
; Author(s) .....: jNizM, just me
; Released ......: 2013-11-02
; Modified ......: 2023-01-18
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: DNSQuery()
;
; Parameter(s)...: [in] Name - String that represents the DNS name to query
; [in] Type - DNS Record Type (https://learn.microsoft.com/en-us/windows/win32/dns/dns-constants#dns-record-types)
; [in, opt] Option - DNS Query Options (https://learn.microsoft.com/en-us/windows/win32/dns/dns-constants#dns-query-options)
;
; Return ........: Retrieves the Resource Record (RR) depends on DNS Record Type.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
#DllLoad "dnsapi.dll"
#DllLoad "ntdll.dll"
DNSQuery(Name, Type, Options := 0)
{
static STATUS_SUCCESS := 0
static DnsFreeRecordList := 1
static RECORD_DATA := (A_PtrSize * 2) + 16
static DNS_TYPE := Map("A", 0x0001, "NS", 0x0002, "CNAME", 0x0005, "SOA", 0x0006, "PTR", 0x000c, "MX", 0x000f, "TEXT", 0x0010, "AAAA", 0x001c)
if !(DNS_TYPE.Has(Type))
throw Error()
DNS_STATUS := DllCall("dnsapi\DnsQuery_W", "Str", Name, "Short", DNS_TYPE[Type], "UInt", Options, "Ptr", 0, "Ptr*", &DNS_RECORD := 0, "Ptr", 0)
if (DNS_STATUS = STATUS_SUCCESS)
{
Addr := DNS_RECORD
DNS_RECORD_LIST := Map()
while (Addr)
{
LIST := Map()
RECORD_TYPE := NumGet(Addr, A_PtrSize * 2, "UShort")
switch RECORD_TYPE
{
case DNS_TYPE["A"]:
{
LIST["IpAddress"] := RtlIpv4AddressToStringW(NumGet(Addr, RECORD_DATA, "UInt"))
}
case DNS_TYPE["NS"], DNS_TYPE["CNAME"], DNS_TYPE["PTR"]:
{
LIST["NameHost"] := StrGet(NumGet(Addr, RECORD_DATA, "Ptr"))
}
case DNS_TYPE["SOA"]:
{
LIST["NamePrimaryServer"] := StrGet(NumGet(Addr, RECORD_DATA, "Ptr"))
LIST["NameAdministrator"] := StrGet(NumGet(Addr + 8, RECORD_DATA, "Ptr"))
LIST["SerialNo"] := NumGet(Addr + 16, RECORD_DATA, "UInt")
LIST["Refresh"] := NumGet(Addr + 20, RECORD_DATA, "UInt")
LIST["Retry"] := NumGet(Addr + 24, RECORD_DATA, "UInt")
LIST["Expire"] := NumGet(Addr + 28, RECORD_DATA, "UInt")
LIST["DefaultTtl"] := NumGet(Addr + 32, RECORD_DATA, "UInt")
}
case DNS_TYPE["MX"]:
{
LIST["NameExchange"] := StrGet(NumGet(Addr, RECORD_DATA, "Ptr"))
LIST["Preference"] := NumGet(Addr + 8, RECORD_DATA, "UChar")
}
case DNS_TYPE["TEXT"]:
{
LIST["StringArray"] := StrGet(NumGet(Addr + 8, RECORD_DATA, "Ptr"))
}
case DNS_TYPE["AAAA"]:
{
LIST["Ip6Address"] := RtlIpv6AddressToStringW(NumGet(Addr, RECORD_DATA, "UInt"))
}
}
DNS_RECORD_LIST[A_Index] := LIST
try Addr := NumGet(Addr, "Ptr")
}
DllCall("dnsapi\DnsRecordListFree", "Ptr", DNS_RECORD, "Int", DnsFreeRecordList)
return DNS_RECORD_LIST
}
throw OSError()
}
RtlIpv4AddressToStringW(IN_ADDR)
{
Size := VarSetStrCapacity(&StringAddr, 32)
if (DllCall("ntdll\RtlIpv4AddressToStringW", "Ptr*", IN_ADDR, "Str", StringAddr))
return StringAddr
return False
}
RtlIpv6AddressToStringW(IN6_ADDR)
{
Size := VarSetStrCapacity(&StringAddr, 92)
if (DllCall("ntdll\RtlIpv6AddressToStringW", "Ptr*", IN6_ADDR, "Str", StringAddr))
return StringAddr
return False
}
; =============================================================================================================================================================
; Example(s)
; =============================================================================================================================================================
for i, v in DNS := DNSQuery("www.google.com", "A")
for k, v in DNS[i]
MsgBox k ": " v
for i, v in DNS := DNSQuery("www.google.com", "AAAA")
for k, v in DNS[i]
MsgBox k ": " v
for i, v in DNS := DNSQuery("learn.microsoft.com", "CNAME")
for k, v in DNS[i]
MsgBox k ": " v
for i, v in DNS := DNSQuery("8.8.8.8.IN-ADDR.ARPA", "PTR")
for k, v in DNS[i]
MsgBox k ": " v
for i, v in DNS := DNSQuery("google.com", "MX")
for k, v in DNS[i]
MsgBox k ": " v
for i, v in DNS := DNSQuery("google.com", "TEXT")
for k, v in DNS[i]
MsgBox k ": " v
for i, v in DNS := DNSQuery("google.com", "MX")
for k, v in DNS[i]
MsgBox k ": " v
for i, v in DNS := DNSQuery("google.com", "SOA")
for k, v in DNS[i]
MsgBox k ": " v