| 1 |
#!/usr/bin/env bash |
| 2 |
set -ou pipefail |
| 3 |
if [[ -f /bin/bash ]]; then |
| 4 |
test_file="/bin/bash" |
| 5 |
elif [[ -f /bin/sh ]]; then |
| 6 |
test_file="/bin/sh" |
| 7 |
elif [[ -f /bin/ls ]]; then |
| 8 |
test_file="/bin/ls" |
| 9 |
else |
| 10 |
echo "could not find valid file to test" |
| 11 |
exit 255 |
| 12 |
fi |
| 13 |
|
| 14 |
DIR=$( |
| 15 |
cd "$(dirname "$0")" |
| 16 |
pwd |
| 17 |
) |
| 18 |
PARENT=$( |
| 19 |
cd "$(dirname "$0")/.." |
| 20 |
pwd |
| 21 |
) |
| 22 |
|
| 23 |
jsonlint=$(command -v jsonlint || command -v jsonlint-py) |
| 24 |
#check json for proc-all |
| 25 |
echo "starting proc-all check - json" |
| 26 |
"${PARENT}"/checksec --format=json --proc-all > "${DIR}/output.json" |
| 27 |
"${jsonlint}" --allow duplicate-keys "${DIR}/output.json" > /dev/null |
| 28 |
RET=$? |
| 29 |
jq . < "${DIR}/output.json" &> /dev/null |
| 30 |
JQRET=$? |
| 31 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 32 |
cat "${DIR}/output.json" |
| 33 |
echo "proc-all json validation failed" |
| 34 |
exit 1 |
| 35 |
fi |
| 36 |
|
| 37 |
#check json for proc-all |
| 38 |
echo "starting extended proc-all check - json" |
| 39 |
"${PARENT}/checksec" --format=json --proc-all --extended > "${DIR}/output.json" |
| 40 |
"${jsonlint}" --allow duplicate-keys "${DIR}/output.json" > /dev/null |
| 41 |
RET=$? |
| 42 |
jq . < "${DIR}/output.json" &> /dev/null |
| 43 |
JQRET=$? |
| 44 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 45 |
cat "${DIR}/output.json" |
| 46 |
echo "proc-all json validation failed" |
| 47 |
exit 1 |
| 48 |
fi |
| 49 |
|
| 50 |
#check json for kernel |
| 51 |
echo "starting kernel check - json" |
| 52 |
"${PARENT}/checksec" --format=json --kernel > "${DIR}/output.json" |
| 53 |
"${jsonlint}" "${DIR}/output.json" > /dev/null |
| 54 |
RET=$? |
| 55 |
jq . < "${DIR}/output.json" &> /dev/null |
| 56 |
JQRET=$? |
| 57 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 58 |
cat "${DIR}/output.json" |
| 59 |
echo "kernel json validation failed" |
| 60 |
exit 1 |
| 61 |
fi |
| 62 |
|
| 63 |
echo "starting custom kernel check for file kernel.config - json" |
| 64 |
"${PARENT}/checksec" --format=json --kernel=kernel.config > "${DIR}/output.json" |
| 65 |
"${jsonlint}" "${DIR}/output.json" > /dev/null |
| 66 |
RET=$? |
| 67 |
jq . < "${DIR}/output.json" &> /dev/null |
| 68 |
JQRET=$? |
| 69 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 70 |
cat "${DIR}/output.json" |
| 71 |
echo "custom kernel json validation failed" |
| 72 |
exit 1 |
| 73 |
fi |
| 74 |
|
| 75 |
while read -r file; do |
| 76 |
#check json against custom kernel config to trigger all checks |
| 77 |
echo "starting custom kernel check for file ${file} - json" |
| 78 |
"${PARENT}/checksec" --format=json --kernel="${file}" > "${DIR}/output.json" |
| 79 |
"${jsonlint}" "${DIR}/output.json" > /dev/null |
| 80 |
RET=$? |
| 81 |
jq . < "${DIR}/output.json" &> /dev/null |
| 82 |
JQRET=$? |
| 83 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 84 |
cat "${DIR}/output.json" |
| 85 |
echo "custom kernel json validation failed" |
| 86 |
exit 1 |
| 87 |
fi |
| 88 |
done < <(find "${PARENT}"/kernel_configs/configs/ -type f -iname "config-*") |
| 89 |
|
| 90 |
#check json for file |
| 91 |
echo "starting file check - json" |
| 92 |
"${PARENT}/checksec" --format=json --file="${test_file}" > "${DIR}/output.json" |
| 93 |
"${jsonlint}" "${DIR}/output.json" > /dev/null |
| 94 |
RET=$? |
| 95 |
jq . < "${DIR}/output.json" &> /dev/null |
| 96 |
JQRET=$? |
| 97 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 98 |
cat "${DIR}/output.json" |
| 99 |
echo "file json validation failed" |
| 100 |
exit 1 |
| 101 |
fi |
| 102 |
|
| 103 |
#check json for file extended |
| 104 |
echo "starting extended file check - json" |
| 105 |
"${PARENT}/checksec" --format=json --extended --file="${test_file}" > "${DIR}/output.json" |
| 106 |
"${jsonlint}" "${DIR}/output.json" > /dev/null |
| 107 |
RET=$? |
| 108 |
jq . < "${DIR}/output.json" &> /dev/null |
| 109 |
JQRET=$? |
| 110 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 111 |
cat "${DIR}/output.json" |
| 112 |
echo "file json validation failed" |
| 113 |
exit 1 |
| 114 |
fi |
| 115 |
|
| 116 |
#check json for fortify file |
| 117 |
echo "starting fortify-file check - json" |
| 118 |
"${PARENT}/checksec" --format=json --fortify-file="${test_file}" > "${DIR}/output.json" |
| 119 |
"${jsonlint}" --allow duplicate-keys "${DIR}/output.json" > /dev/null |
| 120 |
RET=$? |
| 121 |
jq . < "${DIR}/output.json" &> /dev/null |
| 122 |
JQRET=$? |
| 123 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 124 |
cat "${DIR}/output.json" |
| 125 |
echo "fortify-file json validation failed" |
| 126 |
exit 1 |
| 127 |
fi |
| 128 |
|
| 129 |
#check json for fortify file |
| 130 |
echo "starting extended fortify-file check - json" |
| 131 |
"${PARENT}/checksec" --format=json --fortify-file="${test_file}" --extended > "${DIR}/output.json" |
| 132 |
"${jsonlint}" --allow duplicate-keys "${DIR}/output.json" > /dev/null |
| 133 |
RET=$? |
| 134 |
jq . < "${DIR}/output.json" &> /dev/null |
| 135 |
JQRET=$? |
| 136 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 137 |
cat "${DIR}/output.json" |
| 138 |
echo "fortify-file json validation failed" |
| 139 |
exit 1 |
| 140 |
fi |
| 141 |
|
| 142 |
#check json for fortify proc |
| 143 |
echo "starting fortify-proc check - json" |
| 144 |
"${PARENT}/checksec" --format=json --fortify-proc=1 > "${DIR}/output.json" |
| 145 |
"${jsonlint}" --allow duplicate-keys "${DIR}/output.json" > /dev/null |
| 146 |
RET=$? |
| 147 |
jq . < "${DIR}/output.json" &> /dev/null |
| 148 |
JQRET=$? |
| 149 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 150 |
cat "${DIR}/output.json" |
| 151 |
echo "fortify-file json validation failed" |
| 152 |
exit 1 |
| 153 |
fi |
| 154 |
|
| 155 |
#check json for fortify proc |
| 156 |
echo "starting extended fortify-proc check - json" |
| 157 |
"${PARENT}/checksec" --format=json --fortify-proc=1 --extended > "${DIR}/output.json" |
| 158 |
"${jsonlint}" --allow duplicate-keys "${DIR}/output.json" > /dev/null |
| 159 |
RET=$? |
| 160 |
jq . < "${DIR}/output.json" &> /dev/null |
| 161 |
JQRET=$? |
| 162 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 163 |
cat "${DIR}/output.json" |
| 164 |
echo "fortify-file json validation failed" |
| 165 |
exit 1 |
| 166 |
fi |
| 167 |
|
| 168 |
#check json for dir |
| 169 |
echo "starting dir check - json" |
| 170 |
"${PARENT}"/checksec --format=json --dir=/sbin > "${DIR}/output.json" |
| 171 |
"${jsonlint}" "${DIR}/output.json" > /dev/null |
| 172 |
RET=$? |
| 173 |
jq . < "${DIR}/output.json" &> /dev/null |
| 174 |
JQRET=$? |
| 175 |
if [[ ${RET} != 0 ]] || [[ ${JQRET} != 0 ]]; then |
| 176 |
cat "${DIR}/output.json" |
| 177 |
echo "dir json validation failed" |
| 178 |
exit 1 |
| 179 |
fi |
| 180 |
|
| 181 |
echo "All json validation tests passed jsonlint" |
| 182 |
rm -f "${DIR}/output.json" |