Skip to content

Instantly share code, notes, and snippets.

@wh1t3h47
Last active May 13, 2025 17:19
Show Gist options
  • Save wh1t3h47/9fbb6d106482fc304c42a8fcaa36426e to your computer and use it in GitHub Desktop.
Save wh1t3h47/9fbb6d106482fc304c42a8fcaa36426e to your computer and use it in GitHub Desktop.
Qemu port forwarding for Android, ADB + Frida for reverse engineering. Host to Client server forwarding in order to share files via http. Host IP in client is http://192.168.122.1
#!/usr/bin/env bash
set -euo pipefail
[ "$#" -eq 1 ] || { echo "Usage: $0 <domain>" >&2; exit 1; }
[ "$EUID" -eq 0 ] || { echo "Run as root" >&2; exit 1; }
domain=$1
read network mac <<<"$(virsh domiflist "$domain" | awk '/network/ {print $3, $5; exit}')"
[ -n "$network" ] || { echo "Error: cannot detect network for domain '$domain'" >&2; exit 1; }
guest_ip=$(virsh net-dhcp-leases "$network" | awk -v m="$mac" 'tolower($2)==tolower(m){split($5,a,"/"); print a[1]}')
if [ -z "$guest_ip" ]; then
bridge=$(virsh net-info "$network" | awk '/Bridge:/{print $2}')
[ -n "$bridge" ] || { echo "Error: cannot detect bridge for network '$network'" >&2; exit 1; }
guest_ip=$(ip neigh show dev "$bridge" | awk -v m="$mac" 'tolower($5)==tolower(m){print $1; exit}')
if [ -z "$guest_ip" ]; then
ping -c1 -W1 "$domain" >/dev/null 2>&1 || true
arping -c1 -I "$bridge" "$mac" >/dev/null 2>&1 || true
net_cidr=$(ip route show dev "$bridge" | awk '/proto kernel/ {print $1; exit}')
prefix=${net_cidr%.*}
for i in $(seq 1 254); do
ping -c1 -W1 "$prefix.$i" >/dev/null 2>&1
done
guest_ip=$(ip neigh show dev "$bridge" | awk -v m="$mac" 'tolower($5)==tolower(m){print $1; exit}')
fi
fi
[ -n "$guest_ip" ] || { echo "Error: unable to determine IP for MAC '$mac'" >&2; exit 1; }
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -N QEMU_80 2>/dev/null || true
iptables -t nat -F QEMU_80
iptables -t nat -A PREROUTING -p tcp --dport 80 -j QEMU_80
iptables -t nat -A QEMU_80 -p tcp --dport 80 -j DNAT --to-destination "${guest_ip}:80"
iptables -t nat -C POSTROUTING -p tcp -d "$guest_ip" --dport 80 -j MASQUERADE 2>/dev/null || \
iptables -t nat -A POSTROUTING -p tcp -d "$guest_ip" --dport 80 -j MASQUERADE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment