├── .gitignore ├── CHANGELOG.md ├── ExampleGraph └── ScanResultExample.png ├── LICENSE ├── PortScanner ├── PortScanExample.py ├── PortScanner.py ├── __init__.py └── etc │ ├── __init__.py │ ├── constants.py │ ├── top100Ports │ ├── top1KPorts │ └── top50Ports ├── README.md ├── _config.yml └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | PortScanner.egg-info 2 | build 3 | dist 4 | *.pyc 5 | .idea -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | Versions are monotonically increased based on Semantic Versioning. 4 | 5 | *** 6 | 7 | ## V0.2 (09/12/2017) 8 | 9 | ## Backward incompatible changes 10 | None 11 | 12 | ## Deprecations 13 | None 14 | 15 | ## Changes 16 | Fixed [issue #1](https://github.com/YaokaiYang-assaultmaster/PythonPortScanner/issues/1). Specifically, fixed Windows has no `SO_REUSEPORT` for TCP socket error and change message encoding to `utf-8` before sending out. 17 | 18 | *** 19 | 20 | ## V0.1 (05/30/2017) 21 | 22 | ## Backward incompatible changes 23 | None 24 | 25 | ## Deprecations 26 | None 27 | 28 | ## Changes 29 | Initialized the whole project. First version finished. 30 | -------------------------------------------------------------------------------- /ExampleGraph/ScanResultExample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YaokaiYang-assaultmaster/PythonPortScanner/cd78087c89cba97ef784b569a242f13ddc19a1b4/ExampleGraph/ScanResultExample.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /PortScanner/PortScanExample.py: -------------------------------------------------------------------------------- 1 | import PortScanner as ps 2 | 3 | 4 | def main(): 5 | # Initialize a Scanner object that will scan top 50 commonly used ports. 6 | scanner = ps.PortScanner(target_ports=50) 7 | 8 | host_name = 'google.com' 9 | 10 | message = 'put whatever message you want here' 11 | 12 | ''' 13 | output contains a dictionary of {port:status} pairs 14 | in which port is the list of ports we scanned 15 | and status is either 'OPEN' or 'CLOSE' 16 | ''' 17 | 18 | # This line sets the thread limit of the scanner to 1500 19 | scanner.set_thread_limit(1500) 20 | 21 | # This line sets the timeout delay to 15s 22 | scanner.set_delay(15) 23 | 24 | # This line shows the target port list of the scanner 25 | scanner.show_target_ports() 26 | ''' 27 | Current port list is: 28 | [blah, blah ....] 29 | ''' 30 | 31 | # This line shows the timeout delay of the scanner 32 | scanner.show_delay() 33 | ''' 34 | Current timeout delay is 15 seconds. 35 | ''' 36 | 37 | # This line shows the top 100 commonly used ports. 38 | scanner.show_top_k_ports(100) 39 | ''' 40 | Top 100 commonly used ports: 41 | [blah, blah ....] 42 | ''' 43 | 44 | output = scanner.scan(host_name, message) 45 | ''' 46 | start scanning website: google.com 47 | server ip is: 172.217.4.110 48 | 80: OPEN 49 | 50 | 443: OPEN 51 | 52 | 2000: OPEN 53 | 54 | 5060: OPEN 55 | 56 | host google.com scanned in 30.956103 seconds 57 | finish scanning! 58 | ''' 59 | 60 | 61 | if __name__ == "__main__": 62 | main() 63 | -------------------------------------------------------------------------------- /PortScanner/PortScanner.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import socket 4 | import platform 5 | import threading 6 | import time 7 | 8 | from etc import constants 9 | 10 | 11 | class PortScanner: 12 | # default ports to be scanned is top 1000 13 | __port_list_top_1000 = constants.port_list_top_1000 14 | __port_list_top_100 = constants.port_list_top_100 15 | __port_list_top_50 = constants.port_list_top_50 16 | 17 | # default thread number limit 18 | __thread_limit = 1000 19 | 20 | # default connection timeout time in seconds 21 | __delay = 10 22 | 23 | 24 | @classmethod 25 | def __usage(cls): 26 | """ 27 | Return the usage information for invalid input host name. 28 | """ 29 | print('python Port Scanner v0.1') 30 | print('please make sure the input host name is in the form of "something.com" or "http://something.com!"\n') 31 | 32 | def __init__(self, target_ports=None): 33 | """ 34 | Constructor of a PortScanner object. If target_ports is a list, this list of ports will be used as 35 | the port list to be scanned. If the target_ports is a int, it should be 50, 100 or 1000, indicating 36 | which default list will be used. 37 | 38 | :param target_ports: if this args is a list, then this list of ports that is going to be scanned, 39 | default to self.__port_list_top_1000. if this args is an int, then it should be 50, 100 or 1000. And 40 | the corresponding default list will be used respectively. 41 | :type target_ports: list or int 42 | """ 43 | if target_ports is None: 44 | self.target_ports = self.__port_list_top_1000 45 | elif type(target_ports) == list: 46 | self.target_ports = target_ports 47 | elif type(target_ports) == int: 48 | self.target_ports = self.check_default_list(target_ports) 49 | 50 | def check_default_list(self, target_port_rank): 51 | """ 52 | Check the input target port rank. The target port rank should be 50, 100 or 1000. 53 | And for a valid input, corresponding port list will be returned. 54 | 55 | :param target_port_rank: top K commonly used port list to be returned. 56 | :return: top K commonly used port list. 57 | """ 58 | if ( 59 | target_port_rank != 50 and 60 | target_port_rank != 100 and 61 | target_port_rank != 1000 62 | ): 63 | raise ValueError( 64 | 'Invalid port rank {}. Should be 50, 100 or 1,000.'.format(target_port_rank) 65 | ) 66 | 67 | if target_port_rank == 50: 68 | return self.__port_list_top_50 69 | elif target_port_rank == 100: 70 | return self.__port_list_top_100 71 | else: 72 | return self.__port_list_top_1000 73 | 74 | def scan(self, host_name, message=''): 75 | """ 76 | This is the function need to be called to perform port scanning 77 | 78 | :param host_name: the hostname that is going to be scanned 79 | :param message: the message that is going to be included in the scanning packets 80 | in order to prevent ethical problem (default: ''). 81 | :return: a dict object containing the scan results for a given host in the form of 82 | {port_number: status} 83 | :rtype: dict 84 | """ 85 | host_name = str(host_name) 86 | if 'http://' in host_name or 'https://' in host_name: 87 | host_name = host_name[host_name.find('://') + 3:] 88 | 89 | print('*' * 60 + '\n') 90 | print('start scanning website: {}'.format(host_name)) 91 | 92 | try: 93 | server_ip = socket.gethostbyname(host_name) 94 | print('server ip is: {}'.format(str(server_ip))) 95 | 96 | except socket.error: 97 | # If the DNS resolution of a website cannot be finished, abort that website. 98 | print('hostname {} unknown!!!'.format(host_name)) 99 | self.__usage() 100 | return {} 101 | # May need to return specific value to indicate the failure. 102 | 103 | start_time = time.time() 104 | output = self.__scan_ports(server_ip, self.__delay, message.encode('utf-8')) 105 | stop_time = time.time() 106 | 107 | print('host {} scanned in {} seconds'.format(host_name, stop_time - start_time)) 108 | print('finished scan!\n') 109 | 110 | return output 111 | 112 | def set_thread_limit(self, limit): 113 | """ 114 | Set the maximum number of thread for port scanning 115 | 116 | :param limit: the maximum number of thread running concurrently, default to 1000. 117 | """ 118 | limit = int(limit) 119 | 120 | if limit <= 0 or limit > 50000: 121 | print( 122 | 'Warning: Invalid thread number limit {}!' 123 | 'Please make sure the thread limit is within the range of (1, 50,000)!'.format(limit) 124 | ) 125 | print('The scanning process will use default thread limit 1,000.') 126 | return 127 | 128 | self.__thread_limit = limit 129 | 130 | def set_delay(self, delay): 131 | """ 132 | Set the time out delay for port scanning in seconds 133 | 134 | :param delay: the time in seconds that a TCP socket waits until timeout, default to 10s. 135 | """ 136 | delay = int(delay) 137 | if delay <= 0 or delay > 100: 138 | print( 139 | 'Warning: Invalid delay value {} seconds!' 140 | 'Please make sure the input delay is within the range of (1, 100)'.format(delay) 141 | ) 142 | print('The scanning process will use the default delay time 10 seconds.') 143 | return 144 | 145 | self.__delay = delay 146 | 147 | def show_target_ports(self): 148 | """ 149 | Print out and return the list of ports being scanned. 150 | 151 | :return: list of ports scanned by current Scanner object. 152 | :rtype: list 153 | """ 154 | print ('Current port list is:') 155 | print (self.target_ports) 156 | return self.target_ports 157 | 158 | def show_delay(self): 159 | """ 160 | Print out and return the delay in seconds that a TCP socket waits until timeout. 161 | 162 | :return: timeout interval of the TCP connection in seconds. 163 | :rtype: int 164 | """ 165 | print ('Current timeout delay is {} seconds.'.format(self.__delay)) 166 | return self.__delay 167 | 168 | def show_top_k_ports(self, k): 169 | """ 170 | Print out and return top K commonly used ports. K should be 50, 100 or 1000. 171 | 172 | :param k: top K list will be returned. 173 | :type k: int 174 | :return: top K commonly used ports. 175 | :rtype: list 176 | """ 177 | port_list = self.check_default_list(k) 178 | print('Top {} commonly used ports:'.format(k)) 179 | print(port_list) 180 | return port_list 181 | 182 | def __scan_ports_helper(self, ip, delay, output, message): 183 | """ 184 | Open multiple threads to perform port scanning 185 | 186 | :param ip: the ip address that is being scanned 187 | :type ip: str 188 | :param delay: the time in seconds that a TCP socket waits until timeout 189 | :type delay: int 190 | :param output: a dict that stores result in {port, status} style pairs. 191 | status can be 'OPEN' or 'CLOSE'. 192 | :type output: dict 193 | :param message: the message that is going to be included in the scanning packets, 194 | in order to prevent ethical problem, default to ''. 195 | :type message: str 196 | """ 197 | port_index = 0 198 | 199 | while port_index < len(self.target_ports): 200 | # Ensure the number of concurrently running threads does not exceed the thread limit 201 | while threading.activeCount() < self.__thread_limit and port_index < len(self.target_ports): 202 | # Start threads 203 | thread = threading.Thread(target=self.__TCP_connect, 204 | args=(ip, self.target_ports[port_index], delay, output, message)) 205 | thread.start() 206 | port_index = port_index + 1 207 | time.sleep(0.01) 208 | 209 | def __scan_ports(self, ip, delay, message): 210 | """ 211 | Controller of the __scan_ports_helper() function 212 | 213 | :param ip: the ip address that is being scanned 214 | :type ip: str 215 | :param delay: the time in seconds that a TCP socket waits until timeout 216 | :type delay: int 217 | :param message: the message that is going to be included in the scanning packets, 218 | in order to prevent ethical problem, default to ''. 219 | :type message: str 220 | :return: a dict that stores result in {port, status} style pairs. 221 | status can be 'OPEN' or 'CLOSE'. 222 | """ 223 | output = {} 224 | 225 | thread = threading.Thread(target=self.__scan_ports_helper, args=(ip, delay, output, message)) 226 | thread.start() 227 | 228 | # Wait until all ports being scanned 229 | while len(output) < len(self.target_ports): 230 | time.sleep(0.01) 231 | continue 232 | 233 | # Print opening ports from small to large 234 | for port in self.target_ports: 235 | if output[port] == 'OPEN': 236 | print('{}: {}\n'.format(port, output[port])) 237 | 238 | return output 239 | 240 | def __TCP_connect(self, ip, port_number, delay, output, message): 241 | """ 242 | Perform status checking for a given port on a given ip address using TCP handshake 243 | 244 | :param ip: the ip address that is being scanned 245 | :type ip: str 246 | :param port_number: the port that is going to be checked 247 | :type port_number: int 248 | :param delay: the time in seconds that a TCP socket waits until timeout 249 | :type delay: int 250 | :param output: a dict that stores result in {port, status} style pairs. 251 | status can be 'OPEN' or 'CLOSE'. 252 | :type output: dict 253 | :param message: the message that is going to be included in the scanning packets, 254 | in order to prevent ethical problem, default to ''. 255 | :type message: str 256 | """ 257 | # Initialize the TCP socket object based on different operating systems. 258 | # All systems except for 'Windows' will be treated equally. 259 | curr_os = platform.system() 260 | if curr_os == 'Windows': 261 | TCP_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 262 | TCP_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 263 | TCP_sock.settimeout(delay) 264 | else: 265 | TCP_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 266 | TCP_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) 267 | TCP_sock.settimeout(delay) 268 | 269 | # Initialize a UDP socket to send scanning alert message if there exists an non-empty message 270 | if message != '': 271 | UDP_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 272 | UDP_sock.sendto(message, (ip, int(port_number))) 273 | 274 | try: 275 | result = TCP_sock.connect_ex((ip, int(port_number))) 276 | if message != '': 277 | TCP_sock.sendall(message) 278 | 279 | # If the TCP handshake is successful, the port is OPEN. Otherwise it is CLOSE 280 | if result == 0: 281 | output[port_number] = 'OPEN' 282 | else: 283 | output[port_number] = 'CLOSE' 284 | 285 | TCP_sock.close() 286 | 287 | except socket.error as e: 288 | # Failed to perform a TCP handshake means the port is probably close. 289 | output[port_number] = 'CLOSE' 290 | pass 291 | -------------------------------------------------------------------------------- /PortScanner/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YaokaiYang-assaultmaster/PythonPortScanner/cd78087c89cba97ef784b569a242f13ddc19a1b4/PortScanner/__init__.py -------------------------------------------------------------------------------- /PortScanner/etc/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YaokaiYang-assaultmaster/PythonPortScanner/cd78087c89cba97ef784b569a242f13ddc19a1b4/PortScanner/etc/__init__.py -------------------------------------------------------------------------------- /PortScanner/etc/constants.py: -------------------------------------------------------------------------------- 1 | """ 2 | This files contains our three list of default ports to be scanned. They are top 50, top 100 and top 1000 ports 3 | commonly used, respectively. 4 | """ 5 | 6 | 7 | port_list_top_1000 = [1, 3, 6, 9, 13, 17, 19, 20, 21, 22, 23, 24, 25, 30, 32, 37, 42, 49, 53, 70, 79, 80, 81, 82, 83, 8 | 84, 9 | 88, 89, 99, 106, 109, 110, 113, 119, 125, 135, 139, 143, 146, 161, 163, 179, 199, 211, 222, 254, 10 | 255, 11 | 259, 264, 280, 301, 306, 311, 340, 366, 389, 406, 416, 425, 427, 443, 444, 458, 464, 481, 497, 12 | 500, 13 | 512, 513, 514, 524, 541, 543, 544, 548, 554, 563, 587, 593, 616, 625, 631, 636, 646, 648, 666, 14 | 667, 15 | 683, 687, 691, 700, 705, 711, 714, 720, 722, 726, 749, 765, 777, 783, 787, 800, 808, 843, 873, 16 | 880, 17 | 888, 898, 900, 901, 902, 911, 981, 987, 990, 992, 995, 999, 1000, 1001, 1007, 1009, 1010, 1021, 18 | 1022, 19 | 1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032, 1033, 1034, 1035, 1036, 1037, 1038, 20 | 1039, 21 | 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055, 22 | 1056, 23 | 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071, 1072, 24 | 1073, 25 | 1074, 1075, 1076, 1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 1085, 1086, 1087, 1088, 1089, 26 | 1090, 27 | 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098, 1099, 1102, 1104, 1105, 1106, 1107, 1110, 1111, 28 | 1112, 29 | 1113, 1117, 1119, 1121, 1122, 1123, 1126, 1130, 1131, 1137, 1141, 1145, 1147, 1148, 1151, 1154, 30 | 1163, 31 | 1164, 1165, 1169, 1174, 1183, 1185, 1186, 1192, 1198, 1201, 1213, 1216, 1217, 1233, 1236, 1244, 32 | 1247, 33 | 1259, 1271, 1277, 1287, 1296, 1300, 1309, 1310, 1322, 1328, 1334, 1352, 1417, 1433, 1443, 1455, 34 | 1461, 35 | 1494, 1500, 1503, 1521, 1524, 1533, 1556, 1580, 1583, 1594, 1600, 1641, 1658, 1666, 1687, 1700, 36 | 1717, 37 | 1718, 1719, 1720, 1723, 1755, 1761, 1782, 1801, 1805, 1812, 1839, 1862, 1863, 1875, 1900, 1914, 38 | 1935, 39 | 1947, 1971, 1974, 1984, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 40 | 2013, 41 | 2020, 2021, 2030, 2033, 2034, 2038, 2040, 2041, 2042, 2045, 2046, 2047, 2048, 2065, 2068, 2099, 42 | 2103, 43 | 2105, 2106, 2111, 2119, 2121, 2126, 2135, 2144, 2160, 2170, 2179, 2190, 2196, 2200, 2222, 2251, 44 | 2260, 45 | 2288, 2301, 2323, 2366, 2381, 2382, 2393, 2399, 2401, 2492, 2500, 2522, 2525, 2557, 2601, 2604, 46 | 2607, 47 | 2638, 2701, 2710, 2717, 2725, 2800, 2809, 2811, 2869, 2875, 2909, 2920, 2967, 2998, 3000, 3003, 48 | 3005, 49 | 3006, 3011, 3013, 3017, 3030, 3052, 3071, 3077, 3128, 3168, 3211, 3221, 3260, 3268, 3283, 3300, 50 | 3306, 51 | 3322, 3323, 3324, 3333, 3351, 3367, 3369, 3370, 3371, 3389, 3404, 3476, 3493, 3517, 3527, 3546, 52 | 3551, 53 | 3580, 3659, 3689, 3703, 3737, 3766, 3784, 3800, 3809, 3814, 3826, 3827, 3851, 3869, 3871, 3878, 54 | 3880, 55 | 3889, 3905, 3914, 3918, 3920, 3945, 3971, 3986, 3995, 3998, 4000, 4001, 4002, 4003, 4004, 4005, 56 | 4045, 57 | 4111, 4125, 4129, 4224, 4242, 4279, 4321, 4343, 4443, 4444, 4445, 4449, 4550, 4567, 4662, 4848, 58 | 4899, 59 | 4998, 5000, 5001, 5002, 5003, 5009, 5030, 5033, 5050, 5054, 5060, 5080, 5087, 5100, 5101, 5120, 60 | 5190, 61 | 5200, 5214, 5221, 5225, 5269, 5280, 5298, 5357, 5405, 5414, 5431, 5440, 5500, 5510, 5544, 5550, 62 | 5555, 63 | 5560, 5566, 5631, 5633, 5666, 5678, 5718, 5730, 5800, 5801, 5810, 5815, 5822, 5825, 5850, 5859, 64 | 5862, 65 | 5877, 5900, 5901, 5902, 5903, 5906, 5910, 5915, 5922, 5925, 5950, 5952, 5959, 5960, 5961, 5962, 66 | 5987, 67 | 5988, 5998, 5999, 6000, 6001, 6002, 6003, 6004, 6005, 6006, 6009, 6025, 6059, 6100, 6106, 6112, 68 | 6123, 69 | 6129, 6156, 6346, 6389, 6502, 6510, 6543, 6547, 6565, 6566, 6580, 6646, 6666, 6667, 6668, 6689, 70 | 6692, 71 | 6699, 6779, 6788, 6792, 6839, 6881, 6901, 6969, 7000, 7001, 7004, 7007, 7019, 7025, 7070, 7100, 72 | 7103, 73 | 7106, 7200, 7402, 7435, 7443, 7496, 7512, 7625, 7627, 7676, 7741, 7777, 7800, 7911, 7920, 7937, 74 | 7999, 75 | 8000, 8001, 8007, 8008, 8009, 8010, 8021, 8031, 8042, 8045, 8080, 8081, 8082, 8083, 8084, 8085, 76 | 8086, 77 | 8087, 8088, 8089, 8093, 8099, 8180, 8192, 8193, 8200, 8222, 8254, 8290, 8291, 8300, 8333, 8383, 78 | 8400, 79 | 8402, 8443, 8500, 8600, 8649, 8651, 8654, 8701, 8800, 8873, 8888, 8899, 8994, 9000, 9001, 9002, 80 | 9009, 81 | 9010, 9040, 9050, 9071, 9080, 9090, 9099, 9100, 9101, 9102, 9110, 9200, 9207, 9220, 9290, 9415, 82 | 9418, 83 | 9485, 9500, 9502, 9535, 9575, 9593, 9594, 9618, 9666, 9876, 9877, 9898, 9900, 9917, 9929, 9943, 84 | 9968, 85 | 9998, 9999, 10000, 10001, 10002, 10003, 10009, 10012, 10024, 10082, 10180, 10215, 10243, 10566, 86 | 10616, 10621, 10626, 10628, 10778, 11110, 11967, 12000, 12174, 12265, 12345, 13456, 13722, 87 | 13782, 88 | 14000, 14238, 14441, 15000, 15002, 15003, 15660, 15742, 16000, 16012, 16016, 16018, 16080, 89 | 16113, 90 | 16992, 17877, 17988, 18040, 18101, 18988, 19101, 19283, 19315, 19350, 19780, 19801, 19842, 91 | 20000, 92 | 20005, 20031, 20221, 20828, 21571, 22939, 23502, 24444, 24800, 25734, 26214, 27000, 27352, 93 | 27355, 94 | 27715, 28201, 30000, 30718, 30951, 31038, 31337, 32768, 32769, 32770, 32771, 32772, 32773, 95 | 32774, 96 | 32775, 32776, 32777, 32778, 32779, 32780, 32781, 32782, 32783, 32784, 33354, 33899, 34571, 97 | 34572, 98 | 35500, 38292, 40193, 40911, 41511, 42510, 44176, 44442, 44501, 45100, 48080, 49152, 49153, 99 | 49154, 100 | 49155, 49156, 49157, 49158, 49159, 49160, 49163, 49165, 49167, 49175, 49400, 49999, 50000, 101 | 50001, 102 | 50002, 50006, 50300, 50389, 50500, 50636, 50800, 51103, 51493, 52673, 52822, 52848, 52869, 103 | 54045, 104 | 54328, 55055, 55555, 55600, 56737, 57294, 57797, 58080, 60020, 60443, 61532, 61900, 62078, 105 | 63331, 106 | 64623, 64680, 65000, 65129, 65389] 107 | port_list_top_100 = [7, 9, 13, 21, 22, 25, 37, 53, 79, 80, 88, 106, 110, 113, 119, 135, 139, 143, 179, 199, 389, 108 | 427, 443, 444, 465, 513, 514, 543, 548, 554, 587, 631, 646, 873, 990, 993, 995, 1025, 1026, 109 | 1027, 1028, 1110, 1433, 1720, 1723, 1755, 1900, 2000, 2049, 2121, 2717, 3000, 3128, 3306, 110 | 3389, 3986, 4899, 5000, 5009, 5051, 5060, 5101, 5190, 5357, 5432, 5631, 5666, 5800, 5900, 111 | 6000, 6646, 7070, 8000, 8008, 8080, 8443, 8888, 9100, 9999, 32768, 49152, 49153, 49154, 112 | 49155, 49156] 113 | port_list_top_50 = [21, 22, 25, 53, 80, 110, 113, 135, 139, 143, 179, 199, 443, 445, 465, 514, 548, 554, 587, 646, 114 | 993, 995, 1025, 1026, 1433, 1720, 1723, 2000, 3306, 3389, 5060, 5666, 5900, 6001, 8000, 8008, 115 | 8080, 8443, 8888, 10000, 32768, 49152, 49154] 116 | -------------------------------------------------------------------------------- /PortScanner/etc/top100Ports: -------------------------------------------------------------------------------- 1 | [7,9,13,21,22,25,37,53,79,80,88,106,110,113,119,135,139,143,179,199,389,427,443,444,465,513,514,543,548,554,587,631,646,873,990,993,995,1025,1026,1027,1028,1110,1433,1720,1723,1755,1900,2000,2049,2121,2717,3000,3128,3306,3389,3986,4899,5000,5009,5051,5060,5101,5190,5357,5432,5631,5666,5800,5900,6000,6646,7070,8000,8008,8080,8443,8888,9100,9999,32768,49152,49153,49154,49155,49156] -------------------------------------------------------------------------------- /PortScanner/etc/top1KPorts: -------------------------------------------------------------------------------- 1 | [1,3,6,9,13,17,19,20,21,22,23,24,25,30,32,37,42,49,53,70,79,80,81,82,83,84,88,89,99,106,109,110,113,119,125,135,139,143,146,161,163,179,199,211,222,254,255,259,264,280,301,306,311,340,366,389,406,416,425,427,443,444,458,464,481,497,500,512,513,514,524,541,543,544,548,554,563,587,593,616,625,631,636,646,648,666,667,683,687,691,700,705,711,714,720,722,726,749,765,777,783,787,800,808,843,873,880,888,898,900,901,902,911,981,987,990,992,995,999,1000,1001,1007,1009,1010,1021,1022,1023,1024,1025,1026,1027,1028,1029,1030,1031,1032,1033,1034,1035,1036,1037,1038,1039,1040,1041,1042,1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,1070,1071,1072,1073,1074,1075,1076,1077,1078,1079,1080,1081,1082,1083,1084,1085,1086,1087,1088,1089,1090,1091,1092,1093,1094,1095,1096,1097,1098,1099,1102,1104,1105,1106,1107,1110,1111,1112,1113,1117,1119,1121,1122,1123,1126,1130,1131,1137,1141,1145,1147,1148,1151,1154,1163,1164,1165,1169,1174,1183,1185,1186,1192,1198,1201,1213,1216,1217,1233,1236,1244,1247,1259,1271,1277,1287,1296,1300,1309,1310,1322,1328,1334,1352,1417,1433,1443,1455,1461,1494,1500,1503,1521,1524,1533,1556,1580,1583,1594,1600,1641,1658,1666,1687,1700,1717,1718,1719,1720,1723,1755,1761,1782,1801,1805,1812,1839,1862,1863,1875,1900,1914,1935,1947,1971,1974,1984,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2013,2020,2021,2030,2033,2034,2038,2040,2041,2042,2045,2046,2047,2048,2065,2068,2099,2103,2105,2106,2111,2119,2121,2126,2135,2144,2160,2170,2179,2190,2196,2200,2222,2251,2260,2288,2301,2323,2366,2381,2382,2393,2399,2401,2492,2500,2522,2525,2557,2601,2604,2607,2638,2701,2710,2717,2725,2800,2809,2811,2869,2875,2909,2920,2967,2998,3000,3003,3005,3006,3011,3013,3017,3030,3052,3071,3077,3128,3168,3211,3221,3260,3268,3283,3300,3306,3322,3323,3324,3333,3351,3367,3369,3370,3371,3389,3404,3476,3493,3517,3527,3546,3551,3580,3659,3689,3703,3737,3766,3784,3800,3809,3814,3826,3827,3851,3869,3871,3878,3880,3889,3905,3914,3918,3920,3945,3971,3986,3995,3998,4000,4001,4002,4003,4004,4005,4045,4111,4125,4129,4224,4242,4279,4321,4343,4443,4444,4445,4449,4550,4567,4662,4848,4899,4998,5000,5001,5002,5003,5009,5030,5033,5050,5054,5060,5080,5087,5100,5101,5120,5190,5200,5214,5221,5225,5269,5280,5298,5357,5405,5414,5431,5440,5500,5510,5544,5550,5555,5560,5566,5631,5633,5666,5678,5718,5730,5800,5801,5810,5815,5822,5825,5850,5859,5862,5877,5900,5901,5902,5903,5906,5910,5915,5922,5925,5950,5952,5959,5960,5961,5962,5987,5988,5998,5999,6000,6001,6002,6003,6004,6005,6006,6009,6025,6059,6100,6106,6112,6123,6129,6156,6346,6389,6502,6510,6543,6547,6565,6566,6580,6646,6666,6667,6668,6689,6692,6699,6779,6788,6792,6839,6881,6901,6969,7000,7001,7004,7007,7019,7025,7070,7100,7103,7106,7200,7402,7435,7443,7496,7512,7625,7627,7676,7741,7777,7800,7911,7920,7937,7999,8000,8001,8007,8008,8009,8010,8021,8031,8042,8045,8080,8081,8082,8083,8084,8085,8086,8087,8088,8089,8093,8099,8180,8192,8193,8200,8222,8254,8290,8291,8300,8333,8383,8400,8402,8443,8500,8600,8649,8651,8654,8701,8800,8873,8888,8899,8994,9000,9001,9002,9009,9010,9040,9050,9071,9080,9090,9099,9100,9101,9102,9110,9200,9207,9220,9290,9415,9418,9485,9500,9502,9535,9575,9593,9594,9618,9666,9876,9877,9898,9900,9917,9929,9943,9968,9998,9999,10000,10001,10002,10003,10009,10012,10024,10082,10180,10215,10243,10566,10616,10621,10626,10628,10778,11110,11967,12000,12174,12265,12345,13456,13722,13782,14000,14238,14441,15000,15002,15003,15660,15742,16000,16012,16016,16018,16080,16113,16992,17877,17988,18040,18101,18988,19101,19283,19315,19350,19780,19801,19842,20000,20005,20031,20221,20828,21571,22939,23502,24444,24800,25734,26214,27000,27352,27355,27715,28201,30000,30718,30951,31038,31337,32768,32769,32770,32771,32772,32773,32774,32775,32776,32777,32778,32779,32780,32781,32782,32783,32784,33354,33899,34571,34572,35500,38292,40193,40911,41511,42510,44176,44442,44501,45100,48080,49152,49153,49154,49155,49156,49157,49158,49159,49160,49163,49165,49167,49175,49400,49999,50000,50001,50002,50006,50300,50389,50500,50636,50800,51103,51493,52673,52822,52848,52869,54045,54328,55055,55555,55600,56737,57294,57797,58080,60020,60443,61532,61900,62078,63331,64623,64680,65000,65129,65389] 2 | -------------------------------------------------------------------------------- /PortScanner/etc/top50Ports: -------------------------------------------------------------------------------- 1 | [21,22,25,53,80,110,113,135,139,143,179,199,443,445,465,514,548,554,587,646,993,995,1025,1026,1433,1720,1723,2000,3306,3389,5060,5666,5900,6001,8000,8008,8080,8443,8888,10000,32768,49152,49154] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Port Scanner v0.2 2 | 3 | __For python version >= 3.0, refer to my new project [py3PortScanner](https://github.com/YaokaiYang-assaultmaster/py3PortScanner) 4 | for a more efficient and elegant implementation that also give you more flexibility in choosing ports to be scanned!__ 5 | 6 | An easy to use Python package that could perform port scanning conveniently. 7 | 8 | An output example is showed as following: 9 | ![Output Example](https://github.com/YaokaiYang-assaultmaster/PythonPortScanner/blob/master/ExampleGraph/ScanResultExample.png) 10 | 11 | ## Installation 12 | 1. Use `git clone https://github.com/YaokaiYang-assaultmaster/PythonPortScanner.git` command to clone the repository to your own machine. 13 | 2. There is 2 methods for you to use this module. 14 | - One is to put `PortScanner package` in your working directory and import `PortScanner` to your code. In this way you can only use it in your current working directory. 15 | - The other is to execute `python setup.py install` in the `PortScanner` package. In this way it will be installed to your python's `site-packages` folder. And after that you can use it globally by `import PortScanner`. 16 | 3. Voilà! You are ready to go! 17 | 18 | ## Usage 19 | 1. Add `import PortScanner` or `from PortScanner import PortScanner`in your code. 20 | 2. Initialize a new PortScanner object using `scanner = PortScanner.PortScanner()` or `scanner = PortScanner()`. You could also put the list of ports you want to scan (if any) as a python `list` and pass it as the `target_ports` argument to the constructor. 21 | 3. Then call `scanner.scan(host_name)` to perform scan task. 22 | 4. __Note that the total scan time for a target website is highly related to the timeout value (delay) set for the Scanner object. Thus for the seek of efficiency, the timeout should not be too long.__ 23 | 24 | ## Documentation 25 | - Constructor 26 | `__init__(self, target_ports=None)` takes a list of ports or an int as the argument. If not provided, it will perform scanning task using default ports. If a list is provided, the list will be used as the list of ports being scanned. If a int is provided (this int need to be 50, 100 or 1000), the top 50, 100 or 1000 commonly used ports will be used. 27 | 28 | - Functions 29 | 1. ` scanner.scan(host_name, message = '')` is the function need to be called to perform port scanning. It takes 2 arguments. 30 | - `host_name` is the hostname that is going to be scanned 31 | - `message` is the message that is going to be included in the scanning packets sent out. This is provided in order to prevent ethical problem. If not provided, no message will be included in the packets. 32 | 2. `scanner.set_thread_limit(limit)` is the function to set the maximum number of threads run concurrently for port scanning. It takes 1 argument. 33 | - `limit` is the maximum number of threads allowed. The valid limit range is 1 to 50,000. The default value is 1,000. 34 | 3. `scanner.set_delay(delay)` is the function to set the timeout delay for port scanning in seconds. It takes 1 argument. 35 | - `delay` the time in seconds that a TCP socket waits until timeout. The valid delay range is 1s to 100s. The default value is 10s. 36 | 4. `scanner.show_target_ports()` is used to get the list of ports being scanned for current Scanner object. 37 | 5. `scanner.show_delay()` is used to get current timeout interval in seconds that a TCP socket waits. 38 | 6. `scanner.show_top_k_ports(k)` is used to get top 50, top 100 or top 1000 port lists. Other k will raise an `ValueError` 39 | 40 | - __An example usage case is showed in `PortScanner/PortScanExample.py`__ 41 | 42 | ## Change logs can be found [here](https://github.com/YaokaiYang-assaultmaster/PythonPortScanner/blob/master/CHANGELOG.md) 43 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup( 4 | name='PortScanner', 5 | version='0.2', 6 | description='Port Scanner for Python', 7 | packages=['PortScanner'], 8 | include_package_data=True, 9 | ) 10 | --------------------------------------------------------------------------------