-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63670ed
commit 0cff27b
Showing
12 changed files
with
243 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
pfSenseBackup/Properties/PublishProfiles/win-x64-full.pubxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters