Skip to content
Permalink
Browse files

Added Port Scan (Python) Dynamic Folder sample

  • Loading branch information...
lemonmojo committed Dec 28, 2018
1 parent 8fb5fc8 commit f04936fe733ef976dbfdac84f5f6808b6df20198
Showing with 1 addition and 0 deletions.
  1. +1 −0 Dynamic Folder/Port Scan/Port Scan (Python).rdfe
@@ -0,0 +1 @@
{"Name":"Dynamic Folder Export","Objects":[{"Type":"DynamicFolder","Name":"Port Scan (Python)","Notes":"<h2><strong>Port Scan Dynamic Folder sample</strong></h2>\n\n<p><strong>Version</strong>: 1.0<br />\n<strong>Author</strong>: Royal Applications</p>\n\n<p>This Dynamic Folder sample scans your main network interface&#39;s IP subnet for open ports. The connection types/ports to scan can be configured in the &quot;Custom Properties&quot; section.</p>\n\n<h3><strong>Note</strong></h3>\n\n<p>Port scans can take a very long time depending on your subnet size,&nbsp;the number of ports enabled for scanning and the configured connection timeout. Please be patient and/or adjust the configuration as needed.</p>\n\n<h3><strong>Requirements</strong></h3>\n\n<ul>\n\t<li>Python Module: netifaces</li>\n</ul>\n\n<h3><strong>Setup</strong></h3>\n\n<ul>\n\t<li>Enable or disable the connection types you want to be scanned in the&nbsp;&quot;Custom Properties&quot; section.</li>\n\t<li>Configure a timeout (in seconds) for each scanned port in the&nbsp;&quot;Custom Properties&quot; section.</li>\n</ul>\n","CustomProperties":[{"Name":"Connection types to scan for:","Type":"Header","Value":""},{"Name":"SSH","Type":"YesNo","Value":"True"},{"Name":"RDP","Type":"YesNo","Value":"False"},{"Name":"VNC","Type":"YesNo","Value":"False"},{"Name":"HTTP","Type":"YesNo","Value":"False"},{"Name":"HTTPS","Type":"YesNo","Value":"False"},{"Name":"Options","Type":"Header","Value":""},{"Name":"Timeout","Type":"Text","Value":"0.25"}],"ScriptInterpreter":"python","DynamicCredentialScriptInterpreter":"json","DynamicCredentialScript":"{\n\t\"Username\": \"user\",\n\t\"Password\": \"pass\"\n}","Script":"import sys\nimport struct\nimport json\nimport socket\n\nimport netifaces\nfrom netifaces import *\n\n\nENABLE_LOGGING = False\n\nSOCKET_TIMEOUT = $CustomProperty.Timeout$\n\nPORT_MAPPINGS = {\n\t22: {\n\t\t\"Type\": \"TerminalConnection\",\n\t\t\"NamePostfix\": \" - SSH\"\n\t},\n\t80: {\n\t\t\"Type\": \"WebConnection\",\n\t\t\"NamePostfix\": \" - HTTP\"\n\t},\n\t443: {\n\t\t\"Type\": \"WebConnection\",\n\t\t\"NamePostfix\": \" - HTTPS\"\n\t},\n\t3389: {\n\t\t\"Type\": \"RemoteDesktopConnection\",\n\t\t\"NamePostfix\": \" - RDP\"\n\t},\n\t5900: {\n\t\t\"Type\": \"VNCConnection\",\n\t\t\"NamePostfix\": \" - VNC\"\n\t}\n}\n\n\ndef get_prefix(subnet_mask):\n prefix = sum([bin(int(x)).count('1') for x in subnet_mask.split('.')])\n\n return prefix\n\n\ndef get_all_ips_in_subnet(ip, cidr):\n host_bits = 32 - cidr\n i = struct.unpack('>I', socket.inet_aton(ip))[0] # note the endianness\n start = (i >> host_bits) << host_bits # clear the host bits\n end = start | ((1 << host_bits) - 1)\n\n ips = []\n\n # excludes the first and last address in the subnet\n for i in range(start, end):\n ips.append(socket.inet_ntoa(struct.pack('>I', i)))\n\n return ips\n\n\ndef is_port_open(target, port):\n\tsock = socket.socket(AF_INET, socket.SOCK_STREAM)\n\tsock.settimeout(SOCKET_TIMEOUT)\n\n\tresult = sock.connect_ex((target, port))\n\n\tsock.close()\n\n\tif (result == 0):\n\t\treturn True\n\n\treturn False\n\n\ndef create_connection(object_type, name_postfix, host, port):\n\tname = host\n\n\tif name_postfix:\n\t\tname += name_postfix\n\n\tconnection = {\n\t\t\"Type\": object_type,\n\t\t\"Name\": name,\n\t\t\"ComputerName\": host,\n\t\t\"Port\": port,\n\t\t\"Path\": \"/\" + host\n\t}\n\n\treturn connection\n\n\ndef parse_potential_string_bool(potential_bool):\n\tactual_bool = False\n\t\n\tif isinstance(potential_bool, bool):\n\t\tactual_bool = potential_bool\n\telif isinstance(potential_bool, str):\n\t\tpotential_bool = potential_bool.lower()\n\t\tif potential_bool == \"true\" or potential_bool == \"yes\":\n\t\t\tactual_bool = True\n\n\treturn actual_bool\n\n\ndef get_connections(config):\n\tiface_name = netifaces.gateways()[\"default\"][netifaces.AF_INET][1]\n\n\taddresses = [i['addr'] for i in ifaddresses(iface_name).setdefault(AF_INET, [{\"addr\": \"\"}] )]\n\tnetmasks = [i['netmask'] for i in ifaddresses(iface_name).setdefault(AF_INET, [{\"addr\": \"\"}] )]\n\n\tip = addresses[0]\n\tnetmask = netmasks[0]\n\tcidr = get_prefix(netmask)\n\n\tips = get_all_ips_in_subnet(ip, cidr)\n\n\tconnections = [ ]\n\n\tfor target_to_scan in ips:\n\t\tif ENABLE_LOGGING:\n\t\t\tprint(\"Scanning \" + target_to_scan + \"...\")\n\n\t\tfor port_to_scan, props in PORT_MAPPINGS.items():\n\t\t\tif port_to_scan == 22 and not config[\"ssh\"]:\n\t\t\t\tcontinue\n\n\t\t\tif port_to_scan == 3389 and not config[\"rdp\"]:\n\t\t\t\tcontinue\n\n\t\t\tif port_to_scan == 5900 and not config[\"vnc\"]:\n\t\t\t\tcontinue\n\n\t\t\tif port_to_scan == 80 and not config[\"http\"]:\n\t\t\t\tcontinue\n\n\t\t\tif port_to_scan == 443 and not config[\"https\"]:\n\t\t\t\tcontinue\n\n\t\t\tif is_port_open(target_to_scan, port_to_scan):\n\t\t\t\tobject_type = props[\"Type\"]\n\t\t\t\tname_postfix = props[\"NamePostfix\"]\n\n\t\t\t\tconnection = create_connection(object_type, name_postfix, target_to_scan, port_to_scan)\n\n\t\t\t\tif ENABLE_LOGGING:\n\t\t\t\t\tprint(\"Found open port and created connection:\")\n\t\t\t\t\tprint(connection)\n\n\t\t\t\tconnections.append(connection)\n\n\tstore = {\n\t\t\"Objects\": connections\n\t}\n\n\tstore_json = json.dumps(store)\n\n\treturn store_json\n\nconfig = {\n\t\"ssh\": parse_potential_string_bool(\"$CustomProperty.SSH$\"),\n\t\"rdp\": parse_potential_string_bool(\"$CustomProperty.RDP$\"),\n\t\"vnc\": parse_potential_string_bool(\"$CustomProperty.VNC$\"),\n\t\"http\": parse_potential_string_bool(\"$CustomProperty.HTTP$\"),\n\t\"https\": parse_potential_string_bool(\"$CustomProperty.HTTPS$\")\n}\n\nprint(get_connections(config))"}]}

0 comments on commit f04936f

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.