Debian Codegraph @

src/core.sh

raw
1 #!/usr/bin/env bash
2 # shellcheck disable=SC2154,SC2034
3 # these top lines are moved during build
4
5 # set global lang to C
6 export LC_ALL="C"
7
8 # version
9 SCRIPT_VERSION=2022052701
10 SCRIPT_MAJOR=2
11 SCRIPT_MINOR=6
12 SCRIPT_REVISION=0
13
14 # global vars
15 debug=false
16 verbose=false
17 format="cli"
18 SCRIPT_NAME="checksec"
19 SCRIPT_URL="https://github.com/slimm609/checksec.sh/raw/master/${SCRIPT_NAME}"
20 SIG_URL="https://github.com/slimm609/checksec.sh/raw/master/$(basename ${SCRIPT_NAME} .sh).sig"
21
22 pkg_release=false
23 commandsmissing=false
24 OPT=0
25 extended_checks=false
26 # FORTIFY_SOURCE vars
27 FS_end=_chk
28 FS_cnt_total=0
29 FS_cnt_checked=0
30 FS_cnt_unchecked=0
31 FS_libc=0
32
33 PATH=${PATH}:/sbin/:/usr/sbin/
34
35 # check if directory exists
36 dir_exists() {
37 if [[ -d "${1}" ]]; then
38 return 0
39 else
40 return 1
41 fi
42 }
43
44 # check user privileges
45 root_privs() {
46 if [[ $(/usr/bin/id -u) -eq 0 ]]; then
47 return 0
48 else
49 return 1
50 fi
51 }
52
53 # check for required files and deps first
54 # check if command exists
55 command_exists() {
56 type "${1}" > /dev/null 2>&1
57 }
58
59 for command in cat awk sed sysctl uname mktemp openssl grep stat file find sort head ps readlink basename id which xargs; do
60 if ! (command_exists ${command}); then
61 echo >&2 -e "\e[31mWARNING: '${command}' not found! It's required for most checks.\e[0m"
62 commandsmissing=true
63 fi
64 done
65
66 if [[ ${commandsmissing} == true ]]; then
67 echo >&2 -e "\n\e[31mWARNING: Not all necessary commands found. Some tests might not work!\e[0m\n"
68 sleep 2
69 fi
70
71 if (command_exists readelf); then
72 readelf="readelf -W"
73 elif (command_exists eu-readelf); then
74 readelf="eu-readelf -W"
75 elif (command_exists greadelf); then
76 readelf="greadelf -W"
77 else
78 echo -e "\n\e[31mERROR: readelf is a required tool for almost all tests. Aborting...\e[0m\n"
79 exit
80 fi
81
82 sysarch=$(uname -m)
83 if [[ "${sysarch}" == "x86_64" ]]; then
84 arch="64"
85 elif [[ "${sysarch}" == "i?86" ]]; then
86 arch="32"
87 elif [[ "${sysarch}" =~ "arm" ]]; then
88 arch="arm"
89 elif [[ "${sysarch}" =~ "aarch64" ]]; then
90 arch="aarch64"
91 fi