Skip to content

Commit

Permalink
build repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Forec committed Oct 7, 2016
1 parent 058f58d commit 71b9b5b
Show file tree
Hide file tree
Showing 10 changed files with 521 additions and 2 deletions.
22 changes: 22 additions & 0 deletions C++/port-scanner.sln
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "port-scanner", "port-scanner\port-scanner.vcxproj", "{6B1B1E38-59B0-48A6-93A2-3905DD0BD538}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6B1B1E38-59B0-48A6-93A2-3905DD0BD538}.Debug|Win32.ActiveCfg = Debug|Win32
{6B1B1E38-59B0-48A6-93A2-3905DD0BD538}.Debug|Win32.Build.0 = Debug|Win32
{6B1B1E38-59B0-48A6-93A2-3905DD0BD538}.Release|Win32.ActiveCfg = Release|Win32
{6B1B1E38-59B0-48A6-93A2-3905DD0BD538}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
33 changes: 33 additions & 0 deletions C++/port-scanner/main.cpp
@@ -0,0 +1,33 @@
/*
author: Forec
last edit date: 2016/09/25
email: forec@bupt.edu.cn
LICENSE
Copyright (c) 2015-2017, Forec <forec@bupt.edu.cn>
Permission to use, copy, modify, and/or distribute this code for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "scanner.h"

int main(int argc, char * argv[]){
std::string IP = "10.201.14.169";
Scanner *scanner = new Scanner(IP, 300);
if (scanner->scan(0, 65535))
scanner->printOpenPorts();
else
std::cout << "ERROR SCANNING" << std::endl;
delete scanner;
system("pause");
return 0;
}
90 changes: 90 additions & 0 deletions C++/port-scanner/port-scanner.vcxproj
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{6B1B1E38-59B0-48A6-93A2-3905DD0BD538}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>portscanner</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="scanner.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="scanner.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
30 changes: 30 additions & 0 deletions C++/port-scanner/port-scanner.vcxproj.filters
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="scanner.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="scanner.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>
135 changes: 135 additions & 0 deletions C++/port-scanner/scanner.cpp
@@ -0,0 +1,135 @@
/*
author: Forec
last edit date: 2016/09/25
email: forec@bupt.edu.cn
LICENSE
Copyright (c) 2015-2017, Forec <forec@bupt.edu.cn>
Permission to use, copy, modify, and/or distribute this code for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "scanner.h"

Scanner::Scanner(std::string IP, unsigned int core,
unsigned int timeout, unsigned int miltimeout){
this->_IP = IP;
this->_timeout = timeout;
this->_miltimeout = miltimeout;
if (core < 1)
this->_core = 1;
else if (core > 1600)
this->_core = 1600;
else
this->_core = core;
this->_scaned = false;
this->_openPorts.clear();
this->_lastScanTime = 0;
this->hMutex = CreateMutex(NULL, FALSE, NULL);
}

bool Scanner::scan(unsigned int start, unsigned int end){
if (end < start || start < 0 || end > 65535){
std::cout << "Invalid parameters!" << std::endl;
return false;
}
std::cout << "Scan starts..." << std::endl;
unsigned fragment = (end - start) / this->_core;
WORD versionRequired = MAKEWORD(1, 1);
WSADATA wsaData;
int err = WSAStartup(versionRequired, &wsaData);
if (err < 0){
return false;
}
HANDLE handles[1600];
unsigned int i = start;
unsigned int coreStack = 0;
while (i <= end){
scanParam param{ this, this->_IP, i, minport<unsigned int>(i + fragment, end) };
i += (fragment + 1);
handles[coreStack++] = (HANDLE)_beginthreadex(NULL, 0,
scanThread, (void *)(&param), 0, NULL);
if (handles[coreStack - 1] <= 0){
coreStack--;
i -= (fragment + 1);
}
//std::cout << coreStack << std::endl;
Sleep(10);
}
std::cout << "Scanning..." << std::endl;
WaitForMultipleObjects(coreStack, handles, TRUE, INFINITE);//(fragment+4)*1000*(this->_timeout+10));
for (unsigned int i = 0; i < coreStack; i++){
CloseHandle(handles[i]);
}
WSACleanup();
this->_scaned = true;
this->_lastScanTime = time(NULL);
std::cout << "Scan Finished..." << std::endl;
return true;
}

void Scanner::printOpenPorts(){
if (this->_openPorts.size() == 0){
std::cout << "No port is opening on " << this->_IP << std::endl;
return;
}
std::cout << "Open ports on " << this->_IP << std::endl;
std::set<unsigned int>::iterator iter;
for (iter = this->_openPorts.begin(); iter != this->_openPorts.end(); ++iter){
std::cout << *iter << std::endl;
}
}

void Scanner::insertOpenPorts(unsigned int port){
WaitForSingleObject(this->hMutex, INFINITE);
this->_openPorts.insert(port);
ReleaseMutex(this->hMutex);
}

unsigned int __stdcall scanThread(void*pM){
scanParam *param = (scanParam *)pM;
Scanner * scanner = param->scanner;
std::string IP = param->IP;
unsigned int start = param->start;
unsigned int end = param->end;
unsigned int timeout = scanner->getTimeout();
unsigned int miltimeout = scanner->getMilTimeout();
for (unsigned int port = start; port <= end; port++){
SOCKADDR_IN scansock_in;
memset(&scansock_in, 0, sizeof(scansock_in));
scansock_in.sin_addr.s_addr = inet_addr(IP.c_str());
scansock_in.sin_family = PF_INET;
scansock_in.sin_port = htons(port);
SOCKET scanSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (scanSocket < 0){
port--;
continue;
}
u_long ul = 1;
ioctlsocket(scanSocket, FIONBIO, &ul);
connect(scanSocket, (SOCKADDR*)&scansock_in, sizeof(SOCKADDR));
/* set timeout */
fd_set rfd;
FD_ZERO(&rfd);
FD_SET(scanSocket, &rfd);
struct timeval Tout;
Tout.tv_sec = timeout;
Tout.tv_usec = miltimeout;
int ret = select(0, 0, &rfd, 0, &Tout);
if (ret > 0){
scanner->insertOpenPorts(port);
}
closesocket(scanSocket);
Sleep(100);
}
return 0;
}
69 changes: 69 additions & 0 deletions C++/port-scanner/scanner.h
@@ -0,0 +1,69 @@
/*
author: Forec
last edit date: 2016/09/25
email: forec@bupt.edu.cn
LICENSE
Copyright (c) 2015-2017, Forec <forec@bupt.edu.cn>
Permission to use, copy, modify, and/or distribute this code for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef __SCANNER_H_
#define __SCANNER_H_

#include <WINSOCK2.H>
#include <string>
#include <iostream>
#include <set>
#include <time.h>
#include <process.h>
#pragma comment(lib,"ws2_32.lib")

class Scanner{
private:
std::set<unsigned int> _openPorts;
std::string _IP;
bool _scaned;
unsigned int _timeout;
unsigned int _miltimeout;
unsigned short _core;
HANDLE hMutex;
time_t _lastScanTime;
public:
Scanner(std::string, unsigned int core=8000,
unsigned int timeout=2, unsigned int miltimeout = 0);
bool scan(unsigned int start = 0, unsigned int end = 65535);
bool is_scaned() { return _scaned; }
std::set<unsigned int> getOpenPorts(){ return _openPorts; }
unsigned int getTimeout(){ return _timeout; }
unsigned int getMilTimeout(){ return _miltimeout; }
time_t getLastTime() { return _lastScanTime; }
void insertOpenPorts(unsigned int);
void printOpenPorts();
};

struct scanParam{
Scanner *scanner;
std::string IP;
unsigned int start;
unsigned int end;
};

unsigned int __stdcall scanThread(void *);

template<typename T>
T minport(T a, T b){
return a < b ? a : b;
};

#endif

0 comments on commit 71b9b5b

Please sign in to comment.