title: DNS port listener not receiving connections
body: <p>My objective is to build a server socket listening on DNS port for connections, without responding anything, in order to collect some informations about IP addresses that are looking for DNS servers. The only thing I need to collect is the source IP. I wrote this code:</p>
  
  <pre><code>import socket
  
  
  def create_socket():
      global host
      global port
      global s
      try:
          host = ''
          port = 53
          s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
          s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      except socket.error as msg:
          print('Socekt creation error:' + str(msg))
  
  def bind_socket():
      try:
          print('Binding socket to port: ' + str(port) + ', Host: ' + str(host))
          s.bind((host, port))
      except socket.error as msg:
          print('Socket Binding error: ' + str(msg))
  
  
  def socket_receive():
      while True:
          msg = s.recvfrom(1024)
          print('IP: ' + str(msg[1]) + ',   DATA: ' + msg[0].decode(encoding='cp1252', errors='ignore'))  
  
  def main():
      create_socket()
      bind_socket()
      socket_receive()
  
  main()
  </code></pre>
  
  <p>Unfortunately, I have to run this on a VirtualMachine using VirtualBox. I created a port forwarding rule for the port 53 on my router and I think I did it right, because the site "<a href="https://canyouseeme.org/" rel="nofollow noreferrer">https://canyouseeme.org/</a>" tells me that my ISP is not blocking the port. I created port forwarding rule on the VirtualMachine, so that all the traffic on the port 53 of the host will be redirected to the guest (where my program is). This is what I hope at least. I created a rule even in Windows Firewall allowing connections for the port 53. Finally, I used <em>iptables</em> to allow incoming traffic on the port 53 of my VirtualMachine. I still don't get anything. Probably I'm missing something, maybe I have to use <em>dnslib</em> in python in order to "attract" connections. Or I need to configure something more on Windows or on the router. Maybe I'm missing some important concepts.
  <strong>Why I don't get any connections?</strong></p>
  
  <pre><code>PS C:\WINDOWS\system32&gt; ipconfig
  
  Windows IP Configuration
  
  
  Ethernet adapter Ethernet 2:
  
     Media State . . . . . . . . . . . : Media disconnected
     Connection-specific DNS Suffix  . :
  
  Ethernet adapter Ethernet 3:
  
     Connection-specific DNS Suffix  . :
     Link-local IPv6 Address . . . . . : fe80::e0ce:c8f6:a594:f24d%17
     IPv4 Address. . . . . . . . . . . : 192.168.56.1
     Subnet Mask . . . . . . . . . . . : 255.255.255.0
     Default Gateway . . . . . . . . . :
  
  Wireless LAN adapter Connessione alla rete locale (LAN)* 2:
  
     Media State . . . . . . . . . . . : Media disconnected
     Connection-specific DNS Suffix  . :
  
  Wireless LAN adapter Connessione alla rete locale (LAN)* 3:
  
     Media State . . . . . . . . . . . : Media disconnected
     Connection-specific DNS Suffix  . :
  
  Ethernet adapter Ethernet:
  
     Media State . . . . . . . . . . . : Media disconnected
     Connection-specific DNS Suffix  . :
  
  Wireless LAN adapter Wi-Fi:
  
     Connection-specific DNS Suffix  . : domain.name
     Link-local IPv6 Address . . . . . : fe80::d03d:c2c9:163e:3eb6%6
     IPv4 Address. . . . . . . . . . . : 192.168.1.11
     Subnet Mask . . . . . . . . . . . : 255.255.255.0
     Default Gateway . . . . . . . . . : fe80::b239:56ff:fea9:f35e%6
                                         192.168.1.1
  
  Ethernet adapter Connessione di rete Bluetooth 2:
  
     Media State . . . . . . . . . . . : Media disconnected
     Connection-specific DNS Suffix  . :
  </code></pre>
  
  <pre><code>PS C:\WINDOWS\system32&gt; netstat -na|findstr 53
    TCP    0.0.0.0:53             0.0.0.0:0              LISTENING
    TCP    0.0.0.0:5353           0.0.0.0:0              LISTENING
    TCP    127.0.0.1:5354         0.0.0.0:0              LISTENING
    UDP    0.0.0.0:53             *:*
    UDP    0.0.0.0:5353           *:*
    UDP    0.0.0.0:5353           *:*
    UDP    0.0.0.0:5353           *:*
    UDP    0.0.0.0:5353           *:*
    UDP    0.0.0.0:5353           *:*
    UDP    0.0.0.0:5355           *:*
    UDP    192.168.1.11:5353      *:*
    UDP    192.168.56.1:5353      *:*
    UDP    [::]:5353              *:*
    UDP    [::]:5353              *:*
    UDP    [::]:5353              *:*
    UDP    [::]:5355              *:*
    UDP    [::1]:5353             *:*
  </code></pre>
  
  <p>UFW on the guest:</p>
  
  <pre><code>diego@diego-VirtualBox:~$ sudo ufw status
  [sudo] password di diego: 
  Stato: attivo
  
  A                          Azione      Da
  -                          ------      --
  53                         ALLOW       Anywhere                  
  19                         ALLOW       Anywhere                  
  5353                       ALLOW       Anywhere                  
  123                        ALLOW       Anywhere                  
  53 (v6)                    ALLOW       Anywhere (v6)             
  19 (v6)                    ALLOW       Anywhere (v6)             
  5353 (v6)                  ALLOW       Anywhere (v6)             
  123 (v6)                   ALLOW       Anywhere (v6)
  </code></pre>
  
  <p>ifconfig on the guest</p>
  
  <pre><code>diego@diego-VirtualBox:~$ sudo ifconfig
  enp0s3: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
          inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
          inet6 fe80::7059:da6f:6a4a:8f4e  prefixlen 64  scopeid 0x20&lt;link&gt;
          ether 08:00:27:34:d5:6d  txqueuelen 1000  (Ethernet)
          RX packets 395  bytes 307669 (307.6 KB)
          RX errors 0  dropped 0  overruns 0  frame 0
          TX packets 278  bytes 33866 (33.8 KB)
          TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  
  lo: flags=73&lt;UP,LOOPBACK,RUNNING&gt;  mtu 65536
          inet 127.0.0.1  netmask 255.0.0.0
          inet6 ::1  prefixlen 128  scopeid 0x10&lt;host&gt;
          loop  txqueuelen 1000  (Loopback locale)
          RX packets 35  bytes 3215 (3.2 KB)
          RX errors 0  dropped 0  overruns 0  frame 0
          TX packets 35  bytes 3215 (3.2 KB)
          TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  </code></pre>
  
  <p>iptables on guest:</p>
  
  <pre><code>diego@diego-VirtualBox:~$ sudo iptables -L
  Chain INPUT (policy DROP)
  target     prot opt source               destination         
  ufw-before-logging-input  all  --  anywhere             anywhere            
  ufw-before-input  all  --  anywhere             anywhere            
  ufw-after-input  all  --  anywhere             anywhere            
  ufw-after-logging-input  all  --  anywhere             anywhere            
  ufw-reject-input  all  --  anywhere             anywhere            
  ufw-track-input  all  --  anywhere             anywhere            
  ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
  </code></pre>