Search code examples
dnsnetcatsocatdig

socat and dig simulate DNS request on localhost DNS server


I am trying to play with socat to simulate a DNS request on localhost. Here is what I did setup DNS server on localhost port 15353 and once request received on 15353, it will forword it to google's DNS server 8.8.8.8:53

socat -v -v TCP-LISTEN:15353,reuseaddr,fork UDP:8.8.8.8:53

Send DNS request on same localhost using dig, specify request port to 15353

dig +tcp example.com @localhost -p 15353

Below are the response I got

On socat server terminal

socat -v -v TCP-LISTEN:15353,reuseaddr,fork UDP:8.8.8.8:53
> 2023/01/24 12:46:13.030249  length=54 from=0 to=53
.4... ........\aexample.com.......).......\f.
.\b........> 2023/01/24 12:46:23.029623  length=54 from=0 to=53
.4.i. ........\aexample.com.......).......\f.
.\b........> 2023/01/24 12:46:49.002657  length=54 from=0 to=53
.4.k. ........\aexample.com.......).......\f.
.\b...Q...0> 2023/01/24 12:46:59.001639  length=54 from=0 to=53
.4... ........\aexample.com.......).......\f.
.\b...Q...0> 2023/01/24 12:47:09.001787  length=54 from=0 to=53
.4E.. ........\aexample.com.......).......\f.

On dig client terminal

dig +tcp example.com @localhost -p 15353
; <<>> DiG 9.16.1-Ubuntu <<>> +tcp example.com @localhost -p 15353
;; global options: +cmd
;; connection timed out; no servers could be reached

Anything wrong on my command ?


Solution

  • Here is the working solution- using socat on UDP set up socat DNS server redirect on localhost(127.0.0.1):

    sudo socat -v -v udp4-listen:15353,reuseaddr,fork udp4:8.8.8.8:53
    

    use dig to ask for DNS of newsblur.com on port 15353 on localhost(127.0.0.1)

    dig  newsblur.com @127.0.0.1 -p 15353
    
    ; <<>> DiG 9.16.1-Ubuntu <<>> newsblur.com @127.0.0.1 -p 15353
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21179
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
    
    ;; QUESTION SECTION:
    ;newsblur.com.          IN  A
    
    ;; ANSWER SECTION:
    newsblur.com.       220 IN  A   128.242.245.221
    
    ;; Query time: 4 msec
    ;; SERVER: 127.0.0.1#15353(127.0.0.1)
    ;; WHEN: Tue Jan 24 18:16:05 CST 2023
    ;; MSG SIZE  rcvd: 46
    

    The previous solution didn't work , seems because I was trying to transfer TCP to UDP ? I am not very sure yet , still figuring out why