Last active
June 9, 2025 15:39
-
-
Save adde88/c711343b2322e66cd0e308421ef9a8d9 to your computer and use it in GitHub Desktop.
My private OpenWRT (SDK) Build Helper Scripts, for easy building of packages on my Kali/Debian/Arch distros.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
# Version 2, December 2004 | |
# Copyright (C) 2023 Andreas Nilsen <[email protected]> | |
# | |
# This script has been fully refactored for production-grade robustness, security, | |
# and maintainability. It incorporates strict error checking, readonly variables, | |
# input validation, and modern Bash practices. | |
# | |
# Example Usage: | |
# source this_script.sh | |
# --- Configuration Block --- | |
# This block contains readonly variables. It is guarded to ensure it only runs once | |
# per shell session, preventing errors on subsequent 'source' commands. It does NOT | |
# use 'return' or 'exit', making it safe for SSH sessions. | |
# | |
# We check if the guard variable has been declared. 'declare -p' is a robust way to do this. | |
if ! declare -p OPENWRT_ALIASES_SOURCED &>/dev/null; then | |
# XXX: SETTINGS - Make these global constants readonly | |
readonly SCRIPT_VERSION="2.6-hardened" | |
readonly LAST_UPDATE="June 9, 2025" | |
# Allow building as root (use with caution). declare -r -x makes it a readonly export. | |
declare -r -x FORCE_UNSAFE_CONFIGURE=1 | |
# --- Global Read-only Variables & Associative Arrays --- | |
declare -A -r OPENWRT_GITS=( | |
[snapshot]="~/gits/openwrt-snapshot" | |
[v24.10]="~/gits/openwrt-24.10" | |
[v23.05]="~/gits/openwrt-23.05" | |
[v22.03]="~/gits/openwrt-22.03" | |
[v21.02]="~/gits/openwrt-21.02" | |
[v19.07]="~/gits/openwrt-19.07" | |
[v18.06]="~/gits/openwrt-18.06" | |
[lede-17.01]="~/gits/lede-17.01" | |
[v15.05]="~/gits/openwrt-15.05" | |
) | |
declare -A -r DEVICE_IP=( | |
[asus]="192.168.1.1:/root/ipks/" | |
[tetra]="192.168.1.6:/root/ipks/" | |
[nano]="172.16.42.1:/root/ipks/" | |
[mk7]="192.168.1.8:/root/ipks/" | |
[mk6]="192.168.1.6:/root/ipks/" | |
) | |
declare -A -r BOARD_CONFIGS=( | |
[mk7]="/etc/openwrt/mk7.config" | |
[mk6]="/etc/openwrt/mk6.config" | |
[mk6_cc]="/etc/openwrt/mk6.cc.config" | |
[mknano_cc]="/etc/openwrt/mknano.cc.config" | |
) | |
declare -A -r PKG_OUTPUT_DIRS=( | |
[snapshot]="~/gits/openwrt-useful-tools-snapshot" | |
[v24.10]="~/gits/openwrt-useful-tools-24.10" | |
[v23.05]="~/gits/openwrt-useful-tools-23.05" | |
[v22.03]="~/gits/openwrt-useful-tools-22.03" | |
[v21.02]="~/gits/openwrt-useful-tools-21.02" | |
[v19.07]="~/gits/openwrt-useful-tools-19.07" | |
[v18.06]="~/gits/openwrt-useful-tools-18.06" | |
[lede-17.01]="~/gits/openwrt-useful-tools-lede-17.01" | |
[v15.05]="~/gits/openwrt-useful-tools-15.05" | |
[mk7]="~/gits/openwrt-useful-tools-mk7" | |
[mk6]="~/gits/openwrt-useful-tools-mk6" | |
) | |
readonly ORIG_PATH="$PATH" | |
# --- Set the Include Guard --- | |
# Declare the guard variable as readonly to signify the script has been loaded. | |
declare -r OPENWRT_ALIASES_SOURCED=1 | |
fi | |
# --- Helper Functions --- | |
# Functions can be redefined without causing errors, so they live outside the guard. | |
# Check for required command-line tools. | |
function check_dependencies() { | |
local -r deps=("git" "nproc" "scp" "awk" "grep" "find" "tr" "date") | |
for cmd in "${deps[@]}"; do | |
if ! command -v "$cmd" &> /dev/null; then | |
echo "[ERROR]: Required command '$cmd' is not installed. Please install it and try again." >&2 | |
return 1 | |
fi | |
done | |
} | |
# Get current CPU architecture from .config. | |
function get_current_arch() { | |
grep 'CONFIG_ARCH' "$(pwd)/.config" | awk -F'"' '{print $2}' | |
} | |
# Get info about current board from .config. | |
function get_current_board() { | |
grep 'CONFIG_TARGET_BOARD' "$(pwd)/.config" | awk -F'"' '{print $2}' | |
} | |
# Get the current git branch name. | |
function get_current_branch() { | |
git branch --show-current | |
} | |
# Set correct staging_dir directory based on current path. | |
function set_staging_dir() { | |
export STAGING_DIR | |
STAGING_DIR="$(pwd)/staging_dir" | |
} | |
# Get the GCC version from the .config file. | |
function get_gcc_version() { | |
if [[ ! -f ./.config ]]; then | |
echo "[ERROR]: OpenWRT '.config' file not found in $(pwd)." >&2 | |
return 1 | |
fi | |
# Source the line containing CONFIG_GCC_VERSION to set it as a variable | |
# shellcheck source=/dev/null | |
source <(grep "CONFIG_GCC_VERSION" ./.config) | |
} | |
# Locate an OpenWRT .ipk package file and export its path. | |
function find_opkg() { | |
local -r pkg_name="$1" | |
local -r arch="$2" | |
export XFILE | |
# Use find -print -quit to stop after the first match for efficiency. | |
XFILE=$(find "$(pwd)/bin" -type f -name "${pkg_name}*${arch}*.ipk" -print -quit) | |
} | |
# --- Core Logic Functions --- | |
# Set up environment variables and compiler flags based on architecture. | |
# REFACTORED: to remove duplicated logic and improve maintainability. | |
function setup_build_environment() { | |
echo "[INFO]: Setting up build environment..." | |
set_staging_dir | |
get_gcc_version | |
local -r arch=$(get_current_arch) | |
export ARCH="$arch" | |
export BOARD | |
case "$(get_current_board)" in | |
ar71xx) BOARD="MK6" ;; | |
ramips) BOARD="MK7" ;; | |
*) BOARD=$(get_current_board) ;; | |
esac | |
# Store original flags | |
export OCFLAGS="${CFLAGS:-}" | |
export OLDFLAGS="${LDFLAGS:-}" | |
local arch_cflags="" | |
# The case statement is now ONLY for parts that are truly different. | |
case "$ARCH" in | |
mipsel) | |
arch_cflags="-mips16 -mdspr2" | |
;; | |
mips) | |
arch_cflags="-mdspr2" | |
;; | |
*) | |
echo "[WARN]: Unsupported architecture '$ARCH'. Using default flags." | |
;; | |
esac | |
# Construct the final variables from a single template, avoiding repetition. | |
local -r base_cflags="-Os -pipe -mno-branch-likely -mips32r2 -mtune=24kc" | |
local -r toolchain_path="${STAGING_DIR}/toolchain-${ARCH}_24kc_gcc-${CONFIG_GCC_VERSION}_musl/bin/" | |
export LDFLAGS="-L${STAGING_DIR}/target-${ARCH}_24kc_musl/usr/lib" | |
export CFLAGS="${base_cflags} ${arch_cflags} -I${STAGING_DIR}/target-${ARCH}_24kc_musl/usr/include" | |
# Add toolchain to PATH if not already present. | |
if [[ -n "$toolchain_path" && ":$PATH:" != *":$toolchain_path:"* ]]; then | |
export PATH="$toolchain_path:$PATH" | |
fi | |
echo "[STATUS]: Environment configured for Pineapple $BOARD ($ARCH)" | |
echo "[INFO]: GCC version detected as: v${CONFIG_GCC_VERSION}" | |
echo "[CFLAGS]: ${CFLAGS}" | |
echo "[LDFLAGS]: ${LDFLAGS}" | |
} | |
# Determine OpenWRT SDK version from git branch. | |
function get_sdk_version() { | |
export CURVER | |
# UPDATED: Treat both main and master as the snapshot branch for compatibility. | |
case "$(get_current_branch)" in | |
main | master) CURVER="snapshot" ;; | |
openwrt-24.10) CURVER="v24.10" ;; | |
openwrt-23.05) CURVER="v23.05" ;; | |
openwrt-22.03) CURVER="v22.03" ;; | |
openwrt-21.02) CURVER="v21.02" ;; | |
openwrt-19.07) CURVER="v19.07" ;; | |
openwrt-18.06) CURVER="v18.06" ;; | |
lede-17.01) CURVER="lede-17.01" ;; | |
chaos_calmer) CURVER="v15.05" ;; | |
*) CURVER=$(get_current_branch) ;; | |
esac | |
} | |
# Display a status message header. | |
function main_message() { | |
echo "" | |
echo "-----------------------------------------------------------------------------------------" | |
echo "| OpenWRT Build Toolkit v${SCRIPT_VERSION} - Author: Andreas Nilsen <[email protected]>" | |
echo "| Last Updated: ${LAST_UPDATE}" | |
echo "|----------------------------------------------------------------------------------------" | |
echo "| BOARD: Hak5 WiFi Pineapple ${BOARD:-unknown}" | |
echo "| CPU ARCH: ${ARCH:-unknown}" | |
echo "| OpenWRT: ${CURVER:-unknown}" | |
echo "| PACKAGE: ${OPKG:-unknown}" | |
echo "-----------------------------------------------------------------------------------------" | |
echo "" | |
} | |
# Copy the appropriate default .config for the selected board. | |
function copy_board_config() { | |
local -r board_key=$(echo "$BOARD" | tr '[:upper:]' '[:lower:]') # normalize to lowercase | |
# This check is now also performed in opkgmake, but kept here as a safeguard. | |
if [[ -v "BOARD_CONFIGS[$board_key]" ]]; then | |
local -r config_file="${BOARD_CONFIGS[$board_key]}" | |
if [[ -f "$config_file" ]]; then | |
echo "[INFO]: Copying board configuration from $config_file to .config" | |
cp "$config_file" .config | |
else | |
echo "[WARN]: Board config file not found at '$config_file'. Skipping copy." | |
fi | |
else | |
echo "[WARN]: No predefined config for board '$BOARD'." | |
fi | |
} | |
# Transfer the compiled package to the target device via SCP. | |
function transfer_opkg() { | |
local -r board_key=$(echo "$BOARD" | tr '[:upper:]' '[:lower:]') | |
if [[ -z "${XFILE:-}" || ! -f "$XFILE" ]]; then | |
echo "[ERROR]: Package file to transfer not found." >&2 | |
return 1 | |
fi | |
if [[ -v "DEVICE_IP[$board_key]" ]]; then | |
local -r target_path="${DEVICE_IP[$board_key]}" | |
echo "[TRANSFER]: Sending '$XFILE' to '$target_path'..." | |
scp -rpC "$XFILE" "$target_path" | |
echo "[SUCCESS]: Transfer complete." | |
else | |
echo "[WARN]: No device IP configured for '$BOARD'. Skipping transfer." | |
fi | |
} | |
# Restore the shell environment to its original state. | |
function restore_settings() { | |
unset CFLAGS LDFLAGS ARCH BOARD CURVER OPKG XFILE | |
export PATH="$ORIG_PATH" | |
export CFLAGS="${OCFLAGS:-}" | |
export LDFLAGS="${OLDFLAGS:-}" | |
unset OCFLAGS OLDFLAGS | |
echo "[INFO]: Build environment has been restored." | |
} | |
# Verify that the script is being run from within an OpenWRT build directory. | |
function test_is_buildroot() { | |
if [[ ! -f ./scripts/feeds ]]; then | |
echo "[ERROR]: Not in a valid OpenWRT build directory." >&2 | |
return 1 | |
fi | |
} | |
# Update and install all custom feed packages, handling priorities. | |
function install_all_custom_packages() { | |
test_is_buildroot | |
echo "[INFO]: Updating all feeds and installing packages..." | |
./scripts/feeds update -a | |
echo "[INFO]: Re-installing 'custom' feed packages to ensure priority..." | |
./scripts/feeds uninstall -a -p custom | |
./scripts/feeds install -a -p custom | |
echo "[SUCCESS]: All custom feed packages are now installed." | |
} | |
# The main build function. | |
function opkgmaker() { | |
local -r log_dir="$(pwd)/custom-logs" | |
mkdir -p "$log_dir" | |
# Sanitize package name to prevent path traversal or invalid characters in filename. | |
local -r sanitized_opkg_name=$(echo "$OPKG" | tr -c 'a-zA-Z0-9._-' '_') | |
local -r log_file="${log_dir}/${sanitized_opkg_name}-build-$(date +%F-%H%M%S).log" | |
echo "[BUILD]: Starting build for package '$OPKG'. Logging to '$log_file'" | |
# Constructing the command safely before using eval. | |
local -r make_cmd="make package/$OPKG/{clean,download,prepare,compile} package/index" | |
local -r make_opts="V=sc -j$(($(nproc) + 1))" | |
# 'eval' is used here to correctly process the make command string with options. | |
# This is considered safe as all variables are constructed internally and are readonly. | |
time eval "$make_cmd $make_opts" 2>&1 | tee "$log_file" | |
} | |
# --- Main Entry Point Functions --- | |
## | |
# Builds a single package for a specific board. | |
# Usage: opkgmake <package_name> <board_id> | |
# Example: opkgmake aircrack-ng mk7 | |
## | |
function opkgmake() { | |
if [[ $# -lt 2 ]]; then | |
echo "Usage: opkgmake <package_name> <board_id>" >&2 | |
echo "Example: opkgmake aircrack-ng mk7" >&2 | |
return 1 | |
fi | |
export OPKG="$1" | |
local -r board_id_arg="$2" | |
# --- Input Validation --- | |
local -r board_key=$(echo "$board_id_arg" | tr '[:upper:]' '[:lower:]') | |
if [[ ! -v "BOARD_CONFIGS[$board_key]" ]]; then | |
# The keys of the array are extracted into a temporary array to be printed. | |
local -r valid_boards=("${!BOARD_CONFIGS[@]}") | |
echo "[ERROR]: Unknown board ID '$board_id_arg'." >&2 | |
echo "Please use one of: ${valid_boards[*]}" >&2 | |
return 1 | |
fi | |
# Use the validated key for the BOARD variable | |
export BOARD="$board_id_arg" | |
trap restore_settings EXIT HUP INT QUIT TERM # Ensure cleanup happens on exit/error | |
check_dependencies | |
test_is_buildroot | |
copy_board_config | |
setup_build_environment | |
install_all_custom_packages | |
get_sdk_version | |
echo "[INFO]: Cleaning up previous build artifacts for '$OPKG'..." | |
find_opkg "$OPKG" "$ARCH" | |
if [[ -n "${XFILE:-}" ]]; then | |
echo "[INFO]: Removing old package: $XFILE" | |
rm -f "$XFILE" | |
fi | |
main_message | |
sleep 1 | |
opkgmaker | |
echo "[INFO]: Searching for compiled package..." | |
find_opkg "$OPKG" "$ARCH" | |
if [[ -z "${XFILE:-}" || ! -f "$XFILE" ]]; then | |
echo "" | |
echo "[FATAL]: Compilation FAILED for package '$OPKG'." >&2 | |
echo "Please check the log file in the 'custom-logs' directory for errors." >&2 | |
return 1 | |
fi | |
echo "[SUCCESS]: Compilation finished successfully!" | |
echo "[PACKAGE]: Found at $XFILE" | |
main_message | |
local output_dir_key="${CURVER}" | |
if [[ "$BOARD" =~ (mk6|mk7) ]]; then | |
output_dir_key=$(echo "$BOARD" | tr '[:upper:]' '[:lower:]') | |
fi | |
if [[ -v "PKG_OUTPUT_DIRS[$output_dir_key]" ]]; then | |
local -r dest_dir=$(eval echo "${PKG_OUTPUT_DIRS[$output_dir_key]}") # Evaluate ~ | |
mkdir -p "$dest_dir" | |
echo "[COPY]: Copying package to $dest_dir" | |
cp "$XFILE" "$dest_dir/" | |
fi | |
transfer_opkg | |
echo "[COMPLETE]: Process finished for '$OPKG' on '$BOARD'." | |
return 0 | |
} | |
## | |
# Builds a package for both MK6 and MK7 boards. | |
# Usage: opkgmakeall <package_name> | |
## | |
function opkgmakeall() { | |
if [[ $# -lt 1 ]]; then | |
echo "Usage: opkgmakeall <package_name>" >&2 | |
return 1 | |
fi | |
local -r pkg="$1" | |
echo "--- Building for MK7 ---" | |
opkgmake "$pkg" mk7 | |
local -r ret_mk7=$? | |
echo "--- Building for MK6 ---" | |
opkgmake "$pkg" mk6 | |
local -r ret_mk6=$? | |
if (( ret_mk7 != 0 || ret_mk6 != 0 )); then | |
echo "[WARN]: One or more builds failed." | |
return 1 | |
fi | |
echo "[SUCCESS]: All builds completed." | |
return 0 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE# Version 2, December 2004 | |
# Copyright (C) 2023 Andreas Nilsen <[email protected]> | |
# | |
# Everyone is permitted to copy and distribute verbatim or modified | |
# copies of this license document, and changing it is allowed as long | |
# as the name is changed. | |
# | |
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
# | |
# 0. You just DO WHAT THE FUCK YOU WANT TO. | |
# | |
# This script will contain some personal / private material. Keep that in mind if you want to use them, you better tweak them | |
# | |
# | |
# Source OpenWRT build helper scripts | |
[ -f /etc/.openwrt_aliases ] && . /etc/.openwrt_aliases | |
[ -f /etc/.openwrt_aliases ] && . /etc/.openwrt_aliases | |
# | |
# Some info for build scripts | |
export EMAIL="[email protected]" | |
export DEBFULLNAME="FSOCIETY" | |
export LANG=nb_NO.UTF-8 | |
export GITS='/root/gits' | |
# Compilation flags | |
export ARCHFLAGS="-arch x86_64" | |
export CFLAGS="-mtune=znver2 -O3" | |
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' | |
# Plex Token and Debug command for UMS | |
export PLEX_TOKEN='pWhyzJxqSwqVgqK83tLK' | |
export PATH_NO_CONDA='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.dotnet/tools:/usr/local/go/bin:/usr/lib/jvm/java-17-openjdk-amd64/bin:/usr/share/maven/bin:/usr/local/cuda/bin:/usr/local/go/bin:/usr/lib/jvm/java-17-openjdk-amd64/bin:/usr/local/cuda/bin' | |
# | |
# | |
alias umsdbg='journalctl -feu ums.service' | |
alias kismetdbg='journalctl -feu kismet.service' | |
# | |
alias activate='source ~/.zshrc' | |
# | |
function deactivate(){ | |
conda deactivate | |
export PATH=$PATH_NO_CONDA | |
unset CONDA_EXE _CE_M _CE_CONDA CONDA_PYTHON_EXE CONDA_SHLVL | |
return 0 | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# XXX: Function: Return different return values depending on what folder you're in | |
function check_dir() { | |
[[ $PWD/ = $PWD1/ ]] && return 1 || return 0 | |
[[ $PWD/ = $PWD2/ ]] && return 2 || return 0 | |
[[ $PWD/ = $PWD3/ ]] && return 3 || return 0 | |
[[ $PWD/ = $PWD4/ ]] && return 4 || return 0 | |
[[ $PWD/ = $PWD5/ ]] && return 5 || return 0 | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# function to set custom parameters in UMS.conf | |
set_ums_var() { | |
console_output INFO "Setting UMS setting '${1}' to '${2}'" | |
sed -i "s+^${1} =$+${1} = ${2}+g" /etc/UMS.conf | |
} | |
# Fix users SSH keys permissions | |
function sshkeyfix() { | |
PS3='Would you like you create a new key-pair? ' | |
if [ -d ~/.ssh ]; then | |
chmod u=rw,go= ~/.ssh/id_* # chmod key with private settings | |
chmod a=r,u+w ~/.ssh/id_*.pub # chmod key with public settings | |
chmod u=rwx,go= ~/.ssh # chmod ~/.ssh directory with correct settings | |
[ -d ~/.ssh ] && echo '[SUCCESS]: Correctly set permissions for this users SSH keys and directory!' | |
return 0 | |
fi | |
echo'[ERROR]: Cannot locate SSH keys directory for this user!' | |
return 1 | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# Let's try to fix some shit. Unset and set some important variables needed to run properly: | |
export CURL_CA_BUNDLE="/etc/ssl/certs/ca-certificates.crt" | |
# | |
alias reboot='reboot -f --reboot' | |
alias wes='wes --color' | |
alias code='/usr/share/code/code --user-data-dir=/home/andreas/ --no-sandbox &' | |
alias stat='stat -c %a' | |
alias backup-custom='tar -zcvf - /root/custom | ssh [email protected] "cat > /root/custom.tar.gz"' | |
alias backup-openwrt='tar -zcvf - /root/gits/openwrt-22.03 | ssh [email protected] "cat > /root/openwrt-22.03-complete.tar.gz"' | |
alias dl='curl -L -J -O' | |
alias c='git clone --recursive' | |
alias gs='git status' | |
alias gp='git push' | |
alias ga='git add' | |
alias workoff='deactivate' | |
alias gdiff='git diff' | |
alias fetch='git fetch' | |
alias clone='git clone --recursive' | |
alias amend='git commit --amend' | |
#alias ll='ls -lh -L' | |
#alias la='ls -A -L' | |
alias show='git show' | |
alias psa='ps aux' | |
alias s='screen' | |
alias stash='git stash' | |
alias log='git log' | |
alias urd-encode='java -jar /root/gits/USB-Rubber-Ducky/Encoder/encoder.jar' | |
alias nstatp='netstat -tulpn' | |
alias untar='tar xvf' | |
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1]);"' | |
alias url-quote='autoload -U url-quote-magic ; zle -N self-insert url-quote-magic' | |
alias whence='type -a' | |
alias wpi='strings -e l' | |
alias xnetmasq='terminal netmasq' | |
alias xnetstat='terminal netstat' | |
alias xo='xdg-open' | |
alias ...='cd ../../' | |
alias ....='cd ../../../' | |
alias .....='cd ../../../../' | |
alias ......='cd ../../../../../' | |
alias .......='cd ../../../../../../' | |
alias ........='cd ../../../../../../../' | |
alias .........='cd ../../../../../../../../' | |
alias ..........='cd ../../../../../../../../../' | |
alias ...........='cd ../../../../../../../../../../' | |
alias ............='cd ../../../../../../../../../../../' | |
alias .............='cd ../../../../../../../../../../../../' | |
alias zshaliases='nano /etc/.zsh_aliases' | |
alias zshconfig='nano /etc/.zshrc' | |
alias yt2mp3='youtube-dl --extract-audio --audio-format mp3' | |
alias gc='git commit -s -m "Default message: Simple new commit, with few changes"' | |
alias merge='git merge' | |
alias wow='git status' | |
alias 'open'='gnome-open' | |
alias whats-my-ip='dig +short myip.opendns.com @resolver1.opendns.com' | |
alias tmux='tmux -2' | |
alias apkstudio='apkstudio > /tmp/' | |
alias cs="php-cs-fixer fix" | |
# ---------------------------------------------------------------------------------------------------------------- | |
# XXX: Function: In dev: Replaces string in file using sed. uses total of 3 args. | |
function replace-str() { | |
## [[ -z $1 ]] || [[ -z $2 ]] || [[ -z $3 ]] && echo -e '[ERROR]: Replacing string failed! Missing argument!' ; return 0 sed -i s/$1/$2/g $3 filename=$1 -- Delete OLD | |
filename=$1 | |
read -r "search?Enter search string: " | |
read -r "replace?Enter replacement string: " | |
if [[ -f $filename ]]; then | |
if [[ $search != "" && $replace != "" ]]; then | |
sed -i "s/${search}/${replace}/" "$filename" | |
return 0 | |
else | |
echo -e "[ERROR]: Un-defined variable(s)!" | |
echo -e "Did you perhaps not properly try to provide both the 'search' and 'replacement' strings?" | |
return 1 | |
fi | |
else | |
echo -e "[ERROR]: No filename specified!" | |
return 2 | |
fi | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# Fix apt key storage: Stores the GPG keys in /etc/apt/trusted.gpg.d/ | |
apt-fix-key(){ | |
local keyurl=$1 | |
local trustedkey=$2 | |
if [[ -z "$2" ]]; then | |
echo -e "[ERROR]: Missing second argument. (trustedkey)" | |
echo -e " This name will prefix the keyfile, and store it in this folder as such: '/etc/apt/trusted.gpg.d/filename.gpg'" | |
echo -e " Example: apt-fix-key https://url.com/folder/keyfile.gpg superkey - (This will save the key as superkey.gpg in the folder mentioned above)" | |
return 1 | |
elif [[ -z "$1" ]]; then | |
echo -e "[ERROR]: Missing first argument. (keyurl)" | |
echo -e " Example: apt-fix-key https://url.com/folder/keyfile.gpg superkey - (This will save the key as superkey.gpg in the folder mentioned above)" | |
return 2 | |
fi | |
curl -fsSL "${keyurl}" | gpg --dearmor -o /etc/apt/trusted.gpg.d/"${trustedkey}".gpg &>/dev/null | |
echo -e "[SUCCESS]: Downloaded and dearmored the GPG key belonging to: ${trustedkey} into APT's new and secure trusted GPG key system." | |
echo -e " Key-file: '/etc/apt/trusted.gpg.d/${trustedkey}.gpg'" | |
return 0 | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# Generate 10 random alphanumeric characters | |
function rand10gen(){ < /dev/urandom tr -dc '[:alnum:]' | fold -w "${1:-10}" | head -n 1 ;} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# Copy, with progress meter! | |
# Usage: xcp [source] [destination] | |
function xcp(){ | |
local source="$1" | |
local destination="$2" | |
local rand=rand10gen | |
if [[ $# -eq 0 ]]; then | |
echo -e '[ERROR]: No arguments provided' | |
echo -e ' Usage: xcp [source] [destination]' | |
return 1 | |
fi | |
time rsync -rah --info=progress2 --stats "$source" "$destination" 2>&1 | tee /tmp/copylog-"$rand".log | |
echo -e "[SUCCESS]: Copy Finished!\nLog-file can be found here: '/tmp/copylog-$rand.log" | |
return 0 | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# Let's fix APT-KEY-ADD which is now deprecated | |
function apt-key-add2(){ | |
local fingerprint="$1" | |
local keyserver="$2" | |
if [[ $# -eq 0 ]]; then | |
echo -e '[ERROR]: No arguments provided' | |
echo -e ' Usage: apt-key-add2 [key-fingerprint] [optional-keyserver]' | |
return 1 | |
fi | |
if [[ -z $keyserver ]]; then | |
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys "$fingerprint" | |
echo -e '\n[SUCCESS]: Read above output to check if the GPG key was successfully fetched from keyserver: keyserver.ubuntu.com\n' | |
return 0 | |
fi | |
sudo apt-key adv --keyserver "$keyserver" --recv-keys "$fingerprint" | |
echo -e "\n[SUCCESS]: Still. Read above output to check if the GPG key was successfully fetched from keyserver: $keyserver\n" | |
return 0 | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# Another attempt at fixing apt-key | |
function apt-key-add3(){ | |
local fkey="$1" | |
local fname="$2" | |
if [[ -f "/etc/apt/trusted.gpg.d/${fname}.gpg" ]]; then | |
echo -e '\n[ERROR:] Certificate Already Exists!' | |
eeturn 1 | |
fi | |
curl -sS "$fkey" | gpg --dearmor | tee /etc/apt/trusted.gpg.d/"$fname".gpg | |
if [ -f "/etc/apt/trusted.gpg.d/${fname}.gpg" ]; then | |
echo -e '\n[SUCCESS:] Certificate Exported Correctly!' | |
return 0 | |
else | |
echo -e '\n[ERROR:] Could not export certificate!' | |
return 2 | |
fi | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# This will launch a netcat listener within a custom screen session on a custom TCP port | |
function nc_listen() { | |
local session_id="$1" | |
local listen_port="$2" | |
if [[ "$*" == "" ]]; then | |
echo -e "No arguments provided" | |
echo -e "Usage: 'nc_listen [session_id] [tcp_listen_port]'" | |
return 1 | |
fi | |
screen -dmS "${session_id}" bash -c "while true; do nc -lp ${listen_port}; done" | |
echo -e "Sucessfully launched a 'netcat_listener' session!\nScreen-id: '${session_id}' is now active listening on TCP port: '$listen_port'" | |
screen -list | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# Binwalk alias | |
alias binwalk='/usr/bin/binwalk --run-as=root' | |
# ---------------------------------------------------------------------------------------------------------------- | |
# Convert UART/serial firmware dump from custom terminal output-log file to it's original complete firmware binary. Byte for Byte. | |
function fwdump_tobin() { | |
local FILE=$1 | |
local OFILE=$2 | |
if [[ "$*" == "" ]]; then | |
echo -e 'No arguments provided' | |
echo -e "Usage: 'fwdump_tobin [input_file] [output_file]'" | |
return 1 | |
fi | |
< "$FILE" awk -F":" '{print $2}' | cut -b -48 | tr -d " \t\n\r" | xxd -r -p - > "$OFILE" | |
chmod +x "$OFILE" | |
echo -e "\n Finished converting RAW binary dumped firmware from serial logfile, equal to its original content on NAND in the output file." | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# Clone with GIT creds using SSH | |
function sshgit() { git clone --recursive [email protected]:"$1"/"$2" ;} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# hostapd taxonomy function | |
taxonomize() { | |
sig="$1" | |
mac="$2" | |
python -c "import taxonomy; print ';'.join(taxonomy.identify_wifi_device('$sig', '$mac'))" | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# Remove SSH key for host $1 | |
ssh-removehost () { | |
host=$1 | |
ssh-keygen -f ~/.ssh/known_hosts -R "$host" | |
return 0 | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# QEMU VM launcher, need kernel and drive (file) | |
function firmadyne-test () { | |
local _kernel=$1 | |
local _drive=$2 | |
if [[ "$*" == "" ]]; then | |
echo -e "No arguments provided" | |
echo -e "Usage: 'firmadyne-test [kernel] [fw_image]'" | |
return 1 | |
fi | |
qemu-system-mips \ | |
-m 256 \ | |
-M malta \ | |
-kernel "${_kernel}" \ | |
-drive file="${_drive}" \ | |
-append "firmadyne.syscall=0 root=/dev/sda1 console=ttyS0 nandsim.parts=64,64,64,64,64,64,64,64,64,64 rdinit=/firmadyne/preInit.sh rw debug ignore_loglevel print-fatal-signals=1" \ | |
-nographic -serial mon:stdio -monitor telnet::45454,server,nowait | |
} | |
alias cp_x='rsync -aP' | |
# ---------------------------------------------------------------------------------------------------------------- | |
# XXX: Copy func... | |
cp_z() | |
{ | |
strace -q -ewrite cp -- "${1}" "${2}" 2>&1 \ | |
| awk '{ | |
count += $NF | |
if (count % 10 == 0) { | |
percent = count / total_size * 100 | |
printf "%3d%% [", percent | |
for (i=0;i<=percent;i++) | |
printf "=" | |
printf ">" | |
for (i=percent;i<100;i++) | |
printf " " | |
printf "]\r" | |
} | |
} | |
END { print "" }' total_size="$(stat -c '%s' "${1}")" count=0 | |
} | |
# ---------------------------------------------------------------------------------------------------------------- | |
# DKMS Builder function | |
function dkms_b() { | |
local PDIR="$1" | |
local MODNAME="$2" | |
if [ -d "/usr/src" ]; then | |
cd /usr/src || echo -e "[ERROR]: Unable to locate '/usr/src/' directory !\nABORTING !" ; return 1 | |
fi | |
if [ -f "./dkms.conf" ]; then | |
VER=$(sed -n 's/\PACKAGE_VERSION="\(.*\)"/\1/p' dkms.conf) | |
else | |
echo -e "[ERROR]: Unable to locate 'dkms.conf' !\nABORTING!" | |
return 2 | |
fi | |
if [ -d "./$PDIR" ]; then | |
rsync -rvhP ./ /usr/src/"$PDIR" | |
else | |
echo -e '[ERROR]: Unable to locate module provided directory !\nABORTING!' | |
return 3 | |
fi | |
# DKMS Build process | |
dkms add -m "$MODNAME" -v "$VER" | |
dkms build -m "$MODNAME" -v "$VER" | |
dkms install -m "$MODNAME" -v "$VER" | |
echo -e "\n\n[SUCCESS]: Successfully compiled the custom/hacked/tweaked kernel-module: '$' for Linux kernel: '$(uname -r)' !" | |
return 0 | |
} | |
# XXX: Quick edit of these settings: | |
alias opkgconf='nano ~/.openwrt_aliases' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LANG=nb_NO.UTF-8 | |
#LANGUAGE=nb_NO:nb:no_NO:no:en | |
LC_CTYPE="nb_NO.UTF-8" | |
LC_NUMERIC="nb_NO.UTF-8" | |
LC_TIME="nb_NO.UTF-8" | |
LC_COLLATE="nb_NO.UTF-8" | |
LC_MONETARY="nb_NO.UTF-8" | |
LC_MESSAGES="nb_NO.UTF-8" | |
LC_PAPER="nb_NO.UTF-8" | |
LC_NAME="nb_NO.UTF-8" | |
LC_ADDRESS="nb_NO.UTF-8" | |
LC_TELEPHONE="nb_NO.UTF-8" | |
LC_MEASUREMENT="nb_NO.UTF-8" | |
LC_IDENTIFICATION="nb_NO.UTF-8" | |
LC_ALL= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment