Information Security Projects - Port Scanner

I am in middle of solving the port-scanner project.
But the tests had failed. Seems like my way of using socket with connect_ex method could not detect:

  • port 443
  • port 80
  • or maybe more

Any advice? or should i abandon socket module, and switch to nmap module ?

import socket

open_ports = []
  for i in range(first_port, last_port):
    if s.connect_ex((target, i)): 
      continue
    else:
      open_ports.append(i)
  s.close()

My replit link : boilerplate-port-scanner - Replit

Link to the challenge:

Move the close() into your loop. You need to open and close the connection for each port you try. Here’s it’s likely making the first connection but fails the rest since the socket is already in use.

1 Like

From your advice, then i also make sure the socket is initialised and closed every loop.

Then it finally works!!!

image

import socket

open_ports = []
  for i in range(first_port, last_port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    s.settimeout(0.5)

    if s.connect_ex((target, i)): 
      pass
    else:
      open_ports.append(i)
    s.close()

Thank you, my angel!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.