/// <summary>
        /// Connects with the specified pfSense server using the v2.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
            HttpUtility.HttpCreateSession(pfSenseServer.ServerBaseUrl, cookieJar);

            Program.WriteOutput("Authenticating");

            // Authenticate the session
            var authenticationResult = HttpUtility.AuthenticateViaUrlEncodedFormMethod(string.Concat(pfSenseServer.ServerBaseUrl, "index.php"),
                                                                                       new Dictionary <string, string>(),
                                                                                       new Dictionary <string, string>
            {
                { "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("Retrieving backup file");

            var downloadArgs = new Dictionary <string, string>
            {
                { "donotbackuprrd", pfSenseServer.BackupStatisticsData ? "" : "on" },
                { "nopackages", pfSenseServer.BackupPackageInfo ? "" : "on" },
                { "Submit", "Download configuration" }
            };

            string filename;
            var    pfSenseBackupFile = new PfSenseBackupFile
            {
                FileContents = HttpUtility.DownloadBackupFile(string.Concat(pfSenseServer.ServerBaseUrl, "diag_backup.php"),
                                                              new Dictionary <string, string>(),
                                                              downloadArgs,
                                                              cookieJar,
                                                              out filename,
                                                              timeout),
                FileName = filename
            };

            return(pfSenseBackupFile);
        }
Esempio n. 2
0
        /// <summary>
        /// Connects with the specified pfSense server using the v1.2 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("Retrieving backup file");

            var downloadArgs = new Dictionary <string, string>
            {
                { "nopackages", pfSenseServer.BackupPackageInfo ? "" : "on" },
                { "Submit", "Download configuration" }
            };

            string filename;
            var    pfSenseBackupFile = new PfSenseBackupFile
            {
                FileContents = HttpUtility.DownloadBackupFile(string.Concat(pfSenseServer.ServerBaseUrl, "diag_backup.php"),
                                                              downloadArgs,
                                                              cookieJar,
                                                              out filename,
                                                              timeout),
                FileName = filename
            };

            return(pfSenseBackupFile);
        }
        /// <summary>
        /// Connects with the specified opnSense server using the v17.1 protocol implementation and returns the backup file contents
        /// </summary>
        /// <param name="opnSenseServer">opnSense server details which identifies which opnSense server to connect to</param>
        /// <param name="cookieJar">Cookie container to use through the communication with opnSense</param>
        /// <param name="timeout">Timeout in milliseconds on how long requests to opnSense may take. Default = 60000 = 60 seconds.</param>
        /// <returns>OpnSenseBackupFile instance containing the retrieved backup content from opnSense</returns>
        public OpnSenseBackupFile Execute(OpnSenseServerDetails opnSenseServer, CookieContainer cookieJar, int timeout = 60000)
        {
            Program.WriteOutput("Connecting using protocol version {0}", new object[] { opnSenseServer.Version });

            // Create a session on the opnSense webserver
            var loginPageContents = HttpUtility.HttpGetLoginPageContents(opnSenseServer.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['\"].+?id=['\"]_+?opnsense_csrf['\"].+?name=['\"](?<xsstokenname>.*?)['\"].+?value=['\"](?<xsstokenvalue>.*?)['\"].+?/>", RegexOptions.IgnoreCase);

            // Verify that the anti XSS token was found
            if (!xssToken.Success)
            {
                throw new ApplicationException("Unable to retrieve Cross Site Request Forgery token from login page");
            }

            // Authenticate the session
            var authenticationResult = HttpUtility.AuthenticateViaUrlEncodedFormMethod(string.Concat(opnSenseServer.ServerBaseUrl, "index.php"),
                                                                                       new Dictionary <string, string>
            {
                { xssToken.Groups["xsstokenname"].Value, xssToken.Groups["xsstokenvalue"].Value },
                { "usernamefld", System.Web.HttpUtility.UrlEncode(opnSenseServer.Username) },
                { "passwordfld", System.Web.HttpUtility.UrlEncode(opnSenseServer.Password) },
                { "login", "1" }
            },
                                                                                       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(opnSenseServer.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['\"].+?id=['\"]_+?opnsense_csrf['\"].+?name=['\"](?<xsstokenname>.*?)['\"].+?value=['\"](?<xsstokenvalue>.*?)['\"].+?/>", RegexOptions.IgnoreCase);

            // Verify that the anti XSS token was found
            if (!xssToken.Success)
            {
                throw new ApplicationException("Unable to retrieve Cross Site Request Forgery token from backup page");
            }

            Program.WriteOutput("Retrieving backup file");

            var downloadArgs = new Dictionary <string, string>
            {
                { xssToken.Groups["xsstokenname"].Value, xssToken.Groups["xsstokenvalue"].Value },
                { "donotbackuprrd", opnSenseServer.BackupStatisticsData ? "" : "on" },
                { "encrypt", opnSenseServer.EncryptBackup ? "on" : "" },
                { "encrypt_password", opnSenseServer.EncryptionPassword },
                { "encrypt_passconf", opnSenseServer.EncryptionPassword },
                { "download", "Download configuration" },
                { "restorearea", "" },
                { "rebootafterrestore", "on" },
                { "decrypt_password", "" },
                { "decrypt_passconf", "" }
            };

            string filename;
            var    opnSenseBackupFile = new OpnSenseBackupFile
            {
                FileContents = HttpUtility.DownloadBackupFile(backupPageUrl,
                                                              downloadArgs,
                                                              cookieJar,
                                                              out filename,
                                                              timeout,
                                                              backupPageUrl),
                FileName = filename
            };

            return(opnSenseBackupFile);
        }
Esempio n. 4
0
        /// <summary>
        /// Connects with the specified pfSense server using the v2.1 and v2.2 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>
            {
                { "__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("Retrieving backup file");

            var downloadArgs = new Dictionary <string, string>
            {
                { "donotbackuprrd", pfSenseServer.BackupStatisticsData ? "" : "on" },
                { "nopackages", pfSenseServer.BackupPackageInfo ? "" : "on" },
                { "encrypt", pfSenseServer.EncryptBackup ? "on" : "" },
                { "encrypt_password", pfSenseServer.EncryptionPassword },
                { "encrypt_passconf", pfSenseServer.EncryptionPassword },
                { "Submit", "Download configuration" }
            };

            string filename;
            var    pfSenseBackupFile = new PfSenseBackupFile
            {
                FileContents = HttpUtility.DownloadBackupFile(string.Concat(pfSenseServer.ServerBaseUrl, "diag_backup.php"),
                                                              downloadArgs,
                                                              cookieJar,
                                                              out filename,
                                                              timeout),
                FileName = filename
            };

            return(pfSenseBackupFile);
        }
        /// <summary>
        /// Connects with the specified OPNSense server using the 19.07 protocol implementation and returns the backup file contents
        /// </summary>
        /// <param name="pfSenseServer">OPNSense server details which identifies which OPNSense server to connect to</param>
        /// <param name="cookieJar">Cookie container to use through the communication with OPNSense</param>
        /// <param name="timeout">Timeout in milliseconds on how long requests to OPNSense may take. Default = 60000 = 60 seconds.</param>
        /// <returns>OPNSenseBackupFile instance containing the retrieved backup content from OPNSense</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 OPNSense 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, @"xhr.setRequestHeader\(""X-CSRFToken"", ""(.*)"" \);", RegexOptions.IgnoreCase);

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

            // Verify if the username/password combination was valid by examining the server response
            if (authenticationResult.Contains("Wrong username or password"))
            {
                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, @"xhr.setRequestHeader\(""X-CSRFToken"", ""(.*)"" \);", RegexOptions.IgnoreCase);

            Program.WriteOutput("Retrieving backup file");

            var downloadArgs = new Dictionary <string, string>();

            downloadArgs.Add("download", "Download configuration");
            if (!pfSenseServer.BackupStatisticsData)
            {
                downloadArgs.Add("donotbackuprrd", "on");
            }
            if (pfSenseServer.EncryptBackup)
            {
                downloadArgs.Add("encrypt", "on");
                downloadArgs.Add("encrypt_password", pfSenseServer.EncryptionPassword);
                downloadArgs.Add("encrypt_passconf", pfSenseServer.EncryptionPassword);
            }

            string filename;
            var    pfSenseBackupFile = new PfSenseBackupFile
            {
                FileContents = HttpUtility.DownloadBackupFile(backupPageUrl,
                                                              new Dictionary <string, string>
                {
                    { "X-CSRFToken", xssToken.Groups[1].Value }
                },
                                                              downloadArgs,
                                                              cookieJar,
                                                              out filename,
                                                              timeout,
                                                              backupPageUrl),
                FileName = filename
            };

            return(pfSenseBackupFile);
        }