Skip to content

Instantly share code, notes, and snippets.

@jonathanduke
Created June 10, 2022 00:09
Show Gist options
  • Save jonathanduke/10f6c05223847fed322d37d713305cd2 to your computer and use it in GitHub Desktop.
Save jonathanduke/10f6c05223847fed322d37d713305cd2 to your computer and use it in GitHub Desktop.
C# code to read a binary DD-WRT backup file and export commands to set the values on the router
// read a DD-WRT nvrambak_xyz.bin backup file and create "nvram set" commands FOR COMPARISON ONLY
// NOTE: some values may have issues, so you can't just copy and paste the whole output or it can mess up your router
// The format should be similar to processes to export from the router via the command line:
// https://gist.github.com/Aikhjarto/5b1c6b5e5d373d8d60c004e075b29acc
// https://gist.github.com/rambotech/d1e2486228c5e972c3c9c8936920f2fc
var set = new List<string>();
using var file = System.IO.File.OpenRead(args[0]);
var buffer = new byte[0xFFFF];
int len = 6;
int count = file.Read(buffer, 0, len);
if (count != len || System.Text.Encoding.ASCII.GetString(buffer, 0, count) != "DD-WRT") throw new InvalidDataException("unrecognized format");
len = (file.ReadByte() | file.ReadByte() << 8);
while (-1 != (len = file.ReadByte()))
{
count = file.Read(buffer, 0, len);
if (count != len) throw new InvalidDataException("unexpected end of name");
var name = System.Text.Encoding.ASCII.GetString(buffer, 0, count);
len = (file.ReadByte() | file.ReadByte() << 8);
if (len > 0)
{
count = file.Read(buffer, 0, len);
if (count != len) throw new InvalidDataException("unexpected end of value");
var value = System.Text.Encoding.ASCII.GetString(buffer, 0, count);
set.Add($"nvram set {name}='{value.Replace("'", "\\'")}'");
}
}
set.Sort();
foreach (var cmd in set) Console.WriteLine(cmd);
@jonathanduke
Copy link
Author

jonathanduke commented Apr 4, 2023

Project file to use dotnet run:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
</Project>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment