Skip to content

Instantly share code, notes, and snippets.

@c3rb3ru5d3d53c
Last active March 29, 2024 09:20
Show Gist options
  • Save c3rb3ru5d3d53c/3bc8041a182467ccae0207394c1e16b3 to your computer and use it in GitHub Desktop.
Save c3rb3ru5d3d53c/3bc8041a182467ccae0207394c1e16b3 to your computer and use it in GitHub Desktop.
mitmhttp - a simple mitmproxy http redirector tool
#!/usr/bin/env bash
DARKGREEN=$'\e[00;32m'
GREEN=$'\e[01;32m'
TEAL=$'\e[00;36m'
DARKGREY=$'\e[01;30m'
CYAN=$'\e[01;36m'
LIGHTGREY=$'\e[00;37m'
RED=$'\e[00;31m'
PINK=$'\e[01;31m'
BLACK=$'\e[00;30m'
BLUE=$'\e[01;34m'
DARKBLUE=$'\e[00;34m'
WHITE=$'\e[01;37m'
RESET=$'\e[0m'
YELLOW=$'\e[01;33m'
MAGENTA=$'\e[01;35m'
PURPLE=$'\e[00;35m'
MITMPROXYDIR=$/home/$SUDO_USER/.mitmproxy
DATE=$(date +"%Y-%m-%dT%H%M%S")
INTERFACE="eth0"
PORT=8080
ENABLE=0
DISABLE=0
HTTP_PORT=80
HTTPS_PORT=443
function help_menu(){
echo "mitmhttp - a simple mitmproxy http redirector tool"
echo " -h --help Help Menu (optional)"
echo " -i --interface Interface (default=eth0)"
echo " -p --port HTTP Redirect Port (default=8080)"
echo " --http-port HTTP Port (default=80)"
echo " --https-port HTTPS Port (default=443)"
echo " -e --enable Enable HTTP Redirection (required)"
echo " -d --disable Disable HTTP Redirection (required)"
echo "Author: @c3rb3ru5d3d53c"
echo "Examples:"
echo " sudo mitmhttp -i eth0 -p 8080 --http-port 80 --https-port 443 --enable"
echo " sudo mitmhttp --disable"
}
function logging(){
case "$1" in
info)
echo "[${BLUE}...${RESET}] $2" 1>&2
;;
warn)
echo "[${YELLOW}!${RESET}] $2" 1>&2
;;
fail)
echo "[${RED}X${RESET}] $2" 1>&2
;;
success)
echo "[${GREEN}*${RESET}] $2"
;;
*)
echo "[${RED}X${RESET}] log type incorrect" 1>&2
exit 1
;;
esac
}
function ifup {
if [[ ! -d /sys/class/net/${1} ]]; then
return 1
else
[[ $(</sys/class/net/${1}/operstate) == up ]]
fi
}
function root_required(){
if [ "$EUID" -ne 0 ]; then
logging fail "to perform iptable changes root is required"
exit 1
fi
}
command_exists() {
command -v "$1" >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
logging fail "$1 is required but is not installed"
exit 1
fi
}
command_exists "iptables"
command_exists "iptables-save"
command_exists "iptables-restore"
while test $# -gt 0; do
case "$1" in
-h|--help)
help_menu
exit 0
;;
-i|--interface)
shift
if test $# -gt 0; then
INTERFACE=$1
else
logging fail "interface was not specified"
exit 1
fi
;;
-p|--port)
shift
if test $# -gt 0; then
PORT=$1
else
logging fail "port number was not specified"
exit 1
fi
;;
--http-port)
shift
if test $# -gt 0; then
HTTP_PORT=$1
else
logging fail "http port number was not specified"
exit 1
fi
;;
--https-port)
shift
if test $# -gt 0; then
HTTPS_PORT=$1
else
logging fail "http port number was not specified"
exit 1
fi
;;
-e|--enable)
ENABLE=1
;;
-d|--disable)
DISABLE=1
;;
esac
shift
done
if ! ifup $INTERFACE; then
logging fail "the interface $INTERFACE does not exist"
exit 1;
fi
function iptables_save(){
iptables-save > $MITMPROXYDIR/iptables.rules
}
function iptables_restore(){
iptables-restore < $MITMPROXYDIR/iptables.rules
}
if [[ $ENABLE -eq 0 && $DISABLE -eq 0 ]]; then
logging fail "one of enable or disable required"
exit 1
fi
if [[ $ENABLE -eq 1 && $DISABLE -eq 1 ]]; then
logging fail "only one of enable or disable can be used"
exit 1
fi
function main(){
if [ $ENABLE -eq 1 ]; then
root_required
iptables_save
iptables -t nat -A PREROUTING -i $INTERFACE -p tcp --dport $HTTP_PORT -j REDIRECT --to-port $PORT
iptables -t nat -A PREROUTING -i $INTERFACE -p tcp --dport $HTTPS_PORT -j REDIRECT --to-port $PORT
exit 0
fi
if [ $DISABLE -eq 1 ]; then
root_required
iptables_restore
exit 0
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment