├── .travis.yml ├── Dockerfile ├── LICENSE ├── README.md ├── data.json ├── header └── header.go ├── info └── info.go ├── netmask └── netmask.go ├── netscanner.go ├── netserv └── netserv.go ├── port └── port.go └── travis.sh /.travis.yml: -------------------------------------------------------------------------------- 1 | 2 | language: go 3 | sudo: true 4 | go: 5 | - 1.9.x 6 | 7 | git: 8 | depth: 3 9 | 10 | before_install: 11 | - sudo apt-get -qq update 12 | 13 | 14 | install: true 15 | 16 | 17 | go_import_path: github.com/R4yGM/netscanner 18 | 19 | script: 20 | - bash travis.sh 21 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.13 2 | 3 | RUN mkdir -p /app 4 | 5 | WORKDIR /app 6 | 7 | RUN apt-get update && apt-get install 8 | RUN go get github.com/R4yGM/netscanner 9 | 10 | RUN netscanner 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 R4yan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/R4yGM/netscanner.svg?branch=master)](https://travis-ci.org/R4yGM/netscanner) 2 | # netscanner 3 | netscanner - TCP/UDP scanner to find open or closed ports 4 | ## installation 5 | you have to run this command to install the program 6 | ```shell 7 | $ go get github.com/R4yGM/netscanner 8 | ``` 9 | 10 | ## Usage 11 | ### Commands : 12 | ```shell 13 | netscanner 0.1 : TCP/UDP port scanner 14 | 15 | Usage: netscanner 16 | 17 | Commands: 18 | 19 | sp,scanport - scan a singular port: usage sp,scanport (ex. scanport tcp 127.0.01 80) 20 | in,initscan - scan the first 1023 ports: usage in,initscan (ex. initscan udp 127.0.0.1 false) 21 | cs,completescan - scan all the ports: usage cs,completescan (ex. completescan tcp 127.0.0.1 true) 22 | sft,scanfromto - scan the ports from a port given to another port given: usage sft,scanfromto (ex. sft tcp 127.0.0.1 80 443 true) 23 | h,help - shows this text 24 | v,version - show the current version of the program 25 | info,information - shows some information about the program 26 | msp,maskscanport - scan a port of all the IPs in Subnet: usage msp,maskscanport (ex. msp tcp 127.0.0.1/24 true) 27 | 28 | ``` 29 | ### Parameters: 30 | 31 | ``` 32 | - protocol to use - can be tcp or udp 33 | - hostname of the target - ex. 127.0.0.1 34 | - port to scan 35 | - bool that saves the scan in a file - true / false 36 | - port to start scanning 37 | - last port to scan 38 | (for mask scan) - must contain /24 or /32 39 | ``` 40 | ### Example 41 | 42 | ``` 43 | $ netscanner sp tcp 127.0.0.1 80 44 | 45 | Port status service 46 | 47 | 80/tcp, Closed, http 48 | 49 | Scan started at : 2020-03-07 12:16:08.4845743 +0100 CET m=+0.002086201 50 | And finished at : 2020-03-07 12:16:08.4854596 +0100 CET m=+0.002971501 51 | 52 | ``` 53 | 54 | -------------------------------------------------------------------------------- /data.json: -------------------------------------------------------------------------------- 1 | { 2 | "version":"0.1", 3 | "owner":"R4yan", 4 | "github":"https://github.com/R4yGM/netscanner", 5 | "help":"\nnetscanner 0.1\n \nUsage: netscanner \n\nCommands:\n\n sp,scanport - scan a singular port: usage sp,scanport (ex. scanport tcp 127.0.01 80)\n in,initscan - scan the first 1023 ports: usage in,initscan (ex. initscan tcp 127.0.0.1)" 6 | } 7 | -------------------------------------------------------------------------------- /header/header.go: -------------------------------------------------------------------------------- 1 | package header 2 | 3 | import ( 4 | "math/rand" 5 | "time" 6 | ) 7 | var asciiArt1 = 8 | ` 9 | ============================================================================= 10 | _ 11 | | | _ 12 | | |__ ___| |_ ___ ___ __ _ _ __ _ __ ___ _ __ 13 | | '_ \ / _ \ __/ __|/ __/ _' | '_ \| '_ \ / _ \ '__| 14 | | | | | __/ |_\__ \ (_| (_| | | | | | | | __/ | 15 | |_| |_|\___|\__|___/\___\__,_|_| |_|_| |_|\___|_| 16 | ` 17 | var asciiArt2 = 18 | ` 19 | ============================================================================= 20 | 21 | __ __ ______ ______ ______ ______ ______ __ __ __ __ ______ ______ 22 | /\ "-.\ \ /\ ___\ /\__ _\ /\ ___\ /\ ___\ /\ __ \ /\ "-.\ \ /\ "-.\ \ /\ ___\ /\ == \ 23 | \ \ \-. \ \ \ __\ \/_/\ \/ \ \___ \ \ \ \____ \ \ __ \ \ \ \-. \ \ \ \-. \ \ \ __\ \ \ __< 24 | \ \_\\"\_\ \ \_____\ \ \_\ \/\_____\ \ \_____\ \ \_\ \_\ \ \_\\"\_\ \ \_\\"\_\ \ \_____\ \ \_\ \_\ 25 | \/_/ \/_/ \/_____/ \/_/ \/_____/ \/_____/ \/_/\/_/ \/_/ \/_/ \/_/ \/_/ \/_____/ \/_/ /_/ 26 | ` 27 | var asciiArt3 = 28 | ` 29 | ============================================================================= 30 | +-+-+-+-+-+-+-+-+-+-+ 31 | |n|e|t|s|c|a|n|n|e|r| 32 | +-+-+-+-+-+-+-+-+-+-+ 33 | ` 34 | var asciiArt4 = 35 | ` 36 | ============================================================================= 37 | _____ _____ ____ _____ _____ _____ _____ _____ _____ _____ 38 | / _ \/ __\/ \/ ___>/ \/ _ \/ _ \/ _ \/ __\/ _ \ 39 | | | || __|\- -/|___ || |--|| _ || | || | || __|| _ < 40 | \__|__/\_____/ |__| <_____/\_____/\__|__/\__|__/\__|__/\_____/\__|\_/ 41 | 42 | ` 43 | func random(min, max int) int { 44 | rand.Seed(time.Now().Unix()) 45 | return rand.Intn(max - min) + min 46 | } 47 | 48 | func AsciiTitle()(tex string){ 49 | num := random(1, 5) 50 | switch num { 51 | case 1: 52 | return asciiArt1 53 | case 2: 54 | return asciiArt2 55 | case 3: 56 | return asciiArt3 57 | case 4: 58 | return asciiArt4 59 | } 60 | return 61 | } 62 | -------------------------------------------------------------------------------- /info/info.go: -------------------------------------------------------------------------------- 1 | package info 2 | 3 | import ( 4 | // "io/ioutil" 5 | // "encoding/json" 6 | //"fmt" 7 | // "os" 8 | ) 9 | var ver = "0.1" 10 | var owner = "R4yan" 11 | var github = "https://github.com/R4yGM/netscanner" 12 | var help = "\nnetscanner 0.1 : TCP/UDP port scanner\n \nUsage: netscanner \n\nCommands:\n\n sp,scanport - scan a singular port: usage sp,scanport (ex. scanport tcp 127.0.01 80)\n in,initscan - scan the first 1023 ports: usage in,initscan (ex. initscan udp 127.0.0.1 false)\n cs,completescan - scan all the ports: usage cs,completescan (ex. completescan tcp 127.0.0.1 true)\n sft,scanfromto - scan the ports from a port given to another port given: usage sft,scanfromto (ex. sft tcp 127.0.0.1 80 443 true)\n h,help - shows this text\n v,version - show the current version of the program\n info,information - shows some information about the program\n msp,maskscanport - scan a port of all the IPs in Subnet: usage msp,maskscanport (ex. msp tcp 127.0.0.1/24 true)\n" 13 | type Info struct { 14 | Version string `json:"version"` 15 | Owner string `json:"owner"` 16 | Github string `json:"github"` 17 | Help string `json:"help"` 18 | } 19 | 20 | 21 | func Version()(tex string){ 22 | // file, _ := ioutil.ReadFile("info.json") 23 | 24 | /* data := Info{} 25 | 26 | _ = json.Unmarshal([]byte(file), &data)*/ 27 | //fmt.Println(ver) 28 | tex = ver 29 | return tex 30 | } 31 | func Owner()(tex string){ 32 | 33 | // file, _ := ioutil.ReadFile("info.json") 34 | 35 | /* data := Info{} 36 | 37 | _ = json.Unmarshal([]byte(file), &data)*/ 38 | // fmt.Println(owner) 39 | tex = owner 40 | return tex 41 | 42 | } 43 | func Github()(tex string){ 44 | 45 | // file, _ := ioutil.ReadFile("./info.json") 46 | 47 | /*data := Info{} 48 | 49 | _ = json.Unmarshal([]byte(file), &data) 50 | tex = data.Github*/ 51 | tex = github 52 | //fmt.Println(tex) 53 | return tex 54 | 55 | } 56 | func Helper()(tex string){ 57 | 58 | //jsonFile, err := os.Open("github.com/R4yGM/netscanner/data.json") 59 | /* 60 | if err != nil { 61 | fmt.Println(err) 62 | } 63 | 64 | defer jsonFile.Close() 65 | 66 | var in Info 67 | 68 | 69 | responseData, err := ioutil.ReadAll(jsonFile) 70 | json.Unmarshal(responseData, &in) 71 | fmt.Println(in.Help)*/ 72 | tex = help 73 | return 74 | 75 | } 76 | -------------------------------------------------------------------------------- /netmask/netmask.go: -------------------------------------------------------------------------------- 1 | package netmask 2 | 3 | import ( 4 | "net" 5 | ) 6 | 7 | func Hosts(cidr string) ([]string, error) { 8 | ip, ipnet, err := net.ParseCIDR(cidr) 9 | if err != nil { 10 | return nil, err 11 | } 12 | 13 | var ips []string 14 | for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) { 15 | ips = append(ips, ip.String()) 16 | } 17 | 18 | lenIPs := len(ips) 19 | switch { 20 | case lenIPs < 2: 21 | return ips, nil 22 | 23 | default: 24 | return ips[1 : len(ips)-1], nil 25 | } 26 | } 27 | 28 | func inc(ip net.IP) { 29 | for j := len(ip) - 1; j >= 0; j-- { 30 | ip[j]++ 31 | if ip[j] > 0 { 32 | break 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /netscanner.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "time" 7 | PortScan"github.com/R4yGM/netscanner/port" 8 | header"github.com/R4yGM/netscanner/header" 9 | Info"github.com/R4yGM/netscanner/info" 10 | "os" 11 | "strconv" 12 | ) 13 | 14 | func main() { 15 | 16 | if len(os.Args) == 1{ 17 | var help = Help() 18 | fmt.Println(help) 19 | return 20 | } 21 | 22 | arg := os.Args[1] 23 | 24 | if arg == "maskscanport" || arg == "msp" { 25 | if len(os.Args) <= 5 { 26 | fmt.Println("Missing parameters! \nUsage : maskscanport,msp (ex. msp tcp 127.0.0.1/24 80 true)\n-protocol - can be tcp or udp\n-hostname - hostname of the target - localhost/127.0.0.1\n-port- port to scan\n-savefile - bool that saves the scan - true / false \ntype help if you need help") 27 | return 28 | } 29 | var proto = os.Args[2] 30 | var hostname = os.Args[3] 31 | var port = os.Args[4] 32 | var booll = os.Args[5] 33 | if booll != "false" || booll != "true" { 34 | 35 | booll = "false" 36 | } 37 | booll1, err := strconv.ParseBool(booll) 38 | sp, err := strconv.Atoi(port) 39 | if err != nil { 40 | fmt.Println(err) 41 | } 42 | 43 | MaskScanPort(proto, hostname, sp, booll1) 44 | return 45 | } 46 | 47 | if arg == "completescan" || arg == "cs"{ 48 | if len(os.Args) <= 4{ 49 | fmt.Println("Missing parameters! \nUsage : cs,completescan (ex. initscan udp 127.0.0.1 true)\n-protocol - can be tcp or udp\n-hostname - hostname of the target - localhost/127.0.0.1\n-savefile - bool that saves the scan - true / false \ntype help if you need help") 50 | return 51 | } 52 | var proto = os.Args[2] 53 | var hostname = os.Args[3] 54 | var booll = os.Args[4] 55 | if booll != "false" || booll != "true"{ 56 | 57 | booll = "false" 58 | } 59 | booll1, err := strconv.ParseBool(booll) 60 | if err != nil{ 61 | fmt.Println(err) 62 | } 63 | //CompleteScan(proto, hostname, booll1) 64 | ScanFromTo(proto, hostname, 1, 65535, booll1) 65 | return 66 | } 67 | if arg == "help" || arg == "h"{ 68 | var help = Help() 69 | fmt.Println(help) 70 | return 71 | } 72 | 73 | if arg == "scanfromto" || arg == "sft"{ 74 | if len(os.Args) <= 6{ 75 | fmt.Println("\nMissing parameters! \nUsage : sft,scanfromto (ex. scanfromto tcp 127.0.0.1 20 100 false)\n-protocol - can be tcp or udp\n-hostname - hostname of the target - localhost/127.0.0.1\n-startport - first port to scan\n-endport - last port to scan\n-savefile - bool that saves the scan - true / false\ntype help if you need more help\n") 76 | return 77 | } 78 | var proto = os.Args[2] 79 | var hostname = os.Args[3] 80 | var startport = os.Args[4] 81 | var endport = os.Args[5] 82 | var booll = os.Args[6] 83 | booll1, err := strconv.ParseBool(booll) 84 | if err != nil{ 85 | fmt.Println(err) 86 | } 87 | sp, err := strconv.Atoi(startport) 88 | if err != nil { 89 | fmt.Println(err) 90 | } 91 | 92 | ep, err := strconv.Atoi(endport) 93 | if err != nil { 94 | fmt.Println(err) 95 | } 96 | ScanFromTo(proto,hostname,sp,ep,booll1) 97 | return 98 | } 99 | if arg == "information" || arg == "info"{ 100 | var info = Infor() 101 | fmt.Println(info) 102 | return 103 | } 104 | if arg == "version" || arg == "v"{ 105 | var ver = Version() 106 | fmt.Println(ver) 107 | return 108 | } 109 | 110 | 111 | if arg == "scanport" || arg == "sp"{ 112 | if len(os.Args) <= 4{ 113 | fmt.Println("Missing parameters! \nUsage : sp,scanport protocol hostname port (ex. scanport tcp 127.0.0.1 80)\n-protocol - can be tcp or udp\n-hostname - hostname of the target - localhost/127.0.0.1 -port - port to scan\ntype help if you need help") 114 | return 115 | } 116 | 117 | var proto = os.Args[2] 118 | var hostname = os.Args[3] 119 | var port = os.Args[4] 120 | porto, err := strconv.Atoi(port) 121 | if err != nil{ 122 | fmt.Println(err) 123 | } 124 | ScanPort(proto, hostname, porto) 125 | return 126 | } 127 | 128 | if arg == "initscan" || arg == "in"{ 129 | if len(os.Args) <= 4{ 130 | fmt.Println("Missing parameters! \nUsage : in,initscan (ex. initscan udp 127.0.0.1 false)\n-protocol - can be tcp or udp\n-hostname - hostname of the target - localhost/127.0.0.1\n-savefile - bool that saves the scan\ntype help if you need help") 131 | return 132 | } 133 | var proto = os.Args[2] 134 | var hostname = os.Args[3] 135 | var booll = os.Args[4] 136 | 137 | booll1, err := strconv.ParseBool(booll) 138 | if err != nil{ 139 | fmt.Println(err) 140 | } 141 | 142 | //InitialScan(proto, hostname, booll1) 143 | ScanFromTo(proto, hostname, 1, 1024, booll1) 144 | return 145 | } 146 | if arg == "help" || arg == "h"{ 147 | var help = Help() 148 | fmt.Println(help) 149 | return 150 | } 151 | fmt.Println("Unkown command\n") 152 | var help = Help() 153 | fmt.Println(help) 154 | return 155 | } 156 | func ScanPort(protocol string, hostname string, port int)(texe string){ 157 | 158 | var tex = header.AsciiTitle() 159 | fmt.Println(tex) 160 | dtStart := time.Now() 161 | fmt.Println("Port Scanning\n") 162 | fmt.Println(" Port status service\n") 163 | results := PortScan.ScanPort(protocol,hostname,port) 164 | var replacer = strings.NewReplacer("{", " ", "}", "") 165 | 166 | 167 | var str = replacer.Replace(fmt.Sprint(results)) 168 | fmt.Println(strings.Trim(fmt.Sprint(str), "[]")) 169 | dtEnd := time.Now() 170 | fmt.Println("Scan started at :", dtStart.String()) 171 | fmt.Println("And finished at :", dtEnd.String()) 172 | fmt.Println("\n") 173 | return str 174 | } 175 | func InitialScan(protocol string, hostname string, savefile bool)(texe string){ 176 | 177 | var tex = header.AsciiTitle() 178 | fmt.Println(tex) 179 | dtStart := time.Now() 180 | fmt.Println("Port Scanning\n") 181 | fmt.Println(" Port status service\n") 182 | results := PortScan.InitialScan(protocol,hostname) 183 | var replacer = strings.NewReplacer("{", " ", "}", "") 184 | 185 | 186 | var str = replacer.Replace(fmt.Sprint(results)) 187 | if savefile == true{ 188 | f, err := os.Create("CompleteScan"+dtStart.String()+ ".txt") 189 | if err != nil { 190 | fmt.Println(err) 191 | return 192 | } 193 | 194 | 195 | var str1 = replacer.Replace(fmt.Sprint(results)) 196 | f.WriteString(strings.Trim(fmt.Sprint(str1), "[]")) 197 | 198 | f.Close() 199 | } 200 | fmt.Println(strings.Trim(fmt.Sprint(str), "[]")) 201 | dtEnd := time.Now() 202 | fmt.Println("Scan started at :", dtStart.String()) 203 | fmt.Println("And finished at :", dtEnd.String()) 204 | if savefile == true{ 205 | fmt.Println("Scan saved in CompleteScan"+dtStart.String()+ ".txt") 206 | } 207 | fmt.Println("\n") 208 | return str 209 | } 210 | func CompleteScan(protocol string, hostname string, savefile bool)(texe string){ 211 | 212 | var tex = header.AsciiTitle() 213 | fmt.Println(tex) 214 | dtStart := time.Now() 215 | fmt.Println("Port Scanning\n") 216 | fmt.Println(" Port status service\n") 217 | results := PortScan.CompleteScan(protocol,hostname) 218 | var replacer = strings.NewReplacer("{", " ", "}", "") 219 | var str = replacer.Replace(fmt.Sprint(results)) 220 | if savefile == true{ 221 | f, err := os.Create("CompleteScan"+dtStart.String()+ ".txt") 222 | if err != nil { 223 | fmt.Println(err) 224 | return 225 | } 226 | 227 | 228 | var str1 = replacer.Replace(fmt.Sprint(results)) 229 | f.WriteString(strings.Trim(fmt.Sprint(str1), "[]")) 230 | 231 | f.Close() 232 | } 233 | 234 | fmt.Println(strings.Trim(fmt.Sprint(str), "[]")) 235 | dtEnd := time.Now() 236 | fmt.Println("Scan started at :", dtStart.String()) 237 | fmt.Println("And finished at :", dtEnd.String()) 238 | if savefile == true{ 239 | 240 | fmt.Println("Scan saved in CompleteScan"+dtStart.String()+ ".txt") 241 | } 242 | 243 | 244 | fmt.Println("\n") 245 | 246 | return str 247 | } 248 | func ScanFromTo(hostname string, protocol string, startport int, endport int,savefile bool)(texe string){ 249 | 250 | if startport > endport { 251 | var initP = startport 252 | startport = endport 253 | endport = initP 254 | } 255 | 256 | var tex = header.AsciiTitle() 257 | fmt.Println(tex) 258 | dtStart := time.Now() 259 | fmt.Println("Port Scanning\n") 260 | fmt.Println(" Port status service\n") 261 | results := PortScan.ScanFromTo(protocol,hostname,startport,endport) 262 | var replacer = strings.NewReplacer("{", " ", "}", "") 263 | if savefile == true{ 264 | f, err := os.Create("CompleteScan"+dtStart.String()+ ".txt") 265 | if err != nil { 266 | fmt.Println(err) 267 | return 268 | } 269 | 270 | 271 | var str1 = replacer.Replace(fmt.Sprint(results)) 272 | f.WriteString(strings.Trim(fmt.Sprint(str1), "[]")) 273 | 274 | f.Close() 275 | } 276 | 277 | 278 | var str = replacer.Replace(fmt.Sprint(results)) 279 | fmt.Println(strings.Trim(fmt.Sprint(str), "[]")) 280 | dtEnd := time.Now() 281 | fmt.Println("Scan started at :", dtStart.String()) 282 | fmt.Println("And finished at :", dtEnd.String()) 283 | if savefile == true{ 284 | 285 | fmt.Println("Scan saved in CompleteScan"+dtStart.String()+ ".txt") 286 | } 287 | fmt.Println("\n") 288 | return str 289 | 290 | } 291 | 292 | func Infor()(tex string){ 293 | var ver =Info.Version() 294 | var own = Info.Owner() 295 | var gith = Info.Github() 296 | tex = "\nversion : "+string(ver)+"\nmade by :"+own+"\nGithub Repository : "+gith+"\n" 297 | return 298 | } 299 | func Version()(tex string){ 300 | var ver =Info.Version() 301 | tex = ver 302 | return 303 | } 304 | 305 | func Help()(help string) { return Info.Helper() } 306 | 307 | func MaskScanPort(protocol string, hostname string, port int, savefile bool) (texe string){ 308 | var tex = header.AsciiTitle() 309 | fmt.Println(tex) 310 | dtStart := time.Now() 311 | fmt.Println("Port Scanning\n") 312 | fmt.Println(" Port status service\n") 313 | results := PortScan.MaskScanPort(protocol, hostname, port) 314 | var replacer = strings.NewReplacer("{", " ", "}", "") 315 | 316 | var str = replacer.Replace(fmt.Sprint(results)) 317 | if savefile == true { 318 | f, err := os.Create("MaskScanPort" + dtStart.String() + ".txt") 319 | if err != nil { 320 | fmt.Println(err) 321 | return 322 | } 323 | 324 | var str1 = replacer.Replace(fmt.Sprint(results)) 325 | f.WriteString(strings.Trim(fmt.Sprint(str1), "[]")) 326 | 327 | f.Close() 328 | } 329 | fmt.Println(strings.Trim(fmt.Sprint(str), "[]")) 330 | dtEnd := time.Now() 331 | fmt.Println("Scan started at :", dtStart.String()) 332 | fmt.Println("And finished at :", dtEnd.String()) 333 | if savefile == true { 334 | fmt.Println("Scan saved in MaskScanPort" + dtStart.String() + ".txt") 335 | } 336 | fmt.Println("\n") 337 | return str 338 | } 339 | -------------------------------------------------------------------------------- /netserv/netserv.go: -------------------------------------------------------------------------------- 1 | package netserv 2 | 3 | import ( 4 | "io/ioutil" 5 | "strconv" 6 | "strings" 7 | ) 8 | 9 | type Protoent struct { 10 | Name string 11 | Aliases []string 12 | Number int 13 | } 14 | 15 | type Servent struct { 16 | Name string 17 | Aliases []string 18 | Port int 19 | Protocol *Protoent 20 | } 21 | 22 | var ( 23 | Protocols []*Protoent 24 | Services []*Servent 25 | ) 26 | 27 | func init() { 28 | protoMap := make(map[string]*Protoent) 29 | 30 | data, err := ioutil.ReadFile("/etc/protocols") 31 | if err != nil { 32 | panic(err) 33 | } 34 | 35 | for _, line := range strings.Split(string(data), "\n") { 36 | line = strings.TrimSpace(line) 37 | split := strings.SplitN(line, "#", 2) 38 | fields := strings.Fields(split[0]) 39 | if len(fields) < 2 { 40 | continue 41 | } 42 | 43 | num, err := strconv.ParseInt(fields[1], 10, 32) 44 | if err != nil { 45 | panic(err) 46 | } 47 | 48 | protoent := &Protoent{ 49 | Name: fields[0], 50 | Aliases: fields[2:], 51 | Number: int(num), 52 | } 53 | Protocols = append(Protocols, protoent) 54 | 55 | protoMap[fields[0]] = protoent 56 | } 57 | 58 | data, err = ioutil.ReadFile("/etc/services") 59 | if err != nil { 60 | panic(err) 61 | } 62 | 63 | for _, line := range strings.Split(string(data), "\n") { 64 | line = strings.TrimSpace(line) 65 | split := strings.SplitN(line, "#", 2) 66 | fields := strings.Fields(split[0]) 67 | if len(fields) < 2 { 68 | continue 69 | } 70 | 71 | name := fields[0] 72 | portproto := strings.SplitN(fields[1], "/", 2) 73 | port, err := strconv.ParseInt(portproto[0], 10, 32) 74 | if err != nil { 75 | panic(err) 76 | } 77 | 78 | proto := portproto[1] 79 | aliases := fields[2:] 80 | 81 | Services = append(Services, &Servent{ 82 | Name: name, 83 | Aliases: aliases, 84 | Port: int(port), 85 | Protocol: protoMap[proto], 86 | }) 87 | } 88 | } 89 | func (this *Protoent) Equal(other *Protoent) bool { 90 | if this == nil && other == nil { 91 | return true 92 | } 93 | 94 | if this == nil || other == nil { 95 | return false 96 | } 97 | 98 | return this.Number == other.Number 99 | } 100 | 101 | func GetProtoByNumber(num int) (protoent *Protoent) { 102 | for _, protoent := range Protocols { 103 | if protoent.Number == num { 104 | return protoent 105 | } 106 | } 107 | return nil 108 | } 109 | 110 | func GetProtoByName(name string) (protoent *Protoent) { 111 | for _, protoent := range Protocols { 112 | if protoent.Name == name { 113 | return protoent 114 | } 115 | 116 | for _, alias := range protoent.Aliases { 117 | if alias == name { 118 | return protoent 119 | } 120 | } 121 | } 122 | 123 | return nil 124 | } 125 | 126 | func GetServByName(name string, protocol *Protoent) (servent *Servent) { 127 | for _, servent := range Services { 128 | if !servent.Protocol.Equal(protocol) { 129 | continue 130 | } 131 | 132 | if servent.Name == name { 133 | return servent 134 | } 135 | 136 | for _, alias := range servent.Aliases { 137 | if alias == name { 138 | return servent 139 | } 140 | } 141 | } 142 | 143 | return nil 144 | } 145 | 146 | func GetServByPort(port int, protocol *Protoent) *Servent { 147 | for _, servent := range Services { 148 | if servent.Port == port && servent.Protocol.Equal(protocol) { 149 | return servent 150 | } 151 | } 152 | 153 | return nil 154 | } 155 | -------------------------------------------------------------------------------- /port/port.go: -------------------------------------------------------------------------------- 1 | 2 | package port 3 | 4 | import ( 5 | "net" 6 | "strconv" 7 | "time" 8 | "log" 9 | netserv"github.com/R4yGM/netscanner/netserv" 10 | netmask"github.com/R4yGM/netscanner/netmask" 11 | ) 12 | 13 | type ScanResult struct { 14 | Port string 15 | State string 16 | Service string 17 | } 18 | 19 | func ScanPort(protocol, hostname string, port int) ScanResult { 20 | 21 | var proto *netserv.Protoent 22 | var serv *netserv.Servent 23 | result := ScanResult{Port: strconv.Itoa(port) + string("/") + protocol+", "} 24 | address := hostname + ":" + strconv.Itoa(port) 25 | conn, err := net.DialTimeout(protocol, address, 60*time.Second) 26 | 27 | if err != nil { 28 | result.State = "Closed, " 29 | }else { 30 | defer conn.Close() 31 | result.State = "Open, " 32 | } 33 | proto = netserv.GetProtoByName(protocol) 34 | serv = netserv.GetServByPort(port, proto) 35 | 36 | if serv != nil { 37 | result.Service = serv.Name + "\n" 38 | } 39 | if result.Service == ""{ 40 | result.Service = "\n" 41 | } 42 | 43 | return result 44 | } 45 | func ScanFromTo(hostname string, protocol string, startport int, endport int) []ScanResult { 46 | 47 | var results []ScanResult 48 | for i := startport; i <= endport; i++ { 49 | results = append(results, ScanPort(protocol, hostname, i)) 50 | } 51 | 52 | 53 | return results 54 | 55 | } 56 | func InitialScan(hostname string, protocol string) []ScanResult { 57 | 58 | var results []ScanResult 59 | 60 | for i := 0; i <= 1024; i++ { 61 | results = append(results, ScanPort(protocol, hostname, i)) 62 | } 63 | return results 64 | } 65 | 66 | func CompleteScan(hostname string, protocol string) []ScanResult { 67 | 68 | var results []ScanResult 69 | 70 | for i := 0; i <= 65535; i++ { 71 | results = append(results, ScanPort(protocol, hostname, i)) 72 | } 73 | 74 | 75 | return results 76 | } 77 | 78 | func MaskPortR(protocol, hostname string, port int) ScanResult { 79 | 80 | var proto *netserv.Protoent 81 | var serv *netserv.Servent 82 | result := ScanResult{Port: hostname+":"+strconv.Itoa(port) + string("/") + protocol+", "} 83 | address := hostname + ":" + strconv.Itoa(port) 84 | conn, err := net.DialTimeout(protocol, address, 60*time.Second) 85 | 86 | if err != nil { 87 | result.State = "Closed, " 88 | }else { 89 | defer conn.Close() 90 | result.State = "Open, " 91 | } 92 | proto = netserv.GetProtoByName(protocol) 93 | serv = netserv.GetServByPort(port, proto) 94 | 95 | if serv != nil { 96 | result.Service = serv.Name + "\n" 97 | } 98 | if result.Service == ""{ 99 | result.Service = "\n" 100 | } 101 | 102 | return result 103 | } 104 | func MaskScanPort(protocol, hostname string, port int) []ScanResult { 105 | 106 | var results []ScanResult 107 | tmp, err := netmask.Hosts(hostname) 108 | if(err != nil){ 109 | log.Fatal(err) 110 | } 111 | for i := 0; i < len(tmp);i++{ 112 | 113 | results = append(results, MaskPortR(protocol, tmp[i], port)) 114 | } 115 | return results 116 | } 117 | -------------------------------------------------------------------------------- /travis.sh: -------------------------------------------------------------------------------- 1 | go build ./ 2 | sudo cp ./netscanner /usr/local/bin 3 | netscanner 4 | --------------------------------------------------------------------------------