【问题标题】:DNS port listener not receiving connectionsDNS 端口侦听器未接收连接
【发布时间】:2020-05-27 20:56:24
【问题描述】:

我的目标是建立一个服务器套接字,在 DNS 端口上侦听连接,而不响应任何内容,以收集有关正在寻找 DNS 服务器的 IP 地址的一些信息。我唯一需要收集的是源 IP。我写了这段代码:

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()

不幸的是,我必须使用 VirtualBox 在 VirtualMachine 上运行它。我为路由器上的端口 53 创建了一个端口转发规则,我认为我做得对,因为站点“https://canyouseeme.org/”告诉我我的 ISP 没有阻止该端口。我在 VirtualMachine 上创建了端口转发规则,以便将主机端口 53 上的所有流量重定向到来宾(我的程序所在的位置)。至少这是我希望的。我什至在 Windows 防火墙中创建了一个规则,允许端口 53 的连接。最后,我使用 iptables 来允许我的 VirtualMachine 的端口 53 上的传入流量。我还是什么都得不到。可能我遗漏了一些东西,也许我必须在 python 中使用 dnslib 才能“吸引”连接。或者我需要在 Windows 或路由器上配置更多内容。也许我错过了一些重要的概念。 为什么我没有任何连接?

PS C:\WINDOWS\system32> 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  . :
PS C:\WINDOWS\system32> 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             *:*

UFW 对客人:

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)

来宾上的 ifconfig

diego@diego-VirtualBox:~$ sudo ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  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<link>
        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<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        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

来宾上的 iptables:

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

【问题讨论】:

  • 问题不在代码中,它似乎工作正常。在这种情况下,你能分享你的网络设置吗?您的主机和来宾的 IP,以及您在 VM 中为来宾创建的网络接口卡的模式。尝试进行分层检查,从访客开始,然后从主机开始,然后从与您的主机在同一子网中的另一个节点开始。
  • 添加了我认为有用的所有信息

标签: python dns port virtualbox listener


【解决方案1】:

调试此问题的标准方法是使用 wireshark (https://www.wireshark.org/#download) 或其他数据包嗅探器。您可以将它安装在您的 Windows 主机和来宾实例上。假设你的网络是这样的:

                                ---------------------------
------------      --------      |         |---------------|
|dns client| ---- |router| ---- |win host | guest instance| 
------------      --------      ---------------------------
  1. 在 Windows 主机的 LAN 适配器 (192.168.1.11) 上侦听 tcp 53 数据包。如果那里没有显示数据包,则说明端口转发/防火墙或 win 主机防火墙有问题。请记住,您的家庭路由器可能会充当 DNS 服务器并吞下数据包。
  2. 在来宾实例上侦听 tcp 53 数据包。 (enp0s3) 如果您在 Windows 主机级别看到数据包,但没有在客户机级别看到数据包,您可以尝试切换使用桥接而不是 NAT 的适配器类型,反之亦然。

另外,最好先在路由器内部使用您的 DNS 客户端进行调试,以确保它确实向您的蜜罐发出请求。如果您使用 nslookup 或其他标准客户端,则需要确保已将 Windows 主机设置为域控制器。例如

% nslookup 
> server 192.168.1.1
Default server: 192.168.1.1
Address: 192.168.1.1#53
> 

如果一切正常,我猜人们根本没有探测您的网络,或者您的 ISP 具有智能入侵防御,不会阻止 canyouseme,但会阻止 dns 扫描 ala netcat。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-31
    • 2022-11-11
    • 1970-01-01
    • 2021-08-17
    • 2012-10-29
    • 2011-10-03
    • 2018-11-05
    • 2014-04-28
    相关资源
    最近更新 更多