-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ARIN-info.ps1
More file actions
480 lines (393 loc) · 18.6 KB
/
Get-ARIN-info.ps1
File metadata and controls
480 lines (393 loc) · 18.6 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
## Written in PowerShell 5, this may have compatibility issues with older versions.
## This script leverages .Net and PowerShell to accomplish its tasks.
## Use Arin API calls to enhance data.
## preferred file types: .txt, .csv
## Regex parsed files cannot be guaranteed for perfect parsing.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# reusable file selector. this does not open folder locaitons.
function Select-File
{
param(
[int]$inputSelection
)
# create the new file selector object
$openFileDialog = New-Object Windows.Forms.Openfiledialog
$openFileDialog.InitialDirectory = [System.IO.Directory]::GetCurrentDirectory()
$openFileDialog.Title = "Select file"
#openthe correct file selector
switch($inputSelection)
{
2
{
$openFileDialog.Filter = "text files (*.txt)| *.txt"
}
3
{
$openFileDialog.Filter = "csv files (*.csv)| *.csv"
}
4
{
$openFileDialog.Filter = "All files (*.*)| *.*"
}
}
# open the file select dialog box
$results = $openFileDialog.ShowDialog()
# progress output
if($results -eq 'OK')
{
Write-Host("File Chosen") -ForegroundColor Green
}
else
{
Write-Host("Import aborted!") -ForegroundColor Red
}
$fileInfo = New-Object System.IO.FileInfo($openFileDialog.FileName)
# return the file info from the selected file
return $fileInfo
}
##
##Validate if needed!!
##
# Check Input File is .csv or .txt.
function Get-IPFile
{
Write-Host("Please choose a .csv or .txt file containing IP addresses.")
DO
{
$fInfo = Get-SelectFile
if(($fInfo.extension.ToLower() -ne '.csv') -and ($fInfo.extension.ToLower() -ne '.txt'))
{
$check = $false
Write-Output("Please choose a .csv or .txt file")
}
else
{
$check = $true
}
}WHILE($check -eq $false)
$fileContents = Get-Content $fInfo.FullName
}
# create storage path if it does not exist: C:\temp
function Make-DefaultDirectory
{
if(!(Test-Path C:\Temp))
{
mkdir C:\Temp
}
}
# choose the method of providing IP
function Get-InputSelection
{
Write-Host('Please choose one of the following ways to look up IP(s):')
DO
{
try
{
Write-Host("1) Single IP on command line `n2) Text File containing line delimited, IP list `n3) CSV containing IP(s) in a column `n4) Attempt to parse a file for a regex match of IP.")
$inputSelection = [int](Read-Host('Type Number 1 through 4'))
}
catch
{
Write-Host('Input does not appear to be a valid Int')
}
if(!($inputSelection -in (1..4)))
{
Write-Host("`n`nInput not a valid option, 1-4`n`n") -ForegroundColor Red
}
}WHILE(!($inputSelection -in (1..4)))
return $inputSelection
}
function Set-OutputSelection
{
Write-Host('Please choose one of the following ways to Output the data:')
DO
{
try
{
Write-Host("1) CSV Output `n2) Output to Elasticsearch database")
$outputSelection = [int](Read-Host('Type Number 1 or 2'))
}
catch
{
Write-Host('Input does not appear to be a valid Int')
}
if(!($outputSelection -in (1..2)))
{
Write-Host("`n`nInput not a valid option, Please use 1 or 2`n`n") -ForegroundColor Red
}
}WHILE(!($outputSelection -in (1..2)))
return $outputSelection
}
# read in single IP and validate it is an IP.
function Get-CommLineIP
{
DO
{
$validIP = Read-Host("Enter a valid IP address")
try
{
[ipaddress]$validIP | Out-Null
return $validIP
}
catch
{
Write-Host("IP failed validation `nPlease enter a valid IP") -ForegroundColor Red
$passed = $false
}
}WHILE($passed -eq $false)
}
#get IP field from CSV input
function Get-IPField
{
param(
$csvColumns
)
Write-Host("Select IP column from field list") -ForegroundColor Yellow
# code primarily leveraged from MS documentation: https://docs.microsoft.com/en-us/powershell/scripting/samples/selecting-items-from-a-list-box?view=powershell-5.1
# Add required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# creation base form and spawn location
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select IP field'
$form.Size = New-Object System.Drawing.Size(300,300)
$form.StartPosition = 'CenterScreen'
# create OK button and action
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,220)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'IP Selected'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
# create Cancel button and action
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,220)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
# create box form label
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please choose the column containing IPs'
$form.Controls.Add($label)
# create listbox for item to select
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 180
# create listbox items and add them to list, generaged from CSV columns
foreach($column in $csvColumns)
{
[void] $listBox.Items.Add($column)
}
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$ipCol = $listBox.SelectedItem
}
return $ipCol
}
# function to make call to API and write output to .csv file.
function Get-ARINInfo
{
param([string]$FileFullPath,
$ipList,
$outputSelection
)
$errorFile = 'ARIN_Elastic_Duplications_' + [string](Get-Date).Year + [string](Get-Date).Month + [string](Get-Date).Day + '_' + [string](Get-Date).Hour + - + [string](Get-Date).Minute + - + [string](Get-Date).Second + '.txt'
# Read in Elasticsearch database and port.
if($outputSelection -eq 2)
{
Write-Host("Enter the Elasticsearch database INSTANCE like the highlighted example: http://") -NoNewline
Write-Host("SomeTestDomain.net") -NoNewline -BackgroundColor Cyan
$databaseName = Read-Host(':9200 ')
Write-Host("Enter the Elasticsearch database PORT like the highlighted example: http://SomeTestDomain.net") -NoNewline
Write-Host("9200") -NoNewline -BackgroundColor Cyan
$databasePort = Read-Host(' ')
$connTestURL = 'http://' + $databaseName + ':' + $databasePort + '/'
try
{
$connTest = Invoke-WebRequest -Method Get -Uri $connTestURL
#If connection successful, test and set the existence of the arin_lookup_data index.
$ES_test_URL = 'http://' + $databaseName + ':' + $databasePort + '/arin_lookup_data'
try
{
$ES_index_exists = Invoke-WebRequest -Method Get -Uri $ES_test_URL -ErrorAction SilentlyContinue
}
catch
{
$ES_index_exists = $false
}
}
catch
{
$response = Read-Host("Failed to connect to database: $databaseName `nValidate the database name/address and port, then rerun script")
}
}
else
{
$FileName = 'ARIN_Lookup_' + [string](Get-Date).Year + [string](Get-Date).Month + [string](Get-Date).Day + '_' + [string](Get-Date).Hour + - + [string](Get-Date).Minute + - + [string](Get-Date).Second + '.csv'
$outputPath = 'C:\Temp\' + $FileName
}
if($ipList.Count -lt 1)
{
$outputPath = $outputPath.Replace('.csv','txt')
Write-Output("No IP found in provided data. Error log written to $outputPath")
"No IP was found in requested data from file $FileFullPath" | Out-File $outputPath
break
}
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add('Accept','application/json')
foreach($i in $ipList)
{
DO
{
$url = 'http://whois.arin.net/rest/ip/' + $i
$arinInfo = Invoke-WebRequest -Uri $url -Headers $header
if($arinInfo.StatusCode -ne 200 -or (($arinInfo.Content | ConvertFrom-Json).net.resources.limitExceeded.'$' -eq $true))
{
Start-Sleep 60
Write-Host("Rate limit exceeded, sleeping 60 seconds") -ForegroundColor Red
}
else
{
$ipInfo = New-Object PSObject -Property @{
Search_IP = $i
Registration_date = ($arinInfo.Content | ConvertFrom-Json).net.registrationDate.'$'
Update_date = ($arinInfo.Content | ConvertFrom-Json).net.updateDate.'$'
Registered_company_handle = ($arinInfo.Content | ConvertFrom-Json).net.customerRef.'@handle'
Registered_company_name = ''
Registered_company_ARIN_info = 'https://search.arin.net/rdap/?query=' + $i
Registered_company_city = ''
Registered_company_State = ''
Registered_company_Country = ''
Registered_company_Address = ''
Registered_company_PostalCode = ''
IP_range_start = ($arinInfo.Content | ConvertFrom-Json).net.startAddress.'$'
IP_range_end = ($arinInfo.Content | ConvertFrom-Json).net.endAddress.'$'
Block_Name = ($arinInfo.Content | ConvertFrom-Json).net.name.'$'
parent_Net_Reference = ($arinInfo.Content | ConvertFrom-Json).net.parentNetRef.'@name'
}
if(($arinInfo.Content | ConvertFrom-Json).net.orgRef.'$' -or ($arinInfo.Content | ConvertFrom-Json).net.customerRef.'$')
{
if(($arinInfo.Content | ConvertFrom-Json).net.orgRef.'$')
{
$arinCompanyInfo = Invoke-WebRequest (($arinInfo.Content | ConvertFrom-Json).net.orgRef.'$')
}
else
{
$arinCompanyInfo = Invoke-WebRequest (($arinInfo.Content | ConvertFrom-Json).net.customerRef.'$')
}
$ipInfo.Registered_company_name = $arinCompanyInfo.Content.Replace($arinCompanyInfo.Content.Substring($arinCompanyInfo.Content.IndexOf('<postalCode>')),'').Substring($arinCompanyInfo.Content.IndexOf('</handle>') + 15) -replace '</name>' | ForEach-Object{if($_.toString() -match '<'){$_.toString().Substring(0,$_.IndexOf('<'))}else{$_.toString()}}
$ipInfo.Registered_company_PostalCode = $arinCompanyInfo.Content.Replace($arinCompanyInfo.Content.Substring($arinCompanyInfo.Content.IndexOf('</postalCode>')),'').Substring($arinCompanyInfo.Content.IndexOf('<postalCode>') + 12)
$ipInfo.Registered_company_city = $arinCompanyInfo.Content.Replace($arinCompanyInfo.Content.Substring($arinCompanyInfo.Content.IndexOf('</city>')),'').Substring($arinCompanyInfo.Content.IndexOf('<city>') + 6)
$ipInfo.Registered_company_State = $arinCompanyInfo.Content.Replace($arinCompanyInfo.Content.Substring($arinCompanyInfo.Content.IndexOf('</iso3166-2>')),'').Substring($arinCompanyInfo.Content.IndexOf('<iso3166-2>') + 11)
$ipInfo.Registered_company_Country = $arinCompanyInfo.Content.Replace($arinCompanyInfo.Content.Substring($arinCompanyInfo.Content.IndexOf('</iso3166-1>')),'').Substring($arinCompanyInfo.Content.IndexOf('<iso3166-1>')).Replace('<iso3166-1><code2>','').Replace('</code2><code3>',' | ').Replace('</code3><name>',' | ').Replace('</name><e164*/e164>','') -replace '<\/name><e[0-9]+>[0-9]+<\/e[0-9]+>'
$ipInfo.Registered_company_Address = $arinCompanyInfo.Content.Replace($arinCompanyInfo.Content.Substring($arinCompanyInfo.Content.IndexOf('</line></streetAddress>')),'').Substring($arinCompanyInfo.Content.IndexOf('<streetAddress>')) -replace '<streetAddress><line number=\"[0-9]+\">' -replace '</line><line number="\d">',"`n"
}
else
{
$ipInfo.Registered_company_city = 'Information_Not_Found'
$ipInfo.Registered_company_State = 'Information_Not_Found'
$ipInfo.Registered_company_Country = 'Information_Not_Found'
$ipInfo.Registered_company_Address = 'Information_Not_Found'
}
switch($outputSelection)
{
#CSV output to file:
1
{
$ipInfo | Select-Object Search_IP,IP_range_start,IP_range_end,Registration_date,Update_date,Registered_company_name,Registered_company_Address,Registered_company_city,Registered_company_State,Registered_company_PostalCode,Registered_company_Country,Registered_company_handle,Registered_company_ARIN_info,Block_Name,parent_Net_Reference | Export-Csv $outputPath -NoTypeInformation -Append
}
#output to Elasticsearch:
2
{
#create the JSON for Elasticsearch
$arinData = $ipInfo | Select-Object Search_IP,IP_range_start,IP_range_end,Registration_date,Update_date,Registered_company_name,Registered_company_Address,Registered_company_city,Registered_company_State,Registered_company_PostalCode,Registered_company_Country,Registered_company_handle,Registered_company_ARIN_info,Block_Name,parent_Net_Reference | ConvertTo-Json
#create the Elasticsearch request string
$elasticsearchURL = 'http://' + $databaseName + ':' + "$databasePort/arin_lookup_data/IPData"
if($ES_index_exists -eq $false)
{
Invoke-WebRequest -Method Post -Uri 'http://localhost:9200/arin_lookup_data/IPData' -Body $arinData -ContentType 'application/json' | Out-Null
}
else
{
$ESSearchURL = $ES_test_URL + "/_search?q=$i"
$existingIPCount = (((Invoke-WebRequest -Method Get -Uri $ESSearchURL).content | ConvertFrom-Json).hits.total.value)
if($existingIPCount -eq 0)
{
Invoke-WebRequest -Method Post -Uri 'http://localhost:9200/arin_lookup_data/IPData' -Body $arinData -ContentType 'application/json' | Out-Null
}
else
{
$i | Out-File "C:\Temp\$errorFile" -Append
Write-Host("$i is already in Elasticsearch. Previously identified IPs written to C:\Temp\$errorFile")
}
}
}
}
Clear-Variable ipInfo
}
}WHILE($arinInfo.StatusCode -ne 200 -or (($arinInfo.Content | ConvertFrom-Json).net.resources.limitExceeded.'$' -eq $true))
}
if($outputSelection -eq 1)
{
Write-Host("`n`nAll ARIN information written to $outputPath. Path has been copied to CLIP BOARD.") -ForegroundColor Green
Set-Clipboard $outputPath
}
else
{
Write-Host("`n`nAll ARIN information should now be in your Elasticsearch database.")
}
}
#main function
function Get-IPInfo
{
# check if C:\Temp exists, if not, create it
Make-DefaultDirectory
$inputSel = Get-InputSelection
$outputSel = Set-OutputSelection
Write-Host($inputSel)
switch($inputSel)
{
1
{
$singleIP = Get-CommLineIP
Get-ARINInfo -ipList $singleIP -outputSelection $outputSel
}
2
{
$fInfo = Select-File 2
$textIP = Get-Content($fInfo.FullName) | Sort-Object -Unique
Get-ARINInfo -ipList $textIP -FileFullPath $fInfo.FullName -outputSelection $outputSel
}
3
{
$fInfo = Select-File 3
$csvFileInfo = Import-Csv($fInfo.FullName)
$csvColumns = ($csvFileInfo | Get-Member | Where-Object{$_.MemberType -ieq 'noteproperty'} | Select-Object Name).Name
$ipColumn = Get-IPField $csvColumns
$csvIPList = $csvFileInfo.$ipColumn | Sort-Object -Unique
Get-ARINInfo -ipList $csvIPList -FileFullPath $fInfo.FullName -outputSelection $outputSel
}
4
{
$fInfo = Select-file 4
Write-Host("`n`nBeginning to import file, larger files can take additional time") -Foregroundcolor Cyan
$rawFileInfo = Get-Content($fInfo.FullName)
Write-Host("`n`nBeginning to parse data for IP match, this may take some time") -ForegroundColor Cyan
$rawFileIPs = ($rawFileInfo | Select-String -Pattern '(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' -AllMatches).Matches.Value
$rawIPList = $rawFileIPs | Sort-Object -Unique
Get-ARINInfo $rawIPList -FileFullPath $fInfo.FullName -outputSelection $outputSel
}
}
}
#Run main function
Get-IPInfo