是否要编辑dd-wrt-nvram.bin备份(设置备份)文件



有什么方法可以编辑.bin备份吗?我发现nirosoft的程序名为";routerpassview";它解密得很好,但不知道如何再次加密到.bin文件并恢复设置

十进制十六进制描述

84 0x54 Zlib压缩数据,默认压缩

这似乎毫无意义,但我需要它来禁用模拟锁定

这里有一个我用来做这些事情的php脚本。希望能有所帮助:

#!/usr/bin/php
<?PHP
/*
* phpnvram - command line editor for DD-WRT nvram-backup
* Copyright (C) 2015 Markus Quint <markus@dd-wrt.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
**/
global $argc, $argv;
if (php_sapi_name() == "cli")
cli();
function cli()
{
global $argc, $argv;
$CMD = array(
//  command => count parameter
"get"   => 1,
"rget"  => 1,
"fget"  => 2,   // get parameter to file
"set"   => 1,
"fset"  => 2,   // set parameter from file
"unset" => 1,
"show"  => 0,
"showkeys"  => 0,
"diff"  => 1,   // diff bewtween two files
"vdiff" => 1,   // verbose diff
"rdiff" => 2,   // diff values matching a regualr expression
);
if ($argc < 3 || !file_exists($argv[1]))
cli_help();
if (!array_key_exists($argv[2], $CMD))
cli_help();
if ( ($argc - 3) < $CMD[$argv[2]] )
cli_help();
$nvrambak = new nvram($argv[1]);
if ($nvrambak->get_errno())
echo $nvrambak->get_errmsg($nvrambak->get_errno());
$nvram = $nvrambak->nvram_getconfig();
$count = $argc - (($argc - 3) - ($CMD[$argv[2]] - 1));
for ($i=$argc; $i>$count; $i--)
$pattern[] = $argv[$i-1];
switch ($argv[2]) {
case "get":
$nvrambak->cli_get($nvram, $argv[3]);
break;
case "rget":
$nvrambak->cli_rget($nvram, $pattern);
break;
case "fget":
$nvrambak->cli_fget($nvram, $argv[3], $argv[4]);
break;
case "set":
$nvrambak->cli_set($nvram, $argv[3]);
break;
case "fset":
$nvrambak->cli_fset($nvram, $argv[3], $argv[4]);
break;
case "unset":
$nvrambak->cli_unset($nvram, $argv[3]);
break;
case "show":
$nvrambak->cli_show($nvram);
break;
case "showkeys":
$nvrambak->cli_keys($nvram);
break;
case "diff":
$nvrambak->cli_diff($nvram, $argv[3]);
break;
case "vdiff":
$nvrambak->cli_diff($nvram, $argv[3], true);
break;
case "rdiff":
$nvrambak->cli_rdiff($nvram, $argv[3], $pattern);
break;
}
if ($nvrambak->get_errno())
echo $nvrambak->get_errmsg($nvrambak->get_errno());
}
function cli_help($cmd="")
{
global $argv;
printf("Usage:n%s <nvrambak.bin> %sn %37s%sn %37s%sn %37s%sn %37s%sn %37s%sn %37s%sn %37s%sn %37s%sn %37s%sn %37s%sn",
$argv[0],
"[get name]",
" ", "[rget regex]",
" ", "[fget name <target-file>]", 
" ", "[set name=value]",
" ", "[fset name <source-file>]",
" ", "[unset name]",
" ", "[show]",
" ", "[showkeys]",
" ", "[diff <nvrambak.bin>]",
" ", "[vdiff <nvrambak.bin>]",
" ", "[rdiff <nvrambak.bin> regex]"
);
exit();
}
class nvram
{
const FILE_HEAD = "DD-WRT";
private $backup_fname;
private $backup_fhandle;
private $backup_config = array();
public function __construct($backup_fname="nvrambak.bin") 
{
if ( !($handle = $this->backup_fread($backup_fname, $this->backup_config)) )
return;
$this->backup_fname = $backup_fname;
$this->backup_fhandle = $handle;
$this->backup_fclose();
$this->set_error($this->ERR_NO["SUCCESS"]);
}
/* core functions  **********
*
*/
private function backup_fclose()
{
if ($this->backup_fhandle) {
fclose($this->backup_fhandle);
$this->backup_fhandle = NULL;
}
}
private function backup_fread($fname, &$config_buff)
{
if (!$fname || !is_array($config_buff) )
return 0;
if (!file_exists($fname)) {
$this->set_error($this->ERR_NO["DONT_EXISTS"]);
return 0;
}
$fhandle = fopen($fname, "rb");
if (!$fhandle) {
$this->set_error($this->ERR_NO["CANT_OPENR"]);
return 0;
}
$len = filesize($fname);
$fhead = fread($fhandle, strlen(self::FILE_HEAD));
if ($fhead != self::FILE_HEAD) {
$this->set_error($this->ERR_NO["NOT_VALID"]);
return 0;
}
$len -= 6;
$byte = fread($fhandle, 1);
$data = unpack("H*", $byte);
$byte = base_convert($data[1], 16, 10);
$count = $byte;
$byte = fread($fhandle, 1);
$data = unpack("H*", $byte);
$byte = base_convert($data[1], 16, 10);
$count += ($byte << 8);
$len -= 2;
for ($i=0; $i < $count && $len > 0; $i++)
{
$byte = fread($fhandle, 1);
$data = unpack("H*", $byte);
$slen = base_convert($data[1], 16, 10);
if ($slen == 0) { // workaround for buggy nvram format
$this->set_error($this->ERR_NO["SUCCESS_WERR"]);
fread($fhandle, 2);
$len -= 3;
continue;
}
$name = sprintf("%s", fread($fhandle, $slen));
$len -= ($slen + 1);
$byte = fread($fhandle, 1);
$data = unpack("H*", $byte);
$byte = base_convert($data[1], 16, 10);
$slen = $byte;
$byte = fread($fhandle, 1);
$data = unpack("H*", $byte);
$byte = base_convert($data[1], 16, 10);
$slen += ($byte << 8);
$len -= ($slen + 2);
if ($slen > 0)
$value = fread($fhandle, $slen);
else
$value = "";
$config_buff[$name] = $value;
}
if ($this->get_errno() != $this->ERR_NO["SUCCESS_WERR"])
$this->set_error($this->ERR_NO["SUCCESS"]);
return $fhandle;
}
private function backup_fwrite($fname, $config)
{
$fhandle = fopen($fname, "wb");
if (!$fhandle) {
$this->set_error($this->ERR_NO["CANT_OPENW"]);
return;
}
fwrite($fhandle, self::FILE_HEAD, strlen(self::FILE_HEAD));
$count = count($config);
$data = base_convert($count & 255, 10, 16);
fwrite($fhandle, pack("H" . 2, sprintf("%02s", $data)));
$data = base_convert($count >> 8, 10, 16);
fwrite($fhandle, pack("H" . 2, sprintf("%02s", $data)));
foreach ($config AS $name => $value) {
$len = strlen($name);
$data = base_convert($len, 10, 16);
fwrite($fhandle, pack("H" . 2, sprintf("%02s", $data)));
fwrite($fhandle, $name);
$len = strlen($value);
$data = base_convert($len & 255, 10, 16);
fwrite($fhandle, pack("H" . 2, sprintf("%02s", $data)));
$data = base_convert($len >> 8, 10, 16);
fwrite($fhandle, pack("H" . 2, sprintf("%02s", $data)));
fwrite($fhandle, $value);
}
fclose($fhandle);
$this->set_error($this->ERR_NO["SUCCESS"]);
}
private function shadow($password)
{
$hash = '';
for($i=0;$i<8;$i++)
{
$j = mt_rand(0,53);
if($j<26)$hash .= chr(rand(65,90));
else if($j<52)$hash .= chr(rand(97,122));
else if($j<53)$hash .= '.';
else $hash .= '/';
}
return crypt($password,'$1$'.$hash.'$');
}
/* programming interface **********
*
*/
public function nvram_writeconfig($config, $fname)
{
if(!$fname OR !$config OR !is_array($config))
return;
$this->backup_fwrite($fname, $config);
}
public function nvram_changevalues(&$nvram, $changes)
{
if (!$nvram OR !$changes)
return;
foreach ($changes AS $name => $value) {
if ($value === NULL)
unset($nvram[$name]);
else
$nvram[$name] = $value;
}
}
public function nvram_getconfig()
{
if (count($this->backup_config))
return $this->backup_config;
}
public function nvram_rget($nvram, $pattern)
{
if (!$nvram || !$pattern)
return;
$config = $this->nvram_getconfig();
foreach($config AS $name => $value) {
for ($i=0; $i<count($pattern); $i++)
if (preg_match($pattern[$i], $name))
$result[$name] = $value;
}
return $result;
}
public function nvram_diff($nvram, $diff_fname)
{
$diff_config = array();
$this->backup_fread($diff_fname, $diff_config);
if ($this->get_errno()) {
echo $this->get_errmsg($this->get_errno());
}
$result = array("identic" => array(),
"different" => array(),
"orig_only" => array(),
"diff_only" => array(),
);
foreach($nvram AS $name => $value)
{
if(!array_key_exists($name, $diff_config))
$result["orig_only"][$name] = $value;
else {
if ($nvram[$name] == $diff_config[$name])
$result["identic"][$name] = $value;
else {
$result["different"][$name][$this->backup_fname] = $value;
$result["different"][$name][$diff_fname] = $diff_config[$name];
}
}
}
foreach ($diff_config AS $name => $value) {
if (!array_key_exists($name, $nvram))
$result["diff_only"][$name] = $value;
}
return $result;
}
public function nvram_rdiff($nvram, $diff_fname, $pattern)
{
if (!$nvram || !$diff_fname || !$pattern)
return;
$full_diff = $this->nvram_diff($nvram, $diff_fname);
foreach($full_diff AS $class => $list) {
foreach($list AS $name => $value) {
for ($i=0; $i<count($pattern); $i++)
if (preg_match($pattern[$i], $name))
$result[$class][$name] = $value;
}
}
return $result;
}
public function nvram_fromfile($fname)
{
if (!$fname)
return;
if (!file_exists($fname)) {
$this->set_error($this->ERR_NO["FILE_EXISTS"]);
return;
}
$fhandle = fopen($fname, "rb");
if (!$fhandle) {
$this->set_error($this->ERR_NO["CANT_OPENR"]);
return;
}
$value = fread($fhandle, filesize($fname));
$this->set_error($this->ERR_NO["SUCCESS"]);
fclose($fhandle);
return $value;
}

/* commandline functions **********
*
*/
public function cli_get($nvram, $name)
{
if (!$nvram OR !$name)
return;
if (count($nvram))
printf("%sn", $nvram[$name]);
}
public function cli_rget($nvram, $pattern)
{
if (!$nvram || !$pattern)
return;
$result = $this->nvram_rget($nvram, $pattern);
print_r($result);
}
public function cli_fget($nvram, $name, $fname)
{
if (!$name || !$fname)
return;
if (file_exists($fname)) {
$this->set_error($this->ERR_NO["FILE_EXISTS"]);
return;
}
$fhandle = fopen($fname, "wb+");
if (!$fhandle) {
$this->set_error($this->ERR_NO["CANT_OPENW"]);
return;
}
if ( fwrite($fhandle, $nvram[$name], strlen($nvram[$name])) )
$this->set_error($this->ERR_NO["SUCCESS"]);
fclose($fhandle);
return;
}
public function cli_set(&$nvram, $namevalue)
{
if (!$nvram OR !$namevalue)
return;
if (count($nvram))
{
// gibts nicht mehr seit php7
// $set = split("=", $namevalue);
$set = explode("=", $namevalue,2);
$name = $set[0];
$value = substr($namevalue,strlen($set[0])+1);
if ($name == "http_username" || $name == "http_passwd")
$nvram[$name] = $this->shadow($value);
else
$nvram[$name] = $value;
}
$this->backup_fwrite($this->backup_fname, $nvram);
}
public function cli_unset(&$nvram, $name)
{
if (!$nvram OR !$name)
return;
if (count($nvram))
unset($nvram[$name]);
$this->backup_fwrite($this->backup_fname, $nvram);
}
public function cli_show($nvram)
{
if (count($nvram))
foreach ($nvram AS $name => $value)
printf ("%s=%sn", $name, $value);
}
public function cli_keys($nvram)
{
if (count($nvram)) {
foreach ($nvram AS $name => $value)
printf ("%sn", $name);
}
}
public function cli_diff($nvram, $d_fname, $full=false)
{
$result = $this->nvram_diff($nvram, $d_fname);
if ($full) {
echo "Identical:n";
print_r ($result["identic"]);
echo "n";
}
echo "Only in $this->backup_fnamen";
ksort($result["orig_only"]);
print_r($result["orig_only"]);
echo "nOnly in $d_fnamen";
ksort($result["diff_only"]);
print_r($result["diff_only"]);
echo "nDifferences:n";
ksort($result["different"]);
print_r($result["different"]);
}
public function cli_rdiff($nvram, $d_fname, $pattern)
{
if (!$nvram || !$d_fname || !$pattern)
return;
$full_diff = $this->nvram_rdiff($nvram, $d_fname, $pattern);
print_r($full_diff);
}
/*
public function cli_fset($nvram, $name, $fname)
{
if (!$name || !$fname)
return;
if (!file_exists($fname)) {
$this->set_error($this->ERR_NO["DONT_EXISTS"]);
return;
}
$fhandle = fopen($fname, "rb");
if (!$fhandle) {
$this->set_error($this->ERR_NO["CANT_OPENR"]);
return;
}
$nvram[$name] = fread($fhandle, filesize($fname));
$this->backup_fwrite($this->backup_fname, $nvram);
$this->set_error($this->ERR_NO["SUCCESS"]);
fclose($fhandle);
return;
}
*/
/* misc **********
*
*/
public function strcrypt($passwd)
{
if ($passwd)
return $this->shadow($passwd);
return "";
}

/* error **********
*
*/
private $err_errno;
private $err_errmsg;
private $ERR_NO = array("SUCCESS"       => 0,
"DONT_EXISTS"   => 1,
"CANT_OPENR"    => 2,
"NOT_VALID"     => 3,
"SUCCESS_WERR"  => 4,
"CANT_OPENW"    => 5,
"FILE_EXISTS"   => 6,
);
private $ERR_MSG = array(0 => "Successn",
1 => "File not foundn",
2 => "Could not open file for readingn",
3 => "File is not a valid nvram-backupn",
4 => "Success, but skipped faulty null-paramn",
5 => "Could not open file for writingn",
6 => "File already existsn",
);

private function set_error($err_no)
{
$this->err_errno = $err_no;
$this->err_errmsg = $this->ERR_MSG[$err_no];
}
public function get_errno()
{
return $this->err_errno;
}
public function get_errmsg()
{
return $this->err_errmsg;
}
}
?>

相关内容

  • 没有找到相关文章

最新更新