#!/bin/bash -e OUT='/var/backups/opnsense'TMP=$(mktemp -d)URL='https://opnsense.domain.tld'LOGIN='backupusr'PASS='p@ssw0rd' # Submit the login form with the previous values, and save a new CSRF token/usr/bin/wget -q -O /dev/null --keep-session-cookies --save-cookies $TMP/cookies.txt --no-check-certificate \ --post-data "login=Login&usernamefld=$LOGIN&passwordfld=$PASS" $URL/diag_backup.php # Save only the config/usr/bin/wget -q --keep-session-cookies --load-cookies $TMP/cookies.txt --save-cookies $TMP/cookies.txt --no-check-certificate \ --post-data "download=Download%20Configuration&donotbackuprrd=yes" $URL/diag_backup.php -O $OUT/config-pfsense.xml \ rm -f $TMP/*.txtrmdir $TMP
felix@plex: backups$ grep magic diag_backup.phpfelix@plex: backups$ grep csrf *diag_backup.php: <form class="clearfix" id="iform" name="iform" method="post" autocomplete="off" action="/diag_backup.php"><input type="hidden" id="__opnsense_csrf" name="UVZLYVFKSUlPbEdKb3RRL3ZkRjhOQT09" value="VlR2SUppNzFBYUJyR1hGUVVSQXErZz09"\/>felix@plex: backups$
grep "__opnsense_csrf"
cut -d'"' -fnumber
wget -O- --keep-session-cookies --save-cookies cookies.txt --no-check-certificate http://192.168.86.1/diag_backup.php | grep "__opnsense_csrf" | sed 's/.*value="\(.*\)".*/\1/' > csrf.txtwget -O- --keep-session-cookies --load-cookies cookies.txt --save-cookies cookies.txt --no-check-certificate --post-data "login=login&usernamefld=root&passwordfld=MYPASSWORD&__opnsense_csrf=$(cat csrf.txt)" http://192.168.86.1/diag_backup.php | grep "__opnsense_csrf" | sed 's/.*value="\(.*\)".*/\1/' > csrf2.txt
--2017-03-03 19:39:50-- http://192.168.86.1/diag_backup.phpConnecting to 192.168.86.1:80... connected.HTTP request sent, awaiting response... 403 Forbidden2017-03-03 19:39:50 ERROR 403: Forbidden.
cat csrf.txtTWIxOFpIdFdUZ1RGZjBON0NXSERoUT09\
puts d.codeputs d.bodyexit 0
this means you do not have the string "__opnsense_csrf" in the body. Do you use 17.1.2?
indexpage = URI("https://#{SERVER_IP}:4443/index.php")backuppage = URI("https://#{SERVER_IP}:4443/diag_backup.php")
For debugging, can you add those three lines:Code: [Select]puts d.codeputs d.bodyexit 0after d is assigned (line 40)?
200<!doctype html><!--[if IE 8 ]><html lang="en" class="ie ie8 lte9 lte8 no-js"><![endif]--><!--[if IE 9 ]><html lang="en" class="ie ie9 lte9 no-js"><![endif]--><!--[if (gt IE 9)|!(IE)]><!--><html lang="en" class="no-js"><!--<![endif]--> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="robots" content="index, follow, noodp, noydir" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <meta name="copyright" content="" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" /> <title>Login</title> <link href="/ui/themes/opnsense/build/css/main.css" rel="stylesheet"> <link href="/ui/themes/opnsense/build/images/favicon.png" rel="shortcut icon"> <!--[if lt IE 9]><script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js"></script><![endif]--> <script type="text/javascript">if (top != self) {top.location.href = self.location.href;}</script><script type="text/javascript">var csrfMagicToken = "sid:7a7f16c6317e0e693af8c8d09a4244ea2f82319e,1488785494;ip:a066ee2908007256ef908b4091d91f35f963ab4c,1488785494";var csrfMagicName = "__csrf_magic";</script><script src="/csrf/csrf-magic.js" type="text/javascript"></script></head> <body class="page-login"> <div class="container"> <main class="login-modal-container"> <header class="login-modal-head" style="height:55px;"> <div class="navbar-brand"> <img src="/ui/themes/opnsense/build/images/default-logo.png" height="30" alt="logo"/> </div> </header> <div class="login-modal-content"> <div id="inputerrors" class="text-danger"> </div><br /> <form class="clearfix" id="iform" name="iform" method="post" autocomplete="off" action="/index.php"><input type='hidden' name='__csrf_magic' value="sid:7a7f16c6317e0e693af8c8d09a4244ea2f82319e,1488785494;ip:a066ee2908007256ef908b4091d91f35f963ab4c,1488785494" /> <div class="form-group"> <label for="usernamefld">Username:</label> <input id="usernamefld" type="text" name="usernamefld" class="form-control user" tabindex="1" autofocus="autofocus" autocapitalize="off" autocorrect="off" /> </div> <div class="form-group"> <label for="passwordfld">Password:</label> <input id="passwordfld" type="password" name="passwordfld" class="form-control pwd" tabindex="2" /> </div> <button type="submit" name="login" value="1" class="btn btn-primary pull-right">Login</button> </form> </div> </main> <div class="login-foot text-center"> <a target="_blank" href="https://opnsense.org" class="redlnk">OPNsense</a> (c) 2014-2016 <a href="https://www.deciso.com/" class="tblnk">Deciso B.V.</a> </div> </div> <script type="text/javascript">CsrfMagic.end();</script></body> </html>