This script in Rocket language is designed to check open ports on the target IP address using the socket library (racket/tcp). It can help the network administrator identify the services that were available on the network.
Sample code:
#lang racket
(require racket/tcp) ; Importing a module for working with sockets
;; The function checks whether the TCP port on the host is open
(define (port-open? host port)
(with-handlers ([exn:fail? (lambda (e) #f)]) ; In case of an error, we consider the port closed
(define-values (in out) (tcp-connect host port)) ; Trying to establish a connection
(close-input-port in)
(close-output-port out)
#t)) ; If the connection is successful, the port is open
;; The function scans a range of ports on the target host
(define (scan-ports host start-port end-port)
(for ([port (in-range start-port (+ end-port 1))]) ; Going through the ports in the range
(when (port-open? host port) ; If the port is open
(printf "Port ~a is open\n" port)))) ; We are displaying a message about an open port
;; ==== Usage example ====
(define target-ip "192.168.1.1") ; Specify the IP address of the target host
(define port-from 20) ; Initial port for scanning
(define port-to 1024) ; Destination port for scanning
(scan-ports target-ip port-from port-to) ; Start scanning
Explanations:
- The script iterates through the specified range of ports and checks, is it possible to establish a TCP connection with each of them.
- If the connection is successful, the port is considered open.
- To speed up scanning of a large range, you can implement parallel checks (for example, using streams).
- Use this script only to test your devices or with the permission of the network owner!
Usage example:
The script is useful for conducting penetration tests into a network security system to provide recommendations on open ports that are susceptible to malicious activity. It can also be used to test firewalls or services among staff.
Tip:
Keep in mind that port scanners must be run on networks that you are allowed to scan, as using the scanner on other networks may be illegal.