-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mohammad Shehar Yaar Tausif <[email protected]>
- Loading branch information
1 parent
b5bc647
commit 00f1fe4
Showing
10 changed files
with
287 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# SPDX-FileCopyrightText: (c) 2024 Mohammad Shehar Yaar Tausif <[email protected]> | ||
# SPDX-License-Identifier: MIT OR GPL-2.0-only | ||
|
||
all: | ||
LUAXTABLE_MODULE=dnsdoctor $(MAKE) -C ../../usr/lib/xtable | ||
|
||
install: | ||
sudo LUAXTABLE_MODULE=dnsdoctor $(MAKE) -C ../../usr/lib/xtable install | ||
|
||
uninstall: | ||
sudo rm -f ${XTABLES_SO_DIR}/libxt_${LUAXTABLE_MODULE}.so | ||
|
||
clean: | ||
LUAXTABLE_MODULE=dnsdoctor $(MAKE) -C ../../usr/lib/xtable clean |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# SPDX-FileCopyrightText: (c) 2024 Mohammad Shehar Yaar Tausif <[email protected]> | ||
# SPDX-License-Identifier: MIT OR GPL-2.0-only | ||
|
||
#!/bin/bash | ||
|
||
set -eux | ||
|
||
rm dnstest -rf | ||
|
||
# backup resolv config | ||
if [[ -f /etc/resolv.conf.lunatik ]] then | ||
echo "Restoring dns config from resolv.conf.lunatik" | ||
sudo rm /etc/resolv.conf | ||
sudo cp /etc/resolv.conf.lunatik /etc/resolv.conf | ||
sudo rm /etc/resolv.conf.lunatik | ||
fi | ||
|
||
# down the interfaces | ||
sudo ip -n ns1 link set veth1 down | ||
sudo ip -n ns2 link set veth3 down | ||
sudo ip link set veth2 down | ||
sudo ip link set veth4 down | ||
|
||
sudo ip addr delete 10.1.1.2/24 dev veth2 | ||
sudo ip -n ns1 addr delete 10.1.1.3/24 dev veth1 | ||
sudo ip addr delete 10.1.2.2/24 dev veth4 | ||
sudo ip -n ns2 addr delete 10.1.2.3/24 dev veth3 | ||
|
||
# delete link between host and the namespaces | ||
sudo ip -n ns1 link delete veth1 | ||
sudo ip -n ns2 link delete veth3 | ||
|
||
# delete namespaces ns1 for dns server ns2 for server | ||
sudo ip netns delete ns1 | ||
sudo ip netns delete ns2 | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
-- | ||
-- SPDX-FileCopyrightText: (c) 2024 Mohammad Shehar Yaar Tausif <[email protected]> | ||
-- SPDX-License-Identifier: MIT OR GPL-2.0-only | ||
-- | ||
|
||
-- DNS Doctoring : Rewrite DNS type A record to a private address for local clients | ||
|
||
local xt = require("xtable") | ||
local linux = require("linux") | ||
local string = require("string") | ||
local action = xt.action | ||
local family = xt.family | ||
|
||
local udp = 0x11 | ||
local dns = 0x35 | ||
|
||
local function nop() end | ||
|
||
local function get_domain(skb, off) | ||
local i = tostring(skb):sub(off+1):find('\0') | ||
return skb:getstring(off, i - 1), i | ||
end | ||
|
||
local function dnsdoctor_tg(skb, par, userargs) | ||
local target_dns, dst_ip, target_ip = string.unpack(">s4 I4 I4", userargs) | ||
local thoff = par.thoff | ||
|
||
local packetdst = skb:getuint32(16) | ||
if packetdst ~= linux.hton32(dst_ip) then | ||
return action.ACCEPT | ||
end | ||
|
||
local srcport = linux.ntoh16(skb:getuint16(thoff)) | ||
if srcport == dns then | ||
local dnsoff = thoff + 8 | ||
local nanswers = linux.ntoh16(skb:getuint16(dnsoff + 6)) | ||
|
||
-- check the domain name | ||
dnsoff = dnsoff + 12 | ||
local domainname, nameoff = get_domain(skb, dnsoff) | ||
|
||
if domainname == target_dns then | ||
dnsoff = dnsoff + nameoff + 4 -- skip over type, label fields | ||
-- iterate over answers | ||
for i = 1,nanswers do | ||
local atype = linux.hton16(skb:getuint16(dnsoff + 2)) | ||
if atype == 1 then | ||
skb:setuint32(dnsoff + 12, linux.hton32(target_ip)) | ||
end | ||
dnsoff = dnsoff + 16 | ||
end | ||
end | ||
end | ||
|
||
return action.ACCEPT | ||
end | ||
|
||
xt.target{ | ||
name = "dnsdoctor", | ||
revision = 0, | ||
family = family.UNSPEC, | ||
proto = 0, | ||
target = dnsdoctor_tg, | ||
checkentry = nop, | ||
destroy = nop, | ||
hooks = 0, | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
local luaxt = require("luaxt") | ||
local family = luaxt.family | ||
|
||
local function noop() end | ||
|
||
local function dnsdoctor_init(par) | ||
local target_ip = "10.1.2.3" | ||
local target = 0 | ||
target_ip:gsub("%d+", function(s) target = target * 256 + tonumber(s) end) | ||
|
||
local src_ip = "10.1.1.2" | ||
local src = 0 | ||
src_ip:gsub("%d+", function(s) src = src * 256 + tonumber(s) end) | ||
|
||
par.userargs = string.pack(">s4 I4 I4", "\x07lunatik\x03com", src, target) | ||
end | ||
|
||
luaxt.target{ | ||
revision = 0, | ||
family = family.UNSPEC, | ||
help = noop, | ||
init = dnsdoctor_init, | ||
print = noop, | ||
save = noop, | ||
parse = noop, | ||
final_check = noop | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# SPDX-FileCopyrightText: (c) 2024 Mohammad Shehar Yaar Tausif <[email protected]> | ||
# SPDX-License-Identifier: MIT OR GPL-2.0-only | ||
|
||
#!/bin/bash | ||
|
||
set -eux | ||
|
||
# add namespaces ns1 for dns server ns2 for server | ||
sudo ip netns add ns1 | ||
sudo ip netns add ns2 | ||
|
||
# add link between host and the namespaces | ||
sudo ip link add veth1 netns ns1 type veth peer name veth2 | ||
sudo ip link add veth3 netns ns2 type veth peer name veth4 | ||
|
||
# add ip address to the links | ||
# DNS IP : 10.1.1.3 | ||
# Server IP : 10.1.2.3 | ||
sudo ip addr add 10.1.1.2/24 dev veth2 | ||
sudo ip -n ns1 addr add 10.1.1.3/24 dev veth1 | ||
sudo ip addr add 10.1.2.2/24 dev veth4 | ||
sudo ip -n ns2 addr add 10.1.2.3/24 dev veth3 | ||
|
||
# up the interfaces | ||
sudo ip -n ns1 link set veth1 up | ||
sudo ip -n ns2 link set veth3 up | ||
sudo ip link set veth2 up | ||
sudo ip link set veth4 up | ||
|
||
# make a directory to setup dns server | ||
mkdir dnstest | ||
cd dnstest | ||
python -m venv .venv | ||
source .venv/bin/activate | ||
pip install dnserver | ||
|
||
# backup resolv config | ||
echo "Backing up resolver config to /etc/resolver.conf.lunatik" | ||
sudo cp -f /etc/resolv.conf /etc/resolv.conf.lunatik && \ | ||
sudo sed -i 's/nameserver/#nameserver/g' /etc/resolv.conf && \ | ||
echo "nameserver 10.1.1.3" | sudo tee -a /etc/resolv.conf && \ | ||
|
||
# add zone info and run dns server in ns1 | ||
echo """ | ||
[[zones]] | ||
host = 'lunatik.com' | ||
type = 'A' | ||
answer = '192.168.10.1' | ||
[[zones]] | ||
host = 'lunatik.com' | ||
type = 'NS' | ||
answer = 'ns1.lunatik.com.' | ||
[[zones]] | ||
host = 'lunatik.com' | ||
type = 'NS' | ||
answer = 'ns2.lunatik.com.' | ||
""" > zones.toml | ||
sudo ip netns exec ns1 .venv/bin/dnserver --no-upstream zones.toml | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.