-
Notifications
You must be signed in to change notification settings - Fork 18
fix(ns-scan): block network scan for large network (/19 or lower) #1493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
packages/ns-api/files/ns.scan
Outdated
| def scan(device, interface): | ||
| ret = [] | ||
| u = EUci() | ||
|
|
||
| if interface: | ||
| netmask = u.get('network', interface, 'netmask') | ||
| netmask_cidr = netmask_to_cidr_notation(netmask) | ||
|
|
||
| # block arp-scan if the subnet is /19 or smaller | ||
| if netmask_cidr is not None and netmask_cidr < 20: | ||
| return utils.validation_error("subnet_too_large_for_scan") | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gsanchietti
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're already executing the code in a try/except, you get rif of the netmask_to_cidr notation function
packages/ns-api/files/ns.scan
Outdated
| ret.append({"interface": i, "device": interfaces[i].get('device', '')}) | ||
| try: | ||
| netmask = u.get('network', i, 'netmask') or "" | ||
| netmask_cidr = netmask_to_cidr_notation(netmask) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| netmask_cidr = netmask_to_cidr_notation(netmask) | |
| netmask_cidr = ipaddress.IPv4Network(f'0.0.0.0/{netmask}').prefixlen |
packages/ns-api/files/ns.scan
Outdated
| def netmask_to_cidr_notation(netmask): | ||
| """Convert a netmask to CIDR notation (ex. 255.255.0.0 -> 16)""" | ||
| if not netmask: | ||
| return None | ||
| try: | ||
| return ipaddress.IPv4Network(f'0.0.0.0/{netmask}').prefixlen | ||
| except: | ||
| return None | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def netmask_to_cidr_notation(netmask): | |
| """Convert a netmask to CIDR notation (ex. 255.255.0.0 -> 16)""" | |
| if not netmask: | |
| return None | |
| try: | |
| return ipaddress.IPv4Network(f'0.0.0.0/{netmask}').prefixlen | |
| except: | |
| return None |
packages/ns-api/files/ns.scan
Outdated
| try: | ||
| interface = utils.get_interface_from_device(u, device) | ||
| netmask = u.get('network', interface, 'netmask') | ||
| netmask_cidr = netmask_to_cidr_notation(netmask) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| netmask_cidr = netmask_to_cidr_notation(netmask) | |
| netmask_cidr = ipaddress.IPv4Network(f'0.0.0.0/{netmask}').prefixlen |
This pull request enhances the network scanning functionality by adding subnet size validation and improving interface information. The most important changes are:
Network scanning improvements:
scanfunction to take bothdeviceandinterfaceparameters, and added logic to block ARP scans on subnets smaller than /20, returning a validation error if the subnet is too large.netmask_to_cidr_notationhelper function to convert a netmask (e.g., 255.255.0.0) into CIDR notation (e.g., 16), using theipaddressmodule.Interface listing enhancements:
list_interfacesfunction to include the netmask in CIDR notation for each interface in its output.Command-line interface updates:
interfaceparameter for scans, ensuring the new validation logic is used.Refs: #1434