Skip to content

Instantly share code, notes, and snippets.

@TheFlash2k
Created October 25, 2023 17:51
Show Gist options
  • Save TheFlash2k/fbd479d082c502a5312828f9b0decfca to your computer and use it in GitHub Desktop.
Save TheFlash2k/fbd479d082c502a5312828f9b0decfca to your computer and use it in GitHub Desktop.
manage aslr
#!/bin/bash
# Default Mode: 2
# MODES:
# 0 - disable
# 1 - stack and code segment
# 2 - stack, code and data segment (default)
# Options:
# aslr on # 2
# aslr stack # 1
# aslr off # 0
function usage() {
echo "Usage: aslr <mode> [off|stack|on]"
echo -e "Available modes:\n\toff => 0\n\tstack => 1\n\ton => 2"
}
function get_mode(){
if [[ -z $1 ]]; then return 0; fi
if [[ $1 -eq 0 ]]; then echo -n "Off"
elif [[ $1 -eq 1 ]]; then echo -n "On [Limited]"
elif [[ $1 -eq 2 ]]; then echo -n "On"
else echo -n "Unknown"; fi
}
current=$(sysctl -a --pattern "randomize" | rev | cut -d ' ' -f 1)
# echo 0 | sudo tee /proc/sys/kernel/randomize_va_space
if [[ -z $1 ]]; then echo "[*] Current aslr mode: $current [$(get_mode $current)]"; exit 0; fi
_mode="$1"
if [[ "$_mode" == "off" ]]; then
mode=0
elif [[ "$_mode" == "stack" ]]; then
mode=1
elif [[ "$_mode" == "on" ]]; then
mode=2
else
echo "Invalid mode \"$mode\""
usage
exit 1
fi
if [[ $mode -eq current ]]; then
echo "[x] Aslr mode is already set to $(get_mode $current) [$current]"
else
echo "[*] Changing mode from $(get_mode $current) to $(get_mode $mode)"
_o=$(echo $mode | sudo tee /proc/sys/kernel/randomize_va_space)
if [[ $_o -eq $mode ]]; then
echo '[+] Done!'
else
echo "[*] An error occrured: $_o"
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment