Skip to content

Commit

Permalink
change some stuff to create a working config file direkt with this tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Stricted committed Jan 24, 2016
1 parent 25c810a commit b6a122e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 33 deletions.
24 changes: 17 additions & 7 deletions SPHDecode/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SPHDecode"
mc:Ignorable="d"
Title="SPHDecode" Height="145" Width="405">
Title="SPHDecode" Height="160" Width="405">
<Grid DataContext="{StaticResource MainWindowModel}">
<StackPanel HorizontalAlignment="Left" Width="375" Margin="10,10,0,0" VerticalAlignment="Top">
<StackPanel.Resources>
Expand All @@ -15,15 +15,25 @@
</StackPanel.Resources>
<DockPanel>
<Button Command="{Binding Path=srcFileDialog}" Content="Source File" Width="70" />
<TextBox Text="{Binding Path=srcFile}" Width="300" Margin="0,5,0,5" IsReadOnly="True"/>
<TextBox Text="{Binding Path=srcFile}" Width="270" Margin="0,5,0,5" IsReadOnly="True"/>
<Button Command="{Binding Path=LoadConfig}" Content="Load"/>
</DockPanel>
<StackPanel Orientation="Horizontal">
<StackPanel Width="187">
<TextBlock Text="Telnet" />
<TextBlock Text="Username" />
<TextBlock Text="Passwort" />
</StackPanel>
<StackPanel Width="187">
<CheckBox IsChecked="{Binding Path=telnet,Mode=TwoWay}"/>
<TextBox Text="{Binding Path=username,Mode=TwoWay}" />
<TextBox Text="{Binding Path=password,Mode=TwoWay}"/>
</StackPanel>
</StackPanel>
<DockPanel>
<Button Command="{Binding Path=dstFileDialog}" Content="Save File" Width="70" />
<TextBox Text="{Binding Path=dstFile}" Width="300" Margin="0,5,0,5" IsReadOnly="True"/>
</DockPanel>
<DockPanel>
<Button Width="185" Command="{Binding Path=decrypt}" Content="decrypt"/>
<Button Width="185" Command="{Binding Path=encrypt}" Content="encrypt"/>
<TextBox Text="{Binding Path=dstFile}" Width="270" Margin="0,5,0,5" IsReadOnly="True"/>
<Button Command="{Binding Path=SaveConfig}" Content="save"/>
</DockPanel>
</StackPanel>
</Grid>
Expand Down
5 changes: 5 additions & 0 deletions SPHDecode/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public MainWindow()
{
InitializeComponent();
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{

}
}
}
118 changes: 92 additions & 26 deletions SPHDecode/Model/MainWindowModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
using Microsoft.Win32;
using System.IO;
using System.Windows;
using System.Xml;

namespace SPHDecode.Model
{
class MainWindowModel : SuperViewModel
{
private string _srcFile;
private string _dstFile;
private bool _telnet;
private string _username;
private string _password;
private string _config;
private DelegateCommand _srcFileDialog;
private DelegateCommand _dstFileDialog;
private DelegateCommand _encrypt;
private DelegateCommand _decrypt;
private DelegateCommand _loadConfig;
private DelegateCommand _saveConfig;


public string srcFile
{
Expand All @@ -27,6 +33,30 @@ public string dstFile
set { SetProperty(ref _dstFile, value); }
}

public bool telnet
{
get { return _telnet; }
set { SetProperty(ref _telnet, value); }
}

public string username
{
get { return _username; }
set { SetProperty(ref _username, value); }
}

public string password
{
get { return _password; }
set { SetProperty(ref _password, value); }
}

public string config
{
get { return _config; }
set { SetProperty(ref _config, value); }
}

public DelegateCommand srcFileDialog
{
get { return _srcFileDialog; }
Expand All @@ -39,16 +69,16 @@ public DelegateCommand dstFileDialog
set { SetProperty(ref _dstFileDialog, value); }
}

public DelegateCommand encrypt
public DelegateCommand LoadConfig
{
get { return _encrypt; }
set { SetProperty(ref _encrypt, value); }
get { return _loadConfig; }
set { SetProperty(ref _loadConfig, value); }
}

public DelegateCommand decrypt
public DelegateCommand SaveConfig
{
get { return _decrypt; }
set { SetProperty(ref _decrypt, value); }
get { return _saveConfig; }
set { SetProperty(ref _saveConfig, value); }
}

private void OnsrcFileDialogExecute()
Expand All @@ -69,40 +99,76 @@ private void OndstFileDialogExecute()
saveFileDialog = null;
}

private void OnencryptExecute()
private void OnLoadConfigExecute()
{
string orig = File.ReadAllText(srcFile);
// remove BOM from utf-8 file
orig = orig.Replace("\ufeff", "");
if (string.IsNullOrWhiteSpace(srcFile))
{
LogManager.WriteToLog("no input file specified");
MessageBox.Show("no input file specified", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

byte[] orig = File.ReadAllBytes(srcFile);
config = Cryptography.Decrypt(orig);

// replace windows with unix line endings
orig = orig.Replace("\r\n", "\n").Replace("\r", "\n");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(config);

byte[] encode = Cryptography.Encrypt(orig);
if (Object.Equals(encode, null).Equals(false))
XmlNode node = xmlDoc.SelectSingleNode("/InternetGatewayDeviceConfig/Device/DeviceInfo/X_ServiceManage");
string tel = node.Attributes["TelnetEnable"]?.InnerText;

telnet = false;
if (tel.Equals("1"))
{
File.WriteAllBytes(dstFile, encode);
MessageBox.Show("config encrypted successfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
telnet = true;
}

node = xmlDoc.SelectSingleNode("/InternetGatewayDeviceConfig/Device/UserInterface/X_Cli/UserInfo").FirstChild;
username = node.Attributes["Username"]?.InnerText;
password = node.Attributes["Userpassword"]?.InnerText;
}

private void OndecryptExecute()
private void OnSaveConfigExecute()
{
byte[] orig = File.ReadAllBytes(srcFile);
string decode = Cryptography.Decrypt(orig);
if (string.IsNullOrWhiteSpace(decode).Equals(false))
if (string.IsNullOrWhiteSpace(dstFile))
{
LogManager.WriteToLog("no output file specified");
MessageBox.Show("no output file specified", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(config);

XmlNode node = xmlDoc.SelectSingleNode("/InternetGatewayDeviceConfig/Device/DeviceInfo/X_ServiceManage");
string tel = node.Attributes["TelnetEnable"]?.InnerText;

node = xmlDoc.SelectSingleNode("/InternetGatewayDeviceConfig/Device/UserInterface/X_Cli/UserInfo").FirstChild;
string user = node.Attributes["Username"]?.InnerText;
string pass = node.Attributes["Userpassword"]?.InnerText;

config = config.Replace(
string.Concat("<X_ServiceManage TelnetEnable=\"", tel, "\" TelnetPort=\"23\" KeyEquipMode=\"0\"/>"),
string.Concat("<X_ServiceManage TelnetEnable=\"", (telnet.Equals(false) ? 0 : 1), "\" TelnetPort=\"23\" KeyEquipMode=\"0\"/>"));

config = config.Replace(
string.Concat("<UserInfoInstance InstanceID=\"1\" Username=\"", user, "\" Userpassword=\"", pass, "\" Userlevel=\"0\" Timestamp=\"0000-00-00 00:00:00\" Size=\"0\"/>"),
string.Concat("<UserInfoInstance InstanceID=\"1\" Username=\"", username, "\" Userpassword=\"", password, "\" Userlevel=\"0\" Timestamp=\"0000 - 00 - 00 00:00:00\" Size=\"0\"/>") );

byte[] encode = Cryptography.Encrypt(config);
if (Object.Equals(encode, null).Equals(false))
{
File.WriteAllText(dstFile, decode);
MessageBox.Show("config decrypted successfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
File.WriteAllBytes(dstFile, encode);
MessageBox.Show("config encrypted successfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
}
}

public MainWindowModel()
{
srcFileDialog = new DelegateCommand(new Action(OnsrcFileDialogExecute));
dstFileDialog = new DelegateCommand(new Action(OndstFileDialogExecute));
encrypt = new DelegateCommand(new Action(OnencryptExecute));
decrypt = new DelegateCommand(new Action(OndecryptExecute));
LoadConfig = new DelegateCommand(new Action(OnLoadConfigExecute));
SaveConfig = new DelegateCommand(new Action(OnSaveConfigExecute));
}
}
}

0 comments on commit b6a122e

Please sign in to comment.