Skip to content

Commit b64e961

Browse files
authored
fixed HasInternalIPs not pupulating on hosts file (#264)
1 parent e3444ee commit b64e961

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,20 @@ func (c *Client) queryMultiple(host string, requestTypes []uint16, resolver Reso
366366
for _, ip := range ips {
367367
if iputil.IsIPv4(ip) {
368368
dnsdata.A = append(dnsdata.A, ip)
369+
if CheckInternalIPs && internalRangeCheckerInstance != nil {
370+
if parsedIP := net.ParseIP(ip); parsedIP != nil && internalRangeCheckerInstance.ContainsIPv4(parsedIP) {
371+
dnsdata.HasInternalIPs = true
372+
dnsdata.InternalIPs = append(dnsdata.InternalIPs, ip)
373+
}
374+
}
369375
} else if iputil.IsIPv6(ip) {
370376
dnsdata.AAAA = append(dnsdata.AAAA, ip)
377+
if CheckInternalIPs && internalRangeCheckerInstance != nil {
378+
if parsedIP := net.ParseIP(ip); parsedIP != nil && internalRangeCheckerInstance.ContainsIPv6(parsedIP) {
379+
dnsdata.HasInternalIPs = true
380+
dnsdata.InternalIPs = append(dnsdata.InternalIPs, ip)
381+
}
382+
}
371383
}
372384
}
373385
}

client_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,90 @@ func TestTrace(t *testing.T) {
170170
_, err := client.Trace("www.projectdiscovery.io", dns.TypeA, 100)
171171
require.Nil(t, err, "could not resolve dns")
172172
}
173+
174+
func TestInternalIPDetectionWithHostsFile(t *testing.T) {
175+
CheckInternalIPs = true
176+
defer func() { CheckInternalIPs = false }()
177+
178+
options := Options{
179+
BaseResolvers: []string{"8.8.8.8:53"},
180+
MaxRetries: 3,
181+
Hostsfile: true,
182+
}
183+
184+
client, err := NewWithOptions(options)
185+
require.NoError(t, err)
186+
187+
client.knownHosts = map[string][]string{
188+
"localhost": {"127.0.0.1", "::1"},
189+
"internal.test": {"192.168.1.100", "10.0.0.1"},
190+
"external.test": {"8.8.8.8"},
191+
}
192+
193+
testCases := []struct {
194+
host string
195+
expectedInternal bool
196+
expectedInternalIP []string
197+
}{
198+
{
199+
host: "localhost",
200+
expectedInternal: true,
201+
expectedInternalIP: []string{"127.0.0.1", "::1"},
202+
},
203+
{
204+
host: "internal.test",
205+
expectedInternal: true,
206+
expectedInternalIP: []string{"192.168.1.100", "10.0.0.1"},
207+
},
208+
{
209+
host: "external.test",
210+
expectedInternal: false,
211+
},
212+
}
213+
214+
for _, tc := range testCases {
215+
t.Run(tc.host, func(t *testing.T) {
216+
result, err := client.QueryMultiple(tc.host, []uint16{dns.TypeA, dns.TypeAAAA})
217+
require.NoError(t, err)
218+
require.True(t, result.HostsFile)
219+
220+
if tc.expectedInternal {
221+
assert.True(t, result.HasInternalIPs, "HasInternalIPs should be true for %s", tc.host)
222+
assert.ElementsMatch(t, tc.expectedInternalIP, result.InternalIPs, "InternalIPs should match for %s", tc.host)
223+
} else {
224+
assert.False(t, result.HasInternalIPs, "HasInternalIPs should be false for %s", tc.host)
225+
assert.Empty(t, result.InternalIPs, "InternalIPs should be empty for %s", tc.host)
226+
}
227+
})
228+
}
229+
}
230+
231+
func TestInternalIPDetectionJSONOutput(t *testing.T) {
232+
CheckInternalIPs = true
233+
defer func() { CheckInternalIPs = false }()
234+
235+
options := Options{
236+
BaseResolvers: []string{"8.8.8.8:53"},
237+
MaxRetries: 3,
238+
Hostsfile: true,
239+
}
240+
241+
client, err := NewWithOptions(options)
242+
require.NoError(t, err)
243+
244+
client.knownHosts = map[string][]string{
245+
"localhost": {"127.0.0.1"},
246+
}
247+
248+
result, err := client.QueryMultiple("localhost", []uint16{dns.TypeA})
249+
require.NoError(t, err)
250+
251+
jsonOutput, err := result.JSON()
252+
require.NoError(t, err)
253+
254+
t.Logf("JSON output for localhost with internal IP detection:\n%s", jsonOutput)
255+
256+
assert.Contains(t, jsonOutput, `"has_internal_ips":true`)
257+
assert.Contains(t, jsonOutput, `"internal_ips":["127.0.0.1"]`)
258+
assert.Contains(t, jsonOutput, `"hosts_file":true`)
259+
}

0 commit comments

Comments
 (0)