Advertisement
Guest User

https://github.com/VladDBA/Asus-Router-Config-Decoder

a guest
Nov 5th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3.  
  4. Decodes the .cfg file resulted from backing up the configuration of an Asus Router
  5.  
  6. .NOTES
  7. Author: Vlad Drumea (VladDBA)
  8. Website: https://vladdba.com/
  9.  
  10. Based on this Bash script:
  11. https://github.com/billchaison/asus-router-decoder
  12.  
  13. .LINK
  14. https://github.com/VladDBA/Asus-Router-Config-Decoder
  15.  
  16. .EXAMPLE
  17. PS>.\Decode-AsusRouterConfig.ps1 '.\Settings_RT-AX86U Pro.CFG'
  18.  
  19. .EXAMPLE
  20. PS>.\Decode-AsusRouterConfig.ps1 'C:\Users\SomeUser\Documents\Settings_RT-AX86U Pro.CFG'
  21.  
  22. #>
  23. [cmdletbinding()]
  24. param (
  25. [Parameter(Position = 0, Mandatory = $true)]
  26. [string]$File
  27. )
  28.  
  29. if (-Not (Test-Path $File)) {
  30. Write-Host " Provide the ASUS router config file name." -Fore Red
  31. Exit
  32. }
  33. # Resolve file path in case of relative path
  34. $File = Get-Item -Path $File | Select-Object -ExpandProperty FullName -ErrorAction Stop
  35.  
  36. $Size = (Get-Item $File).length
  37.  
  38. if ($Size -lt 10) {
  39. Write-Host " File size is too small." -Fore Red
  40. Exit
  41. }
  42. try {
  43. $FileData = [System.IO.File]::ReadAllBytes($File) | ForEach-Object { "{0:x2}" -f $_ }
  44. }
  45. catch {
  46. Write-Host " Cannot read file." -Fore Red
  47. Write-Host " Try providing the full file path."
  48. Exit
  49. }
  50.  
  51. if ($FileData.Count -ne $Size) {
  52. Write-Host " File read error." -Fore Red
  53. Exit
  54. }
  55. elseif ($FileData[0] -ne "48" -or $FileData[1] -ne "44" -or $FileData[2] -ne "52" -or $FileData[3] -ne "32") {
  56. Write-Host " File header check failed." -Fore Red
  57. Exit
  58. }
  59. else {
  60.  
  61. $DataLength = "$($FileData[6])$($FileData[5])$($FileData[4])"
  62. $DataLength = [convert]::ToInt32($DataLength, 16)
  63.  
  64. if ($DataLength -ne ($Size - 8)) {
  65. Write-Host " Data length check failed."
  66. Exit
  67. }
  68. else {
  69. Write-Host " Configuration file appears to be valid."
  70. }
  71. }
  72.  
  73. $Rand = [convert]::ToInt32($FileData[7], 16)
  74. $i = 8
  75. $DecodedBytes = New-Object System.Collections.Generic.List[byte]
  76.  
  77. Write-Host " Decoding configuration file..."
  78. while ($i -lt $Size) {
  79. $CurrentByte = [convert]::ToInt32($FileData[$i], 16)
  80. if ($CurrentByte -gt 252) {
  81. if ($i -gt 8 -and $FileData[$i - 1] -ne "00") {
  82. $DecodedBytes.Add(0x00)
  83. }
  84. }
  85. else {
  86. $B = 0xff + $Rand - $CurrentByte
  87. $DecodedBytes.Add([byte]$B)
  88. }
  89. $i++
  90. }
  91.  
  92. # Convert decoded bytes to a string and replace the null character with a new line
  93. $DecodedString = -join ($DecodedBytes | ForEach-Object { if ($_ -eq 0) { "`n" } else { [char]$_ } })
  94.  
  95. # Write the decoded string to the output file
  96. $OutputFile = $File -ireplace '.cfg', '_Decoded.txt'
  97. $DecodedString | Out-File -FilePath "$OutputFile" -Encoding ascii -Force
  98. Write-Host " ->Decoded configuration file has been saved to:"
  99. Write-Host " $OutputFile" -Fore Green
  100. # Export dhcp_staticlist
  101. $FoundInfo = Select-String -Path $OutputFile -Pattern 'dhcp_staticlist=.+'
  102. $FoundInfo = $FoundInfo -Replace ".+Decoded\.txt:[0-9]+:dhcp_staticlist=", ""
  103. if($FoundInfo.Length -gt 0){
  104. Write-Host " Found DHCP client list"
  105. $Header = " MAC | IP | HostName "
  106. $FoundInfo = $FoundInfo -replace "<","`n" -replace ">>", " | " -replace ">"," | "
  107. $FoundInfo = "$Header$FoundInfo"
  108. $DHCPFile = $File -ireplace '.cfg', '_DHCP.txt'
  109.  
  110. $FoundInfo | Out-File -FilePath "$DHCPFile" -Encoding ascii -Force
  111. Write-Host " ->DHCP client list has been saved to:"
  112. Write-Host " $DHCPFile" -Fore Green
  113. }
  114. # Retrieve admin username & password, and any configured SSID and password
  115. Write-Host " ->Attempting to identify:`n HTTP (admin) username & password`n PPPOE credentials`n SSIDs (Wi-Fi names)`n WPA PSKs (Wi-Fi passwords)"
  116. $FoundInfo = Select-String -Path $OutputFile -Pattern '_wpa_psk=.+|wl.*_ssid=.+|http_passwd=.+|http_username=.+|pppoe_passwd=.+|pppoe_username=.+'
  117.  
  118. # Cleanup output for PS versions older than 7
  119. Write-Host $("=" * 60) -Fore Green
  120. $FoundInfo -Replace ".+Decoded\.txt:[0-9]+:", ""
  121. Write-Host $("=" * 60) -Fore Green
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement