Skip to content

Commit

Permalink
T06181: make tools for scaning ports
Browse files Browse the repository at this point in the history
show ports local <--- show opened ports on a local host
show ports <HOST_IP> <--- show opened ports and services on a remote host (scan popular ports)
show ports all <HOST_IP> <--- show opened ports and services on a remote host (scan all ports 1-65535)
  • Loading branch information
mkorobeinikov committed Aug 6, 2024
1 parent e55f789 commit 42a31d0
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
17 changes: 17 additions & 0 deletions op-mode-definitions/show-local-ports.xml.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<interfaceDefinition>
<node name="show">
<children>
<node name="ports">
<children>
<node name="local">
<properties>
<help>show opened ports on a local host</help>
</properties>
<command>sudo ${vyos_op_scripts_dir}/scan_localhost.py</command>
</node>
</children>
</node>
</children>
</node>
</interfaceDefinition>
20 changes: 20 additions & 0 deletions op-mode-definitions/show-ports-all.xml.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<interfaceDefinition>
<node name="show">
<children>
<node name="ports">
<children>
<node name="all">
<properties>
<help>show opened ports and services on a remote host (scan all ports 1-65535)</help>
<completionHelp>
<list>&lt;hostname&gt; &lt;x.x.x.x&gt; &lt;h:h:h:h:h:h:h:h&gt;</list>
</completionHelp>
</properties>
<command>sudo ${vyos_op_scripts_dir}/scan_all_ports.py ${@:2}</command>
</node>
</children>
</node>
</children>
</node>
</interfaceDefinition>
19 changes: 19 additions & 0 deletions op-mode-definitions/show-ports.xml.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<interfaceDefinition>
<node name="show">
<children>
<node name="ports">
<children>
<properties>
<help>show opened ports on a remote host (scan popular ports)</help>
<completionHelp>
<list>&lt;hostname&gt; &lt;x.x.x.x&gt; &lt;h:h:h:h:h:h:h:h&gt;</list>
</completionHelp>
</properties>
<command>sudo ${vyos_op_scripts_dir}/scan_ports_popular.py ${@:2}</command>
</node>
</children>
</node>
</children>
</node>
</interfaceDefinition>
37 changes: 37 additions & 0 deletions src/op_mode/scan_all_ports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3

import subprocess
import argparse

def scan_ports(host):
# Define the command to execute
command = ['nmap', '-p-', '-T4', '--min-rate=5000', '--max-retries=1', '--host-timeout=30s', host]

try:
# Execute the command and capture the result
result = subprocess.run(command, capture_output=True, text=True, check=True)

# Extract and print only the lines containing port information
output = result.stdout
start_extracting = False
for line in output.split('\n'):
if line.startswith("PORT"):
start_extracting = True
if start_extracting:
if line.startswith("Nmap done:"):
break
print(line)

except subprocess.CalledProcessError as e:
print(f"Error executing command: {e}")

if __name__ == "__main__":
# Create an argument parser
parser = argparse.ArgumentParser(description='Scan all ports on a remote host using T4 timing template with high rate and reduced retries.')
parser.add_argument('host', type=str, help='IP address or domain name of the host to scan')

# Parse the arguments
args = parser.parse_args()

# Perform the scan
scan_ports(args.host)
28 changes: 28 additions & 0 deletions src/op_mode/scan_localhost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3

import subprocess

def scan_localhost():
try:
# Run the nmap command to list open TCP ports on localhost
result = subprocess.run(
['nmap', '-sT', 'localhost'],
capture_output=True, text=True, check=True
)
output = result.stdout

# Extract only the lines containing port information
start_extracting = False
for line in output.split('\n'):
if line.startswith("PORT"):
start_extracting = True
if start_extracting:
if line.startswith("Nmap done:"):
break
print(line)

except subprocess.CalledProcessError as e:
print(f"Error executing nmap command: {e}")

if __name__ == "__main__":
scan_localhost()
44 changes: 44 additions & 0 deletions src/op_mode/scan_ports_popular.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3

import subprocess
import sys

def scan_popular_ports(host):
# List of popular ports to scan
popular_ports = [
20, 21, 22, 23, 25, 53, 80, 110, 123, 135, 137, 138, 139, 143, 161, 162,
179, 389, 443, 445, 465, 514, 587, 993, 995, 1080, 1433, 1434, 1521, 1723,
3306, 3389, 5060, 5432, 5900, 5938, 8080, 8443, 8888
]

# Create a comma-separated string of ports
ports_str = ",".join(map(str, popular_ports))

try:
# Run the nmap command to scan the specified ports on the given host
result = subprocess.run(
['nmap', '-p', ports_str, host],
capture_output=True, text=True, check=True
)
output = result.stdout

# Extract only the lines containing port information
start_extracting = False
for line in output.split('\n'):
if line.startswith("PORT"):
start_extracting = True
if start_extracting:
if line.startswith("Nmap done:"):
break
print(line)

except subprocess.CalledProcessError as e:
print(f"Error executing nmap command: {e}")

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python scan_popular_ports.py <IP>")
sys.exit(1)

remote_host = sys.argv[1]
scan_popular_ports(remote_host)

0 comments on commit 42a31d0

Please sign in to comment.