Search code examples
pythonnetworkingportnmap

Nmap scan cannot see the open ports even if it is really open


I have 3 PCs. The first PC's IP address is 10.0.0.6 which is a Wi-Fi hotspot point and it has an Ubuntu 22.04 operating system (Raspberry Pi). The second PC's IP address is 10.0.0.52 which is connected to the Wi-Fi hotspot point and has a Xubuntu operating system on it. The third PC's IP address is 10.0.0.86 which is connected to the Wi-Fi hotspot point and has a Kali-Linux operating system on it. Those 3 PCs are on the same network. I can ping them one by one and they can send and receive the packets.

I am doing a based stream by using the following code from the first PC.

import cv2
import socket
import sys
import pickle
import struct

# Raspberry Pi camera setup
cap = cv2.VideoCapture(0)
cap.set(3, 640)  # Set width
cap.set(4, 480)  # Set height

# Create a socket connection to the receiver (Ubuntu machine)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to a specific local address and port
local_port = 8086  # Change this to your desired local port
client_socket.bind(('10.0.0.6', local_port))

# Specify the remote server's IP address and port to connect to
remote_server_ip = '10.0.0.52'
remote_server_port = 8085  # Replace with your Ubuntu machine's port
client_socket.connect((remote_server_ip, remote_server_port))

try:
    while True:
        # Read a frame from the camera
        ret, frame = cap.read()

        # Serialize the frame
        data = pickle.dumps(frame)

        # Send the frame size
        client_socket.sendall(struct.pack("L", len(data)) + data)

except KeyboardInterrupt:
    cap.release()
    client_socket.close()

Also, I am receiving packets from the second PC by using the following code,

import cv2
import socket
import pickle
import struct

# Create a socket server
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('10.0.0.52', 8085))  # Use 0.0.0.0 to listen on all available network interfaces
server_socket.listen(10)

print("Waiting for a connection...")
conn, addr = server_socket.accept()
print("Connected to", addr)

data = b""
payload_size = struct.calcsize("L")

while True:
    while len(data) < payload_size:
        data += conn.recv(4096)

    packed_msg_size = data[:payload_size]
    data = data[payload_size:]
    msg_size = struct.unpack("L", packed_msg_size)[0]

    while len(data) < msg_size:
        data += conn.recv(4096)

    frame_data = data[:msg_size]
    data = data[msg_size:]

    # Deserialize the received frame
    frame = pickle.loads(frame_data)

    # Display the received frame
    cv2.imshow('Received Frame', frame)
    cv2.waitKey(1)

When I check the communication between the first and second PC by using the following command from the first PC,

sudo tcpdump -i wlan0 -nn

I can see they are receiving and sending packets to each other by using the 8085 port and 8086 port respectively. When I try to scan the network by using the following command from the third PC,

sudo nmap -p 8080-8090 10.0.0.52

I can see the 8085 port is open. However, when I try to scan the first PC (Wi-Fi hotspot point) by using the same command from the third PC,

sudo nmap -p 8080-8090 10.0.0.6 

I cannot see the 8085 or 80806 port is open. I also checked all firewall rules on the first PC, it does not exist any firewall rules and I disabled them. What is wrong here? What am I missing?


Solution

  • @Barmar mentioned in his response:

    A port is only OPEN if you call listen() on the socket. The first script never calls listen(), but the second script does.

    It does make sense. The question is solved.