Skip to content

Commit c5c55dc

Browse files
authored
Merge pull request #57 from dizda/chore/udp-python
chore: small python sscript to test dns query thru udp proxy
2 parents 374e480 + 39c7c76 commit c5c55dc

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

test_udp.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import socket
2+
import socks
3+
import dns.message # pip install dnspython
4+
5+
def create_dns_query(domain):
6+
# Create a DNS query message
7+
query = dns.message.make_query(domain, 'A')
8+
return query.to_wire()
9+
10+
def parse_dns_response(response_data):
11+
# Parse the DNS response
12+
response = dns.message.from_wire(response_data)
13+
return response
14+
15+
# Setup SOCKS connection
16+
s = socks.socksocket(socket.AF_INET, socket.SOCK_DGRAM)
17+
s.set_proxy(socks.SOCKS5, "localhost", 1337) # your SOCKS5 port
18+
19+
try:
20+
# Send DNS query for google.com
21+
query_data = create_dns_query("google.com")
22+
s.sendto(query_data, ("8.8.8.8", 53)) # Google's DNS server
23+
24+
# Receive response
25+
data, addr = s.recvfrom(1024)
26+
27+
# Parse and print response
28+
response = parse_dns_response(data)
29+
print(f"Response from {addr}:")
30+
print(response)
31+
32+
# Print A records
33+
for answer in response.answer:
34+
for item in answer.items:
35+
if item.rdtype == dns.rdatatype.A:
36+
print(f"IP Address: {item}")
37+
38+
except Exception as e:
39+
print(f"Error: {e}")
40+
finally:
41+
s.close()

0 commit comments

Comments
 (0)