-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_net.ps1
218 lines (180 loc) · 6.51 KB
/
fake_net.ps1
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
<#
.SYNOPSIS
Configures DNS server with specific zones and A records. Assume DNS on Windows exists.
.DESCRIPTION
- Creates DNS zones with specified replication scopes.
- Adds A records to the created DNS zones.
- Provides detailed verbose logging for each step.
.EXAMPLE
.\fake_net.ps1 -Verbose
.NOTES
File Name : fake_net.ps1.ps1
Author : @packetmonk (@packetalien on GitHub)
Requires : PowerShell 5.1 or higher
Version : 1.0
Date : 30 JAN 2025
Ensure you run this with administrative privileges. This script assumes you have the necessary permissions to install features and manage DNS zones.
.LINK
None
#>
# Set error handling to stop script on error
$ErrorActionPreference = "Stop"
# Function to log messages with timestamp
function Write-Log {
param (
[string]$Message
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Output "[$timestamp] $Message"
}
<#
.SYNOPSIS
Checks if the DNS Server service is installed on the system.
.DESCRIPTION
This function checks to see if the DNS Server feature is installed.
If the DNS Server service is not detected, it logs a message and
exits the script. This ensures that DNS-related operations are
performed only when the necessary service is available.
.EXAMPLE
Check-DNSService -Verbose
This command checks for the DNS Server service and outputs verbose logging.
#>
function Check-DNSService {
[CmdletBinding()]
param()
Write-Verbose "Checking for DNS Server service..."
try {
$dnsFeature = Get-WindowsFeature -Name DNS
if ($dnsFeature -and $dnsFeature.Installed) {
Write-Verbose "DNS Server service is installed and available."
} else {
Write-Verbose "DNS Server service is not installed."
Write-Error "The DNS Server service is not installed. Exiting script."
exit 1
}
} catch {
Write-Error "An error occurred while checking for DNS Server service: $_"
exit 1
}
}
<#
.SYNOPSIS
Creates a new DNS Primary Zone.
.DESCRIPTION
This function adds a new primary DNS zone with the specified replication scope.
.PARAMETER ZoneName
The name of the DNS zone to create.
.PARAMETER ReplicationScope
The replication scope for the zone, e.g., 'Domain', 'Forest', or 'Legacy'.
.EXAMPLE
Update-DNSZone -ZoneName "example.com" -ReplicationScope "Domain"
#>
function Update-DNSZone {
param (
[Parameter(Mandatory=$true)]
[string]$ZoneName,
[Parameter(Mandatory=$true)]
[string]$ReplicationScope
)
Write-Log "Creating DNS zone $ZoneName with replication scope $ReplicationScope..."
Add-DnsServerPrimaryZone -Name $ZoneName -ReplicationScope $ReplicationScope -Verbose
}
<#
.SYNOPSIS
Adds an A record to a DNS zone.
.DESCRIPTION
Adds an A (Address) record to the specified DNS zone.
.PARAMETER ZoneName
The DNS zone where the A record will be added.
.PARAMETER RecordName
The name of the A record.
.PARAMETER RecordIP
The IP address associated with the A record.
.EXAMPLE
Add-ARecord -ZoneName "example.com" -RecordName "www" -RecordIP "192.168.1.1"
#>
function Add-ARecord {
param (
[Parameter(Mandatory=$true)]
[string]$ZoneName,
[Parameter(Mandatory=$true)]
[string]$RecordName,
[Parameter(Mandatory=$true)]
[string]$RecordIP
)
Write-Log "Adding A record $RecordName with IP $RecordIP to zone $ZoneName..."
Add-DnsServerResourceRecordA -ZoneName $ZoneName -Name $RecordName -IPv4Address $RecordIP -Verbose
}
<#
.SYNOPSIS
Imports DNS zone data from a CSV file and structures it into an array.
.DESCRIPTION
This function reads a CSV file containing DNS zone information, constructs
an array where each element represents a DNS zone with its associated
records. The function uses verbose logging to detail each step of the
import process.
.PARAMETER Path
The full path to the CSV file containing the DNS zone data.
.RETURNS
An array of hashtables, each representing a DNS zone with its records.
.EXAMPLE
$dnsZones = Import-DNSZonesFromCSV -Path "C:\dnsZones.csv" -Verbose
This command imports DNS zones from dnsZones.csv, outputs verbose logging,
and stores the result in $dnsZones.
.NOTES
The CSV should have columns named "ZoneName", "Replication", "RecordName",
and "IPAddress". This function assumes the CSV adheres to this structure.
#>
function Import-DNSZonesFromCSV {
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
[string]$Path = ".\fake_net.csv"
)
Write-Verbose "Starting import of DNS zones from CSV."
try {
Write-Verbose "Reading CSV file from path: $Path"
$importedData = Import-Csv -Path $Path
$dnsZones = @()
$currentZoneName = $null
Write-Verbose "Processing CSV entries..."
foreach ($row in $importedData) {
if ($currentZoneName -ne $row.ZoneName) {
Write-Verbose "Adding new zone: $($row.ZoneName)"
$currentZoneName = $row.ZoneName
$dnsZones += @{
name = $row.ZoneName
replication = $row.Replication
records = @()
}
}
# Find the correct zone to add the record to
$zone = $dnsZones | Where-Object { $_.name -eq $row.ZoneName }
Write-Verbose "Adding record $($row.RecordName) with IP $($row.IPAddress) to zone $($row.ZoneName)"
$zone.records += @{ name = $row.RecordName; ip = $row.IPAddress }
}
Write-Verbose "Finished processing CSV. Total zones imported: $($dnsZones.Count)"
return $dnsZones
} catch {
Write-Error "An error occurred while importing DNS zones from CSV: $_"
return $null
}
}
# Main script execution starts here
try {
# Update the system and install necessary features
Check-DNSService
# DNS zones and A records configuration
$dnsZones = Import-DNSZonesFromCSV -Path .\fake_net.csv -Verbose
# Create DNS zones and add A records
foreach ($zone in $dnsZones) {
Update-DNSZone -ZoneName $zone.name -ReplicationScope $zone.replication
foreach ($record in $zone.records) {
Add-ARecord -ZoneName $zone.name -RecordName $record.name -RecordIP $record.ip
}
}
Write-Log "DNS configuration completed successfully!"
} catch {
Write-Log "An error occurred: $_"
exit 1
}