Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit d9ca535

Browse files
author
Matt Graeber
committed
Merge pull request #26 from obscuresec/master
Update Invoke-ReverseDnsLookup.ps1
2 parents 7c51e93 + 14780a5 commit d9ca535

File tree

1 file changed

+130
-105
lines changed

1 file changed

+130
-105
lines changed

Recon/Invoke-ReverseDnsLookup.ps1

Lines changed: 130 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -50,146 +50,171 @@ IP HostName
5050
Description
5151
-----------
5252
Returns the hostnames of the IP addresses specified by the IP range specified.
53+
54+
.EXAMPLE
55+
56+
PS C:\> Write-Output "74.125.228.1,74.125.228.0/29" | Invoke-ReverseDnsLookup
57+
58+
IP HostName
59+
-- --------
60+
74.125.228.1 iad23s05-in-f1.1e100.net
61+
74.125.228.1 iad23s05-in-f1.1e100.net
62+
74.125.228.2 iad23s05-in-f2.1e100.net
63+
74.125.228.3 iad23s05-in-f3.1e100.net
64+
74.125.228.4 iad23s05-in-f4.1e100.net
65+
74.125.228.5 iad23s05-in-f5.1e100.net
66+
74.125.228.6 iad23s05-in-f6.1e100.net
67+
68+
Description
69+
-----------
70+
Returns the hostnames of the IP addresses piped from another source.
71+
5372
5473
.LINK
5574
5675
http://www.exploit-monday.com
76+
https://github.com/mattifestation/PowerSploit
5777
#>
5878

5979
Param (
60-
[Parameter(Position = 0, Mandatory = $True)]
80+
[Parameter(Position = 0, Mandatory = $True,ValueFromPipeline=$True)]
6181
[String]
6282
$IpRange
6383
)
6484

65-
function Parse-IPList ([String] $IpRange)
66-
{
67-
68-
function IPtoInt
85+
BEGIN {
86+
87+
function Parse-IPList ([String] $IpRange)
6988
{
70-
Param([String] $IpString)
89+
90+
function IPtoInt
91+
{
92+
Param([String] $IpString)
7193

72-
$Hexstr = ""
73-
$Octets = $IpString.Split(".")
74-
foreach ($Octet in $Octets) {
75-
$Hexstr += "{0:X2}" -f [Int] $Octet
94+
$Hexstr = ""
95+
$Octets = $IpString.Split(".")
96+
foreach ($Octet in $Octets) {
97+
$Hexstr += "{0:X2}" -f [Int] $Octet
98+
}
99+
return [Convert]::ToInt64($Hexstr, 16)
76100
}
77-
return [Convert]::ToInt64($Hexstr, 16)
78-
}
79101

80-
function InttoIP
81-
{
82-
Param([Int64] $IpInt)
83-
$Hexstr = $IpInt.ToString("X8")
84-
$IpStr = ""
85-
for ($i=0; $i -lt 8; $i += 2) {
86-
$IpStr += [Convert]::ToInt64($Hexstr.SubString($i,2), 16)
87-
$IpStr += '.'
102+
function InttoIP
103+
{
104+
Param([Int64] $IpInt)
105+
$Hexstr = $IpInt.ToString("X8")
106+
$IpStr = ""
107+
for ($i=0; $i -lt 8; $i += 2) {
108+
$IpStr += [Convert]::ToInt64($Hexstr.SubString($i,2), 16)
109+
$IpStr += '.'
110+
}
111+
return $IpStr.TrimEnd('.')
88112
}
89-
return $IpStr.TrimEnd('.')
90-
}
91113

92-
$Ip = [System.Net.IPAddress]::Parse("127.0.0.1")
114+
$Ip = [System.Net.IPAddress]::Parse("127.0.0.1")
93115

94-
foreach ($Str in $IpRange.Split(","))
95-
{
96-
$Item = $Str.Trim()
97-
$Result = ""
98-
$IpRegex = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
99-
100-
# First, validate the input
101-
switch -regex ($Item)
116+
foreach ($Str in $IpRange.Split(","))
102117
{
103-
"^$IpRegex/\d{1,2}$"
104-
{
105-
$Result = "cidrRange"
106-
break
107-
}
108-
"^$IpRegex-$IpRegex$"
109-
{
110-
$Result = "range"
111-
break
112-
}
113-
"^$IpRegex$"
114-
{
115-
$Result = "single"
116-
break
117-
}
118-
default
118+
$Item = $Str.Trim()
119+
$Result = ""
120+
$IpRegex = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
121+
122+
# First, validate the input
123+
switch -regex ($Item)
119124
{
120-
Write-Warning "Inproper input"
121-
return
125+
"^$IpRegex/\d{1,2}$"
126+
{
127+
$Result = "cidrRange"
128+
break
129+
}
130+
"^$IpRegex-$IpRegex$"
131+
{
132+
$Result = "range"
133+
break
134+
}
135+
"^$IpRegex$"
136+
{
137+
$Result = "single"
138+
break
139+
}
140+
default
141+
{
142+
Write-Warning "Inproper input"
143+
return
144+
}
122145
}
123-
}
124146

125-
#Now, start processing the IP addresses
126-
switch ($Result)
127-
{
128-
"cidrRange"
147+
#Now, start processing the IP addresses
148+
switch ($Result)
129149
{
130-
$CidrRange = $Item.Split("/")
131-
$Network = $CidrRange[0]
132-
$Mask = $CidrRange[1]
150+
"cidrRange"
151+
{
152+
$CidrRange = $Item.Split("/")
153+
$Network = $CidrRange[0]
154+
$Mask = $CidrRange[1]
133155

134-
if (!([System.Net.IPAddress]::TryParse($Network, [ref] $Ip))) { Write-Warning "Invalid IP address supplied!"; return}
135-
if (($Mask -lt 0) -or ($Mask -gt 30)) { Write-Warning "Invalid network mask! Acceptable values are 0-30"; return}
156+
if (!([System.Net.IPAddress]::TryParse($Network, [ref] $Ip))) { Write-Warning "Invalid IP address supplied!"; return}
157+
if (($Mask -lt 0) -or ($Mask -gt 30)) { Write-Warning "Invalid network mask! Acceptable values are 0-30"; return}
136158

137-
$BinaryIP = [Convert]::ToString((IPtoInt $Network),2).PadLeft(32,'0')
138-
#Generate lower limit (Excluding network address)
139-
$Lower = $BinaryIP.Substring(0, $Mask) + "0" * ((32-$Mask)-1) + "1"
140-
#Generate upperr limit (Excluding broadcast address)
141-
$Upper = $BinaryIP.Substring(0, $Mask) + "1" * ((32-$Mask)-1) + "0"
142-
$LowerInt = [Convert]::ToInt64($Lower, 2)
143-
$UpperInt = [Convert]::ToInt64($Upper, 2)
144-
for ($i = $LowerInt; $i -le $UpperInt; $i++) { InttoIP $i }
145-
}
146-
"range"
147-
{
148-
$Range = $item.Split("-")
159+
$BinaryIP = [Convert]::ToString((IPtoInt $Network),2).PadLeft(32,'0')
160+
#Generate lower limit (Excluding network address)
161+
$Lower = $BinaryIP.Substring(0, $Mask) + "0" * ((32-$Mask)-1) + "1"
162+
#Generate upperr limit (Excluding broadcast address)
163+
$Upper = $BinaryIP.Substring(0, $Mask) + "1" * ((32-$Mask)-1) + "0"
164+
$LowerInt = [Convert]::ToInt64($Lower, 2)
165+
$UpperInt = [Convert]::ToInt64($Upper, 2)
166+
for ($i = $LowerInt; $i -le $UpperInt; $i++) { InttoIP $i }
167+
}
168+
"range"
169+
{
170+
$Range = $item.Split("-")
149171

150-
if ([System.Net.IPAddress]::TryParse($Range[0],[ref]$Ip)) { $Temp1 = $Ip }
151-
else { Write-Warning "Invalid IP address supplied!"; return }
172+
if ([System.Net.IPAddress]::TryParse($Range[0],[ref]$Ip)) { $Temp1 = $Ip }
173+
else { Write-Warning "Invalid IP address supplied!"; return }
152174

153-
if ([System.Net.IPAddress]::TryParse($Range[1],[ref]$Ip)) { $Temp2 = $Ip }
154-
else { Write-Warning "Invalid IP address supplied!"; return }
175+
if ([System.Net.IPAddress]::TryParse($Range[1],[ref]$Ip)) { $Temp2 = $Ip }
176+
else { Write-Warning "Invalid IP address supplied!"; return }
155177

156-
$Left = (IPtoInt $Temp1.ToString())
157-
$Right = (IPtoInt $Temp2.ToString())
178+
$Left = (IPtoInt $Temp1.ToString())
179+
$Right = (IPtoInt $Temp2.ToString())
158180

159-
if ($Right -gt $Left) {
160-
for ($i = $Left; $i -le $Right; $i++) { InttoIP $i }
161-
}
162-
else { Write-Warning "Invalid IP range. The right portion must be greater than the left portion."; return}
181+
if ($Right -gt $Left) {
182+
for ($i = $Left; $i -le $Right; $i++) { InttoIP $i }
183+
}
184+
else { Write-Warning "Invalid IP range. The right portion must be greater than the left portion."; return}
163185

164-
break
165-
}
166-
"single"
167-
{
168-
if ([System.Net.IPAddress]::TryParse($Item,[ref]$Ip)) { $Ip.IPAddressToString }
169-
else { Write-Warning "Invalid IP address supplied!"; return }
170-
break
171-
}
172-
default
173-
{
174-
Write-Warning "An error occured."
175-
return
186+
break
187+
}
188+
"single"
189+
{
190+
if ([System.Net.IPAddress]::TryParse($Item,[ref]$Ip)) { $Ip.IPAddressToString }
191+
else { Write-Warning "Invalid IP address supplied!"; return }
192+
break
193+
}
194+
default
195+
{
196+
Write-Warning "An error occured."
197+
return
198+
}
176199
}
177200
}
178-
}
179201

202+
}
180203
}
181-
182-
Parse-IPList $IpRange | ForEach-Object {
183-
try {
184-
$Temp = [System.Net.Dns]::GetHostEntry($_)
204+
205+
PROCESS {
206+
Parse-IPList $IpRange | ForEach-Object {
207+
try {
208+
Write-Verbose "Resolving $_"
209+
$Temp = [System.Net.Dns]::GetHostEntry($_)
185210

186-
$Result = @{
187-
IP = $_
188-
HostName = $Temp.HostName
189-
}
211+
$Result = @{
212+
IP = $_
213+
HostName = $Temp.HostName
214+
}
190215

191-
New-Object PSObject -Property $Result
192-
} catch [System.Net.Sockets.SocketException] {}
216+
New-Object PSObject -Property $Result
217+
} catch [System.Net.Sockets.SocketException] {}
218+
}
193219
}
194-
195220
}

0 commit comments

Comments
 (0)