├── C++ ├── port-scanner.sln └── port-scanner │ ├── main.cpp │ ├── port-scanner.vcxproj │ ├── port-scanner.vcxproj.filters │ ├── scanner.cpp │ └── scanner.h ├── Go └── port-scanner.go ├── LICENSE ├── Python └── port-scanner.py └── README.md /C++/port-scanner.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "port-scanner", "port-scanner\port-scanner.vcxproj", "{6B1B1E38-59B0-48A6-93A2-3905DD0BD538}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {6B1B1E38-59B0-48A6-93A2-3905DD0BD538}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {6B1B1E38-59B0-48A6-93A2-3905DD0BD538}.Debug|Win32.Build.0 = Debug|Win32 16 | {6B1B1E38-59B0-48A6-93A2-3905DD0BD538}.Release|Win32.ActiveCfg = Release|Win32 17 | {6B1B1E38-59B0-48A6-93A2-3905DD0BD538}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /C++/port-scanner/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | author: Forec 3 | last edit date: 2016/09/25 4 | email: forec@bupt.edu.cn 5 | LICENSE 6 | Copyright (c) 2015-2017, Forec 7 | 8 | Permission to use, copy, modify, and/or distribute this code for any 9 | purpose with or without fee is hereby granted, provided that the above 10 | copyright notice and this permission notice appear in all copies. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 | */ 20 | 21 | #include "scanner.h" 22 | 23 | int main(int argc, char * argv[]){ 24 | std::string IP = "10.201.14.169"; 25 | Scanner *scanner = new Scanner(IP, 300); 26 | if (scanner->scan(0, 65535)) 27 | scanner->printOpenPorts(); 28 | else 29 | std::cout << "ERROR SCANNING" << std::endl; 30 | delete scanner; 31 | system("pause"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /C++/port-scanner/port-scanner.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {6B1B1E38-59B0-48A6-93A2-3905DD0BD538} 15 | Win32Proj 16 | portscanner 17 | 18 | 19 | 20 | Application 21 | true 22 | v120 23 | Unicode 24 | 25 | 26 | Application 27 | false 28 | v120 29 | true 30 | Unicode 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | true 44 | 45 | 46 | false 47 | 48 | 49 | 50 | 51 | 52 | Level3 53 | Disabled 54 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 55 | true 56 | 57 | 58 | Console 59 | true 60 | 61 | 62 | 63 | 64 | Level3 65 | 66 | 67 | MaxSpeed 68 | true 69 | true 70 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) 71 | true 72 | 73 | 74 | Console 75 | true 76 | true 77 | true 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /C++/port-scanner/port-scanner.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 头文件 20 | 21 | 22 | 23 | 24 | 源文件 25 | 26 | 27 | 源文件 28 | 29 | 30 | -------------------------------------------------------------------------------- /C++/port-scanner/scanner.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | author: Forec 3 | last edit date: 2016/09/25 4 | email: forec@bupt.edu.cn 5 | LICENSE 6 | Copyright (c) 2015-2017, Forec 7 | 8 | Permission to use, copy, modify, and/or distribute this code for any 9 | purpose with or without fee is hereby granted, provided that the above 10 | copyright notice and this permission notice appear in all copies. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 | */ 20 | 21 | #include "scanner.h" 22 | 23 | Scanner::Scanner(std::string IP, unsigned int core, 24 | unsigned int timeout, unsigned int miltimeout){ 25 | this->_IP = IP; 26 | this->_timeout = timeout; 27 | this->_miltimeout = miltimeout; 28 | if (core < 1) 29 | this->_core = 1; 30 | else if (core > 1600) 31 | this->_core = 1600; 32 | else 33 | this->_core = core; 34 | this->_scaned = false; 35 | this->_openPorts.clear(); 36 | this->_lastScanTime = 0; 37 | this->hMutex = CreateMutex(NULL, FALSE, NULL); 38 | } 39 | 40 | bool Scanner::scan(unsigned int start, unsigned int end){ 41 | if (end < start || start < 0 || end > 65535){ 42 | std::cout << "Invalid parameters!" << std::endl; 43 | return false; 44 | } 45 | std::cout << "Scan starts..." << std::endl; 46 | unsigned fragment = (end - start) / this->_core; 47 | WORD versionRequired = MAKEWORD(1, 1); 48 | WSADATA wsaData; 49 | int err = WSAStartup(versionRequired, &wsaData); 50 | if (err < 0){ 51 | return false; 52 | } 53 | HANDLE handles[1600]; 54 | unsigned int i = start; 55 | unsigned int coreStack = 0; 56 | while (i <= end){ 57 | scanParam param{ this, this->_IP, i, minport(i + fragment, end) }; 58 | i += (fragment + 1); 59 | handles[coreStack++] = (HANDLE)_beginthreadex(NULL, 0, 60 | scanThread, (void *)(¶m), 0, NULL); 61 | if (handles[coreStack - 1] <= 0){ 62 | coreStack--; 63 | i -= (fragment + 1); 64 | } 65 | //std::cout << coreStack << std::endl; 66 | Sleep(10); 67 | } 68 | std::cout << "Scanning..." << std::endl; 69 | WaitForMultipleObjects(coreStack, handles, TRUE, INFINITE);//(fragment+4)*1000*(this->_timeout+10)); 70 | for (unsigned int i = 0; i < coreStack; i++){ 71 | CloseHandle(handles[i]); 72 | } 73 | WSACleanup(); 74 | this->_scaned = true; 75 | this->_lastScanTime = time(NULL); 76 | std::cout << "Scan Finished..." << std::endl; 77 | return true; 78 | } 79 | 80 | void Scanner::printOpenPorts(){ 81 | if (this->_openPorts.size() == 0){ 82 | std::cout << "No port is opening on " << this->_IP << std::endl; 83 | return; 84 | } 85 | std::cout << "Open ports on " << this->_IP << std::endl; 86 | std::set::iterator iter; 87 | for (iter = this->_openPorts.begin(); iter != this->_openPorts.end(); ++iter){ 88 | std::cout << *iter << std::endl; 89 | } 90 | } 91 | 92 | void Scanner::insertOpenPorts(unsigned int port){ 93 | WaitForSingleObject(this->hMutex, INFINITE); 94 | this->_openPorts.insert(port); 95 | ReleaseMutex(this->hMutex); 96 | } 97 | 98 | unsigned int __stdcall scanThread(void*pM){ 99 | scanParam *param = (scanParam *)pM; 100 | Scanner * scanner = param->scanner; 101 | std::string IP = param->IP; 102 | unsigned int start = param->start; 103 | unsigned int end = param->end; 104 | unsigned int timeout = scanner->getTimeout(); 105 | unsigned int miltimeout = scanner->getMilTimeout(); 106 | for (unsigned int port = start; port <= end; port++){ 107 | SOCKADDR_IN scansock_in; 108 | memset(&scansock_in, 0, sizeof(scansock_in)); 109 | scansock_in.sin_addr.s_addr = inet_addr(IP.c_str()); 110 | scansock_in.sin_family = PF_INET; 111 | scansock_in.sin_port = htons(port); 112 | SOCKET scanSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 113 | if (scanSocket < 0){ 114 | port--; 115 | continue; 116 | } 117 | u_long ul = 1; 118 | ioctlsocket(scanSocket, FIONBIO, &ul); 119 | connect(scanSocket, (SOCKADDR*)&scansock_in, sizeof(SOCKADDR)); 120 | /* set timeout */ 121 | fd_set rfd; 122 | FD_ZERO(&rfd); 123 | FD_SET(scanSocket, &rfd); 124 | struct timeval Tout; 125 | Tout.tv_sec = timeout; 126 | Tout.tv_usec = miltimeout; 127 | int ret = select(0, 0, &rfd, 0, &Tout); 128 | if (ret > 0){ 129 | scanner->insertOpenPorts(port); 130 | } 131 | closesocket(scanSocket); 132 | Sleep(100); 133 | } 134 | return 0; 135 | } -------------------------------------------------------------------------------- /C++/port-scanner/scanner.h: -------------------------------------------------------------------------------- 1 | /* 2 | author: Forec 3 | last edit date: 2016/09/25 4 | email: forec@bupt.edu.cn 5 | LICENSE 6 | Copyright (c) 2015-2017, Forec 7 | 8 | Permission to use, copy, modify, and/or distribute this code for any 9 | purpose with or without fee is hereby granted, provided that the above 10 | copyright notice and this permission notice appear in all copies. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 | */ 20 | 21 | #ifndef __SCANNER_H_ 22 | #define __SCANNER_H_ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #pragma comment(lib,"ws2_32.lib") 31 | 32 | class Scanner{ 33 | private: 34 | std::set _openPorts; 35 | std::string _IP; 36 | bool _scaned; 37 | unsigned int _timeout; 38 | unsigned int _miltimeout; 39 | unsigned short _core; 40 | HANDLE hMutex; 41 | time_t _lastScanTime; 42 | public: 43 | Scanner(std::string, unsigned int core=8000, 44 | unsigned int timeout=2, unsigned int miltimeout = 0); 45 | bool scan(unsigned int start = 0, unsigned int end = 65535); 46 | bool is_scaned() { return _scaned; } 47 | std::set getOpenPorts(){ return _openPorts; } 48 | unsigned int getTimeout(){ return _timeout; } 49 | unsigned int getMilTimeout(){ return _miltimeout; } 50 | time_t getLastTime() { return _lastScanTime; } 51 | void insertOpenPorts(unsigned int); 52 | void printOpenPorts(); 53 | }; 54 | 55 | struct scanParam{ 56 | Scanner *scanner; 57 | std::string IP; 58 | unsigned int start; 59 | unsigned int end; 60 | }; 61 | 62 | unsigned int __stdcall scanThread(void *); 63 | 64 | template 65 | T minport(T a, T b){ 66 | return a < b ? a : b; 67 | }; 68 | 69 | #endif -------------------------------------------------------------------------------- /Go/port-scanner.go: -------------------------------------------------------------------------------- 1 | /* 2 | author: Forec 3 | last edit date: 2016/09/15 4 | email: forec@bupt.edu.cn 5 | LICENSE 6 | Copyright (c) 2015-2017, Forec 7 | 8 | Permission to use, copy, modify, and/or distribute this code for any 9 | purpose with or without fee is hereby granted, provided that the above 10 | copyright notice and this permission notice appear in all copies. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 | */ 20 | 21 | package main 22 | 23 | import ( 24 | "flag" 25 | "fmt" 26 | "net" 27 | "runtime" 28 | "strconv" 29 | ) 30 | 31 | var numCores = flag.Int("cores", 2, "number of CPU cores to use") 32 | var ip = flag.String("host", "127.0.0.1", "target ip address") 33 | 34 | type Scanner struct { 35 | ip string 36 | } 37 | 38 | func (scanner *Scanner) scan(signal chan int, port int) { 39 | _, err := net.Dial("tcp", scanner.ip+":"+strconv.Itoa(port)) 40 | if err != nil { 41 | signal <- 0 42 | return 43 | } 44 | signal <- 0 45 | fmt.Println("Port open:", port) 46 | } 47 | 48 | func main() { 49 | flag.Parse() 50 | runtime.GOMAXPROCS(*numCores) 51 | scanner := Scanner{*ip} 52 | ch := make(chan int, 65535) 53 | for i := 0; i <= 65535; i++ { 54 | go scanner.scan(ch, i) 55 | } 56 | for i := 0; i <= 65535; i++ { 57 | <-ch 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015-2017, Forec 2 | 3 | Permission to use, copy, modify, and/or distribute this code for any 4 | purpose with or without fee is hereby granted, provided that the above 5 | copyright notice and this permission notice appear in all copies. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -------------------------------------------------------------------------------- /Python/port-scanner.py: -------------------------------------------------------------------------------- 1 | # last edit date: 2016/09/25 2 | # author: Forec 3 | # LICENSE 4 | # Copyright (c) 2015-2017, Forec 5 | 6 | # Permission to use, copy, modify, and/or distribute this code for any 7 | # purpose with or without fee is hereby granted, provided that the above 8 | # copyright notice and this permission notice appear in all copies. 9 | 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | 18 | import sys 19 | import threading 20 | import time 21 | from socket import * 22 | 23 | class Scanner(threading.Thread): 24 | def __init__(self, port): 25 | threading.Thread.__init__(self) 26 | self.port = port 27 | def run(self): 28 | sock = socket(AF_INET, SOCK_STREAM) 29 | sock.settimeout(8) 30 | result = sock.connect_ex((target_ip, self.port)) 31 | if result == 0: 32 | lock.acquire() 33 | print("Port Open:", self.port) 34 | lock.release() 35 | 36 | if __name__=='__main__': 37 | host = sys.argv[1] 38 | if len(sys.argv) <= 2: 39 | start_port = 0 40 | end_port = 65535 41 | else: 42 | portstrs = sys.argv[2].split('-') 43 | start_port = int(portstrs[0]) 44 | end_port = int(portstrs[1]) 45 | 46 | target_ip = gethostbyname(host) 47 | lock = threading.Lock() 48 | for port in range(start_port, end_port): 49 | Scanner(port).start() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Port Scanner (端口检测工具) 2 | > This project is a tool for **scanning opened ports** . This tool has three implementations by different languages: `Python 3.5`, `C++` and `Golang 1.6`. The `Go` version is the fastest. The `C++` version remains some bugs, one of them is **program will be blocked when the number of threads exceeds 1600**. If you have any ideas or suggestions, please [e-mail](mailto:forec@bupt.edu.cn) me or open your PR. 3 | 4 | ## Platform 5 | This small script is written in three languages: `Python 3.5`, `Golang 1.6` and `C++`. 6 | * The Visual Studio project file `port-scanner.sln` is in folder `C++`. I wrote these code with VS Ultimate 2013, version `12.0.21005.1 REL`. 7 | * `port-scanner.go` is the implementation of `Golang 1.6`, in folder `Go`. 8 | * `port-scanner.py` is the implementation of `Python 3.5`, in folder `Python`. 9 | * All packages used are built-in. 10 | 11 | ## Usage 12 | * Python version: `python3 port-scanner.py ip_address start_port-end_port`. For example, `python3 port-scanner.py localhost 0-65535`, then the tool will scan all the ports opened in your PC. You can change the timeout value in code. 13 | * Go version: `go run port-scanner.go --host=ip_address --cores=cores`, here `--cores` parameter is used to assigning how many cpu cores to use. Default is 2 CPU cores. You can change the timeout value in code. This version is the fastest. 14 | * C++ version: You need to build the project first. In `main` function, you should create a `Scanner` object, you can specify the ip address, port range and threads you want to use. However, the tool may be slower when the number of threads is beyond 1600. Also, sometimes the `C++` version cannot scan all the opend ports. Maybe I will fix this later. Call it `v0.1` temporarily. 15 | 16 | ## Update-logs 17 | * 2016-9-25: Add this tool. 18 | * 2016-10-7: Build repository. 19 | 20 | # License 21 | All codes in this repository are licensed under the terms you may find in the file named "LICENSE" in this directory. --------------------------------------------------------------------------------