Hack Forums
Looking for a Shell Script to Modify DNS Records - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Hacks, Exploits, and Various Discussions (https://hackforums.net/forumdisplay.php?fid=45)
+--- Forum: Blackhat • Red Team • Offensive Security (https://hackforums.net/forumdisplay.php?fid=447)
+---- Forum: Hacking Tools and Programs (https://hackforums.net/forumdisplay.php?fid=10)
+---- Thread: Looking for a Shell Script to Modify DNS Records (/showthread.php?tid=6316787)



Looking for a Shell Script to Modify DNS Records - snattan - Dec 29, 2025

Hi! I’m looking for a shell script that will allow me to edit DNS records. If anyone has something available or can point me in the right direction, I’d appreciate it.

Thanks!


RE: Looking for a Shell Script to Modify DNS Records - DramaKing - Dec 30, 2025

Here's an example using dnsmasq.

Code
#!/bin/bash
[h1]dnsmasq-records.sh[/h1]
[h1]Script to add or update custom DNS records in dnsmasq[/h1]

[h1]CONFIGURATION[/h1]
DNSMASQ_DIR="/etc/dnsmasq.d"
CUSTOM_CONF="$DNSMASQ_DIR/custom-records.conf"
SERVICE_NAME="dnsmasq"

[h1]Ensure running as root[/h1]
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root." >&2
    exit 1
fi

[h1]Validate arguments[/h1]
if [[ $# -ne 2 ]]; then
    echo "Usage: $0 <hostname> <ip-address>"
    echo "Example: $0 myapp.local 192.168.1.50"
    exit 1
fi

HOSTNAME="$1"
IP="$2"

[h1]Validate IP format[/h1]
if ! [[ $IP =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
    echo "Invalid IP address format: $IP" >&2
    exit 1
fi

[h1]Create dnsmasq.d directory if missing[/h1]
mkdir -p "$DNSMASQ_DIR"

[h1]Remove any existing entry for the hostname[/h1]
grep -v "address=/$HOSTNAME/" "$CUSTOM_CONF" 2>/dev/null > "$CUSTOM_CONF.tmp" || true

[h1]Add the new record[/h1]
echo "address=/$HOSTNAME/$IP" >> "$CUSTOM_CONF.tmp"

[h1]Replace the old config[/h1]
mv "$CUSTOM_CONF.tmp" "$CUSTOM_CONF"

[h1]Reload dnsmasq to apply changes[/h1]
if command -v systemctl >/dev/null; then
    systemctl reload "$SERVICE_NAME"
else
    service "$SERVICE_NAME" reload
fi

echo "DNS record updated: $HOSTNAME → $IP"

Run it with 'sudo ./dnsmasq-records.sh myapp.local 192.168.1.50'.


Do be aware if you're spoofing DNS records of TLS and HSTS.


RE: Looking for a Shell Script to Modify DNS Records - snattan - Dec 30, 2025

(Dec 30, 2025 09:51 AM)DramaKing Wrote:  Here's an example using dnsmasq.

Code
#!/bin/bash
[h1]dnsmasq-records.sh[/h1]
[h1]Script to add or update custom DNS records in dnsmasq[/h1]

[h1]CONFIGURATION[/h1]
DNSMASQ_DIR="/etc/dnsmasq.d"
CUSTOM_CONF="$DNSMASQ_DIR/custom-records.conf"
SERVICE_NAME="dnsmasq"

[h1]Ensure running as root[/h1]
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root." >&2
    exit 1
fi

[h1]Validate arguments[/h1]
if [[ $# -ne 2 ]]; then
    echo "Usage: $0 <hostname> <ip-address>"
    echo "Example: $0 myapp.local 192.168.1.50"
    exit 1
fi

HOSTNAME="$1"
IP="$2"

[h1]Validate IP format[/h1]
if ! [[ $IP =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
    echo "Invalid IP address format: $IP" >&2
    exit 1
fi

[h1]Create dnsmasq.d directory if missing[/h1]
mkdir -p "$DNSMASQ_DIR"

[h1]Remove any existing entry for the hostname[/h1]
grep -v "address=/$HOSTNAME/" "$CUSTOM_CONF" 2>/dev/null > "$CUSTOM_CONF.tmp" || true

[h1]Add the new record[/h1]
echo "address=/$HOSTNAME/$IP" >> "$CUSTOM_CONF.tmp"

[h1]Replace the old config[/h1]
mv "$CUSTOM_CONF.tmp" "$CUSTOM_CONF"

[h1]Reload dnsmasq to apply changes[/h1]
if command -v systemctl >/dev/null; then
    systemctl reload "$SERVICE_NAME"
else
    service "$SERVICE_NAME" reload
fi

echo "DNS record updated: $HOSTNAME → $IP"

Run it with 'sudo ./dnsmasq-records.sh myapp.local 192.168.1.50'.


Do be aware if you're spoofing DNS records of TLS and HSTS.

Hi mate, thank you very much — really appreciate it! I’ll give it a try