This commit is contained in:
Kris Clough 2023-11-06 16:32:26 -05:00
parent 495daf5168
commit 02c789eafc
2 changed files with 49 additions and 41 deletions

View File

@ -64,7 +64,7 @@ for ($subnetOctet1 = $startRange1stOctet; $subnetOctet1 -le $endRange1stOctet; $
for ($subnetOctet2 = $startRange2ndOctet; $subnetOctet2 -le $endRange2ndOctet; $subnetOctet2++) {
for ($subnetOctet3 = $startRange3rdOctet; $subnetOctet3 -le $endRange3rdOctet; $subnetOctet3++) {
for ($subnetOctet4 = $startRange4thOctet; $subnetOctet4 -le $endRange4thOctet; $subnetOctet4++) {
$targetIP = "$subnetOctet1.$subnetOctet2.$subnetOctet3.$subnetOctet4"
$targetIPs += "$subnetOctet1.$subnetOctet2.$subnetOctet3.$subnetOctet4"
# Check if the host is reachable
$isReachable = Test-Connection -ComputerName $targetIP -Count 1 -ErrorAction SilentlyContinue
@ -85,7 +85,7 @@ for ($subnetOctet1 = $startRange1stOctet; $subnetOctet1 -le $endRange1stOctet; $
IP = $targetIP
MAC = $targetmacAddress
}
}
}
else {
$targetSMB = "No"
}
@ -95,12 +95,12 @@ for ($subnetOctet1 = $startRange1stOctet; $subnetOctet1 -le $endRange1stOctet; $
MAC = $targetmacAddress
Windows = $targetSMB
}
}
}
}
}
}
}
}
###########
#
# Multithreading WIP Code

View File

@ -48,11 +48,11 @@ $startRange2ndOctet = 168
$endRange2ndOctet = 168
# Define the range for the 3rd octet (adjust as needed)
$startRange3rdOctet = 1
$endRange3rdOctet = 1
$startRange3rdOctet = 001
$endRange3rdOctet = 001
# Define the range for the 4th octet (adjust as needed)
$startRange4thOctet = 1
$startRange4thOctet = 001
$endRange4thOctet = 254
# Define the arrays to store discovered machines
@ -74,43 +74,46 @@ for ($subnetOctet1 = $startRange1stOctet; $subnetOctet1 -le $endRange1stOctet; $
}
}
}
# Specify the length of each sub-list
$batchLength = 20
# Initialize array to store sublists
# Initialize arrays to store sublists
$targetBatches = @()
$batch = @()
# Loop through the original list and create sub-lists
$targets | ForEach-Object {}
for ($i = 0; $i -lt $targets.Count; $i += $batchLength) {
$batchList = $targets[$i..($i + $batchLength - 1)].IP
$batch += [PSCustomObject]@{
IPlist = $batchList
}
$targetBatches += $batch
1..($batchNumber) | ForEach-Object {
$targetBatches += @(($_),($_+1))
}
# Loop through the original list and add addresses to the empty $targetBatches lists.
$targetBatches | ForEach-Object {
# Add a batch of addresses to the current list
$_ += $targets[0..($batchLength)]
# Remove the corresponding objets from $targets so the next batch is not the same as the first.
$targets = $targets -notmatch $_
}
#$targets | ForEach-Object {}
#for ($i = 0; $i -lt $targets.Count) {
# $batchList = $targets[$i..($i + $batchLength - 1)].IP
# $batch += [PSCustomObject]@{
# IPlist = $batchList
# }
# $targetBatches += $batch
#}
# Initialize output arrays
$neigborMachines = @()
# Script block to process each sub-list
$discoverScript = {
param ($ipList)
param ($targetIP)
# Add your processing logic here
$ipList | ForEach-Object {
$targetIP = $_
$smbResult = Test-NetConnection -ComputerName $targetIP -Port 445 -ErrorAction Continue
$dnsResult = Test-NetConnection -ComputerName $targetIP -Port 53 -ErrorAction Continue
$ftpResult = Test-NetConnection -ComputerName $targetIP -Port 21 -ErrorAction Continue
$sshResult = Test-NetConnection -ComputerName $targetIP -Port 22 -ErrorAction Continue
$targetMac = Get-NetNeighbor -IPAddress $targetIP
$neighborMachines += [PSCustomObject]@{
isWindowsMachine = $smbResult.TcpTestSucceeded
isDnsServer = $dnsResult.TcpTestSucceeded
isFtpServer = $ftpResult.TcpTestSucceeded
isSshServer = $sshResult.TcpTestSucceeded
macAddress = $targetMac.LinkLayerAddress
}
$smbResult = Test-NetConnection -ComputerName $targetIP -Port 445 -ErrorAction Continue
$dnsResult = Test-NetConnection -ComputerName $targetIP -Port 53 -ErrorAction Continue
$ftpResult = Test-NetConnection -ComputerName $targetIP -Port 21 -ErrorAction Continue
$sshResult = Test-NetConnection -ComputerName $targetIP -Port 22 -ErrorAction Continue
$targetMac = Get-NetNeighbor -IPAddress $targetIP
$neighborMachines += [PSCustomObject]@{
isWindowsMachine = $smbResult.TcpTestSucceeded
isDnsServer = $dnsResult.TcpTestSucceeded
isFtpServer = $ftpResult.TcpTestSucceeded
isSshServer = $sshResult.TcpTestSucceeded
macAddress = $targetMac.LinkLayerAddress
}
}
@ -131,13 +134,18 @@ $targetBatches | Export-Csv -Path D:/discovery-data/test/targetbatches.csv -NoTy
#}
$targetBatches | ForEach-Object {
$job = Start-Job -ScriptBlock $discoverScript -ArgumentList $batch.IPlist
Write-Host "Queueing job $job ID $batch"
$jobManager += $job
if ($jobManager.Count -ge 50) {
$completedJob = Wait-Job -Job $jobManager -Any
$jobManager = $jobManager | Where-Object { $_ -ne $completedJob }
}
Write-Host "Current Batch:"
$batchID = $_[0].IP
Write-Host $batchID
foreach ($target in $_) {
$job = Start-Job -Name $batchID -ScriptBlock $discoverScript -ArgumentList $target.IP
Write-Host "Queueing job $batchID"
$jobManager += $job
if ($jobManager.Count -ge 50) {
$completedJob = Wait-Job -Job $jobManager -Any
$jobManager = $jobManager | Where-Object { $_ -ne $completedJob }
}
}
}