Skip to content

Commit

Permalink
Version 2.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KoenZomers committed Feb 21, 2021
1 parent 63670ed commit 0cff27b
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ publish/
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
#*.pubxml
*.publishproj

# NuGet Packages
Expand Down
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ pfSense Backup allows you to backup the complete configuration of your pfSense o

## Release Notes

2.5.2 - released May 11, 2020 [download](https://github.com/KoenZomers/pfSenseBackup/releases/tag/2.5.2)
2.6.0 - released February 21, 2021 [download](https://github.com/KoenZomers/pfSenseBackup/releases/tag/2.6.0)

- Fixed bug when not providing the -o parameter that the backup would be downloaded to a temporary location instead of the location where you would start the application from [Issue #11](https://github.com/KoenZomers/pfSenseBackup/issues/11), thanks to [jvivona](https://github.com/jvivona) for reporting it!

2.5.1 - released May 11, 2020 [download](https://github.com/KoenZomers/pfSenseBackup/releases/tag/2.5.1)

- Added optional option -k to allow for backups older than a provided number of days to automatically be deleted on each run of this tool. Submitted through [PR #10](https://github.com/KoenZomers/pfSenseBackup/pull/10), thanks to [Shaun McCloud](https://github.com/smccloud) for submitting!

2.5 - released December 1, 2019 - [download](https://github.com/KoenZomers/pfSenseBackup/releases/tag/2.5.0)

- Added support for connecting to OPNSense
- Migration to .NET Core - builds are multiplaform (Windows, Linux, Mac) and self-contained (no need to install any framework)
- Added support for pfSense v2.5.0
- Upgraded to .NET Core 3.1

[Version History](https://github.com/KoenZomers/pfSenseBackup/blob/master/VersionHistory.md)

Expand Down
4 changes: 4 additions & 0 deletions VersionHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Version History

2.5.2 - released May 11, 2020 [download](https://github.com/KoenZomers/pfSenseBackup/releases/tag/2.5.2)

- Fixed bug when not providing the -o parameter that the backup would be downloaded to a temporary location instead of the location where you would start the application from [Issue #11](https://github.com/KoenZomers/pfSenseBackup/issues/11), thanks to [jvivona](https://github.com/jvivona) for reporting it!

2.5.1 - released May 11, 2020 [download](https://github.com/KoenZomers/pfSenseBackup/releases/tag/2.5.1)

- Added optional option -k to allow for backups older than a provided number of days to automatically be deleted on each run of this tool. Submitted through [PR #10](https://github.com/KoenZomers/pfSenseBackup/pull/10), thanks to [Shaun McCloud](https://github.com/smccloud) for submitting!
Expand Down
8 changes: 6 additions & 2 deletions pfSenseBackup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class Program
/// <summary>
/// Defines the pfSense version to use if not explicitly specified
/// </summary>
private const string DefaultPfSenseVersion = "2.3.3";
private const string DefaultPfSenseVersion = "2.5.0";

#endregion

Expand Down Expand Up @@ -142,6 +142,10 @@ private static void RetrieveBackupFile()
pfSenseProtocol = new Protocols.PfSenseVersion233();
break;

case "2.5.0":
pfSenseProtocol = new Protocols.PfSenseVersion250();
break;

case "opn1907":
pfSenseProtocol = new Protocols.OPNSenseVersion197();
break;
Expand Down Expand Up @@ -337,7 +341,7 @@ private static void DisplayHelp()
WriteOutput("u: Username of the account to use to log on to pfSense");
WriteOutput("p: Password of the account to use to log on to pfSense");
WriteOutput("s: IP address or DNS name of the pfSense server");
WriteOutput("v: PFSense version. Supported are 1.2, 2.0, 2.1, 2.2, 2.3 and 2.3.3 (2.3.3 = default, optional). For OPNSense provide opn1907.");
WriteOutput("v: PFSense version. Supported are 1.2, 2.0, 2.1, 2.2, 2.3, 2.3.3 and 2.5.0 (2.5.0 = default, optional). For OPNSense provide opn1907.");
WriteOutput("o: Folder or complete path where to store the backup file (optional)");
WriteOutput("e: Have pfSense encrypt the backup using this password (optional)");
WriteOutput("t: Timeout in seconds for pfSense to retrieve the backup (60 seconds = default, optional)");
Expand Down
17 changes: 17 additions & 0 deletions pfSenseBackup/Properties/PublishProfiles/linux-arm.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\publish\linux-arm</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>linux-arm</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>
</Project>
17 changes: 17 additions & 0 deletions pfSenseBackup/Properties/PublishProfiles/linux-x64.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\publish\linux-x64</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>
</Project>
17 changes: 17 additions & 0 deletions pfSenseBackup/Properties/PublishProfiles/osx-x64.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\publish\osx-x64</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>osx-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>
</Project>
18 changes: 18 additions & 0 deletions pfSenseBackup/Properties/PublishProfiles/win-arm.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\publish\win-arm</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>win-arm</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
<PublishReadyToRun>False</PublishReadyToRun>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>
</Project>
18 changes: 18 additions & 0 deletions pfSenseBackup/Properties/PublishProfiles/win-x64-full.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\publish\win-x64-full</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
<PublishReadyToRun>False</PublishReadyToRun>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>
</Project>
18 changes: 18 additions & 0 deletions pfSenseBackup/Properties/PublishProfiles/win-x64.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\publish\win-x64</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
<PublishReadyToRun>False</PublishReadyToRun>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>
</Project>
115 changes: 115 additions & 0 deletions pfSenseBackup/Protocols/PfSenseVersion250.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text.RegularExpressions;

namespace KoenZomers.Tools.pfSense.pfSenseBackup.Protocols
{
/// <summary>
/// Implementation of the pfSense protocol for version 2.5.0
/// </summary>
public class PfSenseVersion250 : IPfSenseProtocol
{
/// <summary>
/// Connects with the specified pfSense server using the v2.5.0 protocol implementation and returns the backup file contents
/// </summary>
/// <param name="pfSenseServer">pfSense server details which identifies which pfSense server to connect to</param>
/// <param name="cookieJar">Cookie container to use through the communication with pfSense</param>
/// <param name="timeout">Timeout in milliseconds on how long requests to pfSense may take. Default = 60000 = 60 seconds.</param>
/// <returns>PfSenseBackupFile instance containing the retrieved backup content from pfSense</returns>
public PfSenseBackupFile Execute(PfSenseServerDetails pfSenseServer, CookieContainer cookieJar, int timeout = 60000)
{
Program.WriteOutput("Connecting using protocol version {0}", new object[] { pfSenseServer.Version });

// Create a session on the pfSense webserver
var loginPageContents = HttpUtility.HttpGetLoginPageContents(pfSenseServer.ServerBaseUrl, cookieJar, timeout);

// Check if a response was returned from the login page request
if (string.IsNullOrEmpty(loginPageContents))
{
throw new ApplicationException("Unable to retrieve login page contents");
}

Program.WriteOutput("Authenticating");

// Use a regular expression to fetch the anti cross site scriping token from the HTML
var xssToken = Regex.Match(loginPageContents, "<input.+?type=['\"]hidden['\"].+?name=['\"]_+?csrf_magic['\"] value=['\"](?<xsstoken>.*?)['\"].+?/>", RegexOptions.IgnoreCase);

// Verify that the anti XSS token was found
if (!xssToken.Success)
{
xssToken = Regex.Match(loginPageContents, "var.*?csrfMagicToken.*?=.*?\"(?<xsstoken>.*?)\"");
}

// Authenticate the session
var authenticationResult = HttpUtility.AuthenticateViaUrlEncodedFormMethod(string.Concat(pfSenseServer.ServerBaseUrl, "index.php"),
new Dictionary<string, string>(),
new Dictionary<string, string>
{
{"__csrf_magic", xssToken.Groups["xsstoken"].Value },
{ "usernamefld", System.Web.HttpUtility.UrlEncode(pfSenseServer.Username) },
{ "passwordfld", System.Web.HttpUtility.UrlEncode(pfSenseServer.Password) },
{ "login", "Login" }
},
cookieJar,
timeout);

// Verify if the username/password combination was valid by examining the server response
if (authenticationResult.Contains("Username or Password incorrect"))
{
throw new ApplicationException("ERROR: Credentials incorrect");
}

Program.WriteOutput("Requesting backup file");

// Get the backup page contents for the xsrf token
var backupPageUrl = string.Concat(pfSenseServer.ServerBaseUrl, "diag_backup.php");

var backupPageContents = HttpUtility.HttpGetLoginPageContents(backupPageUrl, cookieJar, timeout);

// Check if a response was returned from the login page request
if (string.IsNullOrEmpty(backupPageContents))
{
throw new ApplicationException("Unable to retrieve backup page contents");
}

// Use a regular expression to fetch the anti cross site scriping token from the HTML
xssToken = Regex.Match(backupPageContents, "<input.+?type=['\"]hidden['\"].+?name=['\"]_+?csrf_magic['\"] value=['\"](?<xsstoken>.*?)['\"].+?/>", RegexOptions.IgnoreCase);

// Verify that the anti XSS token was found
if (!xssToken.Success)
{
xssToken = Regex.Match(backupPageContents, "var.*?csrfMagicToken.*?=.*?\"(?<xsstoken>.*?);.*?\"");
}

Program.WriteOutput("Retrieving backup file");

var downloadArgs = new Dictionary<string, string>
{
{"__csrf_magic", xssToken.Groups["xsstoken"].Value },
{ "backuparea", "" },
{ "nopackages", pfSenseServer.BackupPackageInfo ? "" : "yes" },
{ "donotbackuprrd", pfSenseServer.BackupStatisticsData ? "" : "yes" },
{ "encrypt", pfSenseServer.EncryptBackup ? "yes" : "" },
{ "encrypt_password", pfSenseServer.EncryptionPassword },
{ "encrypt_password_confirm", pfSenseServer.EncryptionPassword },
{ "download", "Download configuration as XML" },
{ "restorearea", "" }
};

string filename;
var pfSenseBackupFile = new PfSenseBackupFile
{
FileContents = HttpUtility.DownloadBackupFile( backupPageUrl,
new Dictionary<string, string>(),
downloadArgs,
cookieJar,
out filename,
timeout,
backupPageUrl),
FileName = filename
};
return pfSenseBackupFile;
}
}
}
17 changes: 9 additions & 8 deletions pfSenseBackup/pfSenseBackup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<ApplicationIcon>pfSense.ico</ApplicationIcon>
<AssemblyVersion>2.5.2.0</AssemblyVersion>
<FileVersion>2.5.0.0</FileVersion>
<AssemblyVersion>2.6.0.0</AssemblyVersion>
<FileVersion>2.6.0.0</FileVersion>
<Authors>Koen Zomers</Authors>
<Product>pfSense Backup</Product>
<Version>2.5.2.0</Version>
<Version>2.6.0.0</Version>
<Copyright>Koen Zomers</Copyright>
<PackageReleaseNotes>- Fixed bug when not providing the -o parameter that the backup would be downloaded to a temporary location instead of the location where you would start the application from [Issue #11](https://github.com/KoenZomers/pfSenseBackup/issues/11), thanks to [jvivona](https://github.com/jvivona) for reporting it!</PackageReleaseNotes>
<PackageReleaseNotes>- Added support for pfSense v2.5.0
- Upgraded to .NET Core 3.1</PackageReleaseNotes>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<PropertyGroup>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>

0 comments on commit 0cff27b

Please sign in to comment.