-
Notifications
You must be signed in to change notification settings - Fork 1
/
Decode-AsusRouterConfig.ps1
121 lines (102 loc) · 3.94 KB
/
Decode-AsusRouterConfig.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
<#
.SYNOPSIS
Decodes the .cfg file resulted from backing up the configuration of an Asus Router
.NOTES
Author: Vlad Drumea (VladDBA)
Website: https://vladdba.com/
Based on this Bash script:
https://github.com/billchaison/asus-router-decoder
.LINK
https://github.com/VladDBA/Asus-Router-Config-Decoder
.EXAMPLE
PS>.\Decode-AsusRouterConfig.ps1 '.\Settings_RT-AX86U Pro.CFG'
.EXAMPLE
PS>.\Decode-AsusRouterConfig.ps1 'C:\Users\SomeUser\Documents\Settings_RT-AX86U Pro.CFG'
#>
[cmdletbinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[string]$File
)
if (-Not (Test-Path $File)) {
Write-Host " Provide the ASUS router config file name." -Fore Red
Exit
}
# Resolve file path in case of relative path
$File = Get-Item -Path $File | Select-Object -ExpandProperty FullName -ErrorAction Stop
$Size = (Get-Item $File).length
if ($Size -lt 10) {
Write-Host " File size is too small." -Fore Red
Exit
}
try {
$FileData = [System.IO.File]::ReadAllBytes($File) | ForEach-Object { "{0:x2}" -f $_ }
}
catch {
Write-Host " Cannot read file." -Fore Red
Write-Host " Try providing the full file path."
Exit
}
if ($FileData.Count -ne $Size) {
Write-Host " File read error." -Fore Red
Exit
}
elseif ($FileData[0] -ne "48" -or $FileData[1] -ne "44" -or $FileData[2] -ne "52" -or $FileData[3] -ne "32") {
Write-Host " File header check failed." -Fore Red
Exit
}
else {
$DataLength = "$($FileData[6])$($FileData[5])$($FileData[4])"
$DataLength = [convert]::ToInt32($DataLength, 16)
if ($DataLength -ne ($Size - 8)) {
Write-Host " Data length check failed."
Exit
}
else {
Write-Host " Configuration file appears to be valid."
}
}
$Rand = [convert]::ToInt32($FileData[7], 16)
$i = 8
$DecodedBytes = New-Object System.Collections.Generic.List[byte]
Write-Host " Decoding configuration file..."
while ($i -lt $Size) {
$CurrentByte = [convert]::ToInt32($FileData[$i], 16)
if ($CurrentByte -gt 252) {
if ($i -gt 8 -and $FileData[$i - 1] -ne "00") {
$DecodedBytes.Add(0x00)
}
}
else {
$B = 0xff + $Rand - $CurrentByte
$DecodedBytes.Add([byte]$B)
}
$i++
}
# Convert decoded bytes to a string and replace the null character with a new line
$DecodedString = -join ($DecodedBytes | ForEach-Object { if ($_ -eq 0) { "`n" } else { [char]$_ } })
# Write the decoded string to the output file
$OutputFile = $File -ireplace '.cfg', '_Decoded.txt'
$DecodedString | Out-File -FilePath "$OutputFile" -Encoding ascii -Force
Write-Host " ->Decoded configuration file has been saved to:"
Write-Host " $OutputFile" -Fore Green
# Export dhcp_staticlist
$FoundInfo = Select-String -Path $OutputFile -Pattern 'dhcp_staticlist=.+'
$FoundInfo = $FoundInfo -Replace ".+Decoded\.txt:[0-9]+:dhcp_staticlist=", ""
if($FoundInfo.Length -gt 0){
Write-Host " Found DHCP client list"
$Header = " MAC | IP | HostName "
$FoundInfo = $FoundInfo -replace "<","`n" -replace ">>", " | " -replace ">"," | "
$FoundInfo = "$Header$FoundInfo"
$DHCPFile = $File -ireplace '.cfg', '_DHCP.txt'
$FoundInfo | Out-File -FilePath "$DHCPFile" -Encoding ascii -Force
Write-Host " ->DHCP client list has been saved to:"
Write-Host " $DHCPFile" -Fore Green
}
# Retrieve admin username & password, and any configured SSID and password
Write-Host " ->Attempting to identify:`n HTTP (admin) username & password`n PPPOE credentials`n SSIDs (Wi-Fi names)`n WPA PSKs (Wi-Fi passwords)"
$FoundInfo = Select-String -Path $OutputFile -Pattern '_wpa_psk=.+|wl.*_ssid=.+|http_passwd=.+|http_username=.+|pppoe_passwd=.+|pppoe_username=.+'
# Cleanup output for PS versions older than 7
Write-Host $("=" * 60) -Fore Green
$FoundInfo -Replace ".+Decoded\.txt:[0-9]+:", ""
Write-Host $("=" * 60) -Fore Green