|
1 | 1 | #!/bin/bash |
2 | 2 | # |
3 | | -# Script to post a payload against a local webserver at each paranoia level |
| 3 | +# Script to post a payload against a local webserver at each paranoia level. |
4 | 4 | # |
5 | | -# Note: Webserver has to be prepared to take desired PL as Request Header "PL" |
6 | | -# Check the access log format at https://www.netnea.com/cms/apache-tutorial-5_extending-access-log/ |
| 5 | +# Note: Webserver has to be prepared to take desired PL as Request Header "PL". |
7 | 6 | # |
| 7 | +# WARNING: Setting the paranoia level using a header without proper |
| 8 | +# authentication and authorization is extremely dangerous, and is not |
| 9 | +# recommended for production. |
| 10 | +# |
| 11 | +# Check how to use the Christian Folini's Apache access log format at: |
| 12 | +# https://www.netnea.com/cms/apache-tutorial-5_extending-access-log/ |
| 13 | +# |
| 14 | +# LogFormat "%h %{GEOIP_COUNTRY_CODE}e %u [%{%Y-%m-%d %H:%M:%S}t.%{usec_frac}t] \"%r\" %>s %b \ |
| 15 | +# \"%{Referer}i\" \"%{User-Agent}i\" \"%{Content-Type}i\" %{remote}p %v %A %p %R \ |
| 16 | +# %{BALANCER_WORKER_ROUTE}e %X \"%{cookie}n\" %{UNIQUE_ID}e %{SSL_PROTOCOL}x %{SSL_CIPHER}x \ |
| 17 | +# %I %O %{ratio}n%% %D %{ModSecTimeIn}e %{ApplicationTime}e %{ModSecTimeOut}e \ |
| 18 | +# %{ModSecAnomalyScoreInPLs}e %{ModSecAnomalyScoreOutPLs}e \ |
| 19 | +# %{ModSecAnomalyScoreIn}e %{ModSecAnomalyScoreOut}e" extended |
| 20 | +# |
| 21 | +# This script assumes %{ModSecAnomalyScoreIn}e is the column before to last in |
| 22 | +# the access log, if this does not match your LogFormat the script won't work |
| 23 | +# For better results set the SecDefaultAction to 'pass'. |
| 24 | +# |
| 25 | +# The anomaly score envvar can be set as follows: |
| 26 | +# SecAction "id:90101,phase:5,pass,nolog,\ |
| 27 | +# setenv:ModSecAnomalyScoreIn=%{TX.anomaly_score}" |
| 28 | +# |
| 29 | +# Sample rule to setup the PL dynamically from localhost" |
| 30 | +# SecRule REMOTE_ADDR "@ipMatch 127.0.0.1,192.168.0.128" \ |
| 31 | +# "id:90102,phase:1,pass,capture,log,auditlog,\ |
| 32 | +# msg:'Setting engine to PL%{matched_var}',chain" |
| 33 | +# SecRule REQUEST_HEADERS:PL "@rx ([1-4])" \ |
| 34 | +# "setvar:'tx.executing_paranoia_level=%{matched_var}'" |
| 35 | + |
8 | 36 | # Path to CRS rule set and local files |
9 | | -CRS="/usr/share/modsecurity-crs/rules/" |
| 37 | +CRS="/usr/share/modsecurity-crs/rules" |
10 | 38 | accesslog="/apache/logs/access.log" |
11 | 39 | errorlog="/apache/logs/error.log" |
| 40 | +URL="localhost:40080" |
| 41 | +protocol="http" |
| 42 | +while [[ $# > 0 ]] |
| 43 | +do |
| 44 | + case "$1" in |
| 45 | + -c|--crs) |
| 46 | + CRS="$2" |
| 47 | + shift |
| 48 | + ;; |
| 49 | + -a|--access) |
| 50 | + accesslog="$2" |
| 51 | + shift |
| 52 | + ;; |
| 53 | + -e|--error) |
| 54 | + errorlog="$2" |
| 55 | + shift |
| 56 | + ;; |
| 57 | + -u|--url) |
| 58 | + URL="$2" |
| 59 | + shift |
| 60 | + ;; |
| 61 | + -r|--resolve) |
| 62 | + resolve="$2" |
| 63 | + resolve="--resolve $resolve" |
| 64 | + shift |
| 65 | + ;; |
| 66 | + --protocol) |
| 67 | + protocol="$2" |
| 68 | + shift |
| 69 | + ;; |
| 70 | + -P|--payload) |
| 71 | + PAYLOAD="$2" |
| 72 | + shift |
| 73 | + ;; |
| 74 | + -h|--help) |
| 75 | + echo "Usage:" |
| 76 | + echo " --access \"/apache/logs/access.log\"" |
| 77 | + echo " --error \"/apache/logs/error.log\"" |
| 78 | + echo " --url \"localhost:40080\"" |
| 79 | + echo " --resolve \"someservername:40080:localhost\"" |
| 80 | + echo " --protocol \"https\"" |
| 81 | + echo " --payload \"/tmp/payload\"" |
| 82 | + echo " --help" |
| 83 | + exit 1 |
| 84 | + ;; |
| 85 | + esac |
| 86 | + shift |
| 87 | +done |
| 88 | + |
| 89 | +echo "Using CRS: $CRS" |
| 90 | +echo "Using accesslog: $accesslog" |
| 91 | +echo "Using errorlog: $errorlog" |
| 92 | +echo "Using URL: $URL" |
| 93 | +echo "Using protocol: $protocol" |
| 94 | + |
| 95 | +if [ -z "${PAYLOAD+x}" ]; then |
| 96 | + echo "Please submit valid payload file as parameter. This is fatal. Aborting." |
| 97 | + $0 -h |
| 98 | + echo "Examples:" |
| 99 | + echo " ./send-payload-pls.sh -a /logs/test/access.log \ |
| 100 | + -e /logs/test/error.log -u test.test.test.com:6443 --protocol https \ |
| 101 | + --payload /tmp/payload --resolve test.test.test.com:6443:192.168.0.128" |
| 102 | + echo " ./send-payload-pls.sh -a /logs/test/access.log \ |
| 103 | + -e /logs/test/error.log -u test.test.test.com:6443 --protocol https \ |
| 104 | + --payload 'or 1=1;--' --resolve test.test.test.com:6443:192.168.0.128" |
| 105 | + exit 1 |
| 106 | +fi |
12 | 107 |
|
13 | 108 | # URL of web server |
14 | | -URL="localhost:40080" |
15 | 109 |
|
16 | 110 | # Rules per Paranoia level |
17 | | -# Paranoia level 1 rules, rule 012 is the phase 2 rule delimiter of the start of PL1 |
18 | | -# Paranoia level 1 rules, rule 013 is the phase 1 rule delimiter of the finish of PL1 |
| 111 | +# Paranoia level 1 rules, rule 012 is the delimiter of the start of PL1 |
| 112 | +# Paranoia level 1 rules, rule 013 is the delimiter of the end of PL1 |
19 | 113 | PL1=$(awk "/012,phase:2/,/013,phase:1/" $CRS/*.conf |egrep -v "(012|013),phase" |egrep -o "id:[0-9]+" |sed -r 's,id:([0-9]+),\1\\,' |tr -t '\n' '\|' |sed -r 's,\\\|$,,') |
20 | 114 |
|
21 | | -# Paranoia level 2 rules, rule 014 is the phase 2 rule delimiter of the start of PL2 |
22 | | -# Paranoia level 2 rules, rule 015 is the phase 1 rule delimiter of the finish of PL2 |
| 115 | +# Paranoia level 2 rules, rule 014 is the delimiter of the start of PL2 |
| 116 | +# Paranoia level 2 rules, rule 015 is the delimiter of the end of PL2 |
23 | 117 | PL2=$(awk "/014,phase:2/,/015,phase:1/" $CRS/*.conf |egrep -v "(014|015),phase" |egrep -o "id:[0-9]+" |sed -r 's,id:([0-9]+),\1\\,' |tr -t '\n' '\|' |sed -r 's,\\\|$,,') |
24 | 118 |
|
25 | | -# Paranoia level 3 rules, rule 016 is the phase 2 rule delimiter of the start of PL3 |
26 | | -# Paranoia level 3 rules, rule 017 is the phase 1 rule delimiter of the finish of PL3 |
| 119 | +# Paranoia level 3 rules, rule 016 is the delimiter of the start of PL3 |
| 120 | +# Paranoia level 3 rules, rule 017 is the delimiter of the end of PL3 |
27 | 121 | PL3=$(awk "/016,phase:2/,/017,phase:1/" $CRS/*.conf |egrep -v "(016|017),phase" |egrep -o "id:[0-9]+" |sed -r 's,id:([0-9]+),\1\\,' |tr -t '\n' '\|' |sed -r 's,\\\|$,,') |
28 | 122 |
|
29 | | -# Paranoia level 4 rules, rule 018 is the phase 2 rule delimiter of the start of PL4 |
30 | | -# Paranoia level 4 rules, "Paranoia Levels Finished" delimiter of the finish of PL4 |
| 123 | +# Paranoia level 4 rules, rule 018 is the delimiter of the start of PL4 |
| 124 | +# Paranoia level 4 rules, "Paranoia Levels Finished" delimiter of the end of PL4 |
31 | 125 | PL4=$(awk "/018,phase:2/,/Paranoia Levels Finished/" $CRS/*.conf |egrep -v "018,phase" |egrep -o "id:[0-9]+" |sed -r 's,id:([0-9]+),\1\\,' |tr -t '\n' '\|' |sed -r 's,\\\|$,,') |
32 | 126 |
|
33 | | -if [ ! -z "$1" ]; then |
34 | | - PAYLOAD="$1" |
35 | | -else |
36 | | - echo "Please submit payload as parameter. This is fatal. Aborting." |
37 | | - exit 1 |
38 | | -fi |
39 | | - |
40 | 127 | echo "Sending the following payload at multiple paranoia levels: $PAYLOAD" |
41 | 128 | echo |
42 | 129 |
|
43 | | -for PL in 1 2 3 4; do |
44 | | - echo "--- Paranoia Level $PL ---" |
45 | | - echo |
46 | | - if [ -f "$PAYLOAD" ]; then |
47 | | - curl $URL --data-binary "@$PAYLOAD" -H "PL: $PL" -o /dev/null -s |
48 | | - else |
49 | | - curl $URL -d "$PAYLOAD" -H "PL: $PL" -o /dev/null -s |
50 | | - fi |
51 | | - grep $(tail -1 $accesslog | cut -d\" -f11 | cut -b2-26) $errorlog | sed -e "s/.*\[id \"//" -e "s/\(......\).*\[msg \"/\1 /" -e "s/\"\].*//" -e "s/(Total .*/(Total ...) .../" -e "s/Incoming and Outgoing Score: [0-9]* [0-9]*/Incoming and Outgoing Score: .../" | sed -e "s/$PL1/& PL1/" -e "s/$PL2/& PL2/" -e "s/$PL3/& PL3/ "-e "s/$PL4/& PL4/" | sort -k2 |
52 | | - echo |
53 | | - echo -n "Total Incoming Score: " |
54 | | - tail -1 $accesslog | cut -d\" -f11 | cut -d\ -f14 | tr "-" "0" |
55 | | - echo |
| 130 | +for PL in 1 2 3 4; do |
| 131 | + echo "--- Paranoia Level $PL ---" |
| 132 | + echo |
| 133 | + if [ -f "$PAYLOAD" ]; then |
| 134 | + curl $protocol://$URL $resolve -k --data-binary "@$PAYLOAD" -H "PL: $PL" -o /dev/null -s |
| 135 | + else |
| 136 | + curl $protocol://$URL $resolve -k -d "$PAYLOAD" -H "PL: $PL" -o /dev/null -s |
| 137 | + fi |
| 138 | + |
| 139 | + # Here are three ways to get the transaction unique id, |
| 140 | + # the first one is Christian's format, second is Spartan's format, |
| 141 | + # and the third one tries to guess which is the unique id using a |
| 142 | + # regular expression, the first two require specific format. |
| 143 | + # The automatic format detection may cause the script to malfunction. |
| 144 | + # Uncomment only the required format. |
| 145 | + # To use Christian's accesslog format uncomment the following line |
| 146 | + uniq_id=$(tail -1 $accesslog | cut -d\" -f11 | cut -b2-26) |
| 147 | + |
| 148 | + # To use Spartan's accesslog format (21 col) uncomment the following line |
| 149 | + #uniq_id=$(tail -1 $accesslog | awk '{print $21}') |
| 150 | + |
| 151 | + # To use the automatic unique_id detection uncomment the following line |
| 152 | + #uniq_id=$(tail -1 $accesslog | egrep -o '[a-zA-Z0-9]{26,28}') |
| 153 | + |
| 154 | + echo "Tracking unique id: $uniq_id" |
| 155 | + |
| 156 | + grep $uniq_id $errorlog | sed -e "s/.*\[id \"//" -e "s/\(......\).*\[msg \"/\1 /" -e "s/\"\].*//" -e "s/(Total .*/(Total ...) .../" -e "s/Incoming and Outgoing Score: [0-9]* [0-9]*/Incoming and Outgoing Score: .../" | sed -e "s/$PL1/& PL1/" -e "s/$PL2/& PL2/" -e "s/$PL3/& PL3/ " -e "s/$PL4/& PL4/" | sort -k2 | sed -r "s/^([0-9]+)$/\1 FOREIGN RULE NOT IN CRS/" |
| 157 | + |
| 158 | + echo |
| 159 | + echo -n "Total Incoming Score: " |
| 160 | + |
| 161 | + # Here are two ways to get the transaction anomaly score, |
| 162 | + # the first one is Christian's format, second is Spartan's format |
| 163 | + # To use Christian's accesslog format uncomment the following line |
| 164 | + tail -1 $accesslog | cut -d\" -f11 | cut -d\ -f14 | tr "-" "0" |
| 165 | + |
| 166 | + # To use Spartan's accesslog format (21 col) uncomment the following line |
| 167 | + # To use a different column change the $NF value, e.g. $(NF-1) |
| 168 | + #tail -1 $accesslog | awk '{print $NF}' | tr "-" "0" |
| 169 | + echo |
56 | 170 | done |
0 commit comments