Skip to content

Commit

Permalink
添加目标支持网段识别、自动计算子网等5种类型。
Browse files Browse the repository at this point in the history
Signed-off-by: AlphabugX <nmtech@aliyun.com>
  • Loading branch information
AlphabugX committed Apr 24, 2022
1 parent 963f5e5 commit c450dea
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 45 deletions.
10 changes: 5 additions & 5 deletions data/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package data
import (
"fmt"
"os"
"strings"
)

var (
Result = make(chan string, 65535)
Result = make(chan string, 1234567)
)

func Save(filename string, format string, Target string) {
func Save(filename string, format string) {
fl, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return
Expand All @@ -20,14 +21,13 @@ func Save(filename string, format string, Target string) {
data_format = "{\"%s\":\"%s\"}\n"
case "text":
data_format = "%s:%s\n"

}
defer fl.Close()
for {
if len(Result) == 0 {
break
}
fl.Write([]byte(fmt.Sprintf(data_format, Target, <-Result)))
tmp := strings.Split(<-Result, ":")
fl.Write([]byte(fmt.Sprintf(data_format, tmp[0], tmp[1])))
}
//fl.Write([]byte(""))
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ module AlphaNet

go 1.16

require github.com/bwmarrin/snowflake v0.3.0 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
33 changes: 13 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ import (
"AlphaNet/data"
"AlphaNet/network"
"flag"
"fmt"
"log"
"os"
"strconv"
"time"
)

var (
Target = flag.String("h", "f5.ink", "127.0.0.1 or f5.ink")
Thread = flag.Int("t", 14000, "Maximum threads")
Timeout = flag.Int("time", 3, "timeout:3 seconds")
Target = flag.String("h", "f5.ink", "Target:f5.ink|114.67.111.74|114.67.111.74/28|114.67.111.74-80|114.67.111.74-114.67.111.80|114.67.111.*")
Thread = flag.Int("t", 10000, "Maximum threads")
Timeout = flag.Int("time", 2, "timeout:3 seconds")
Outfile = flag.String("out", "", "result.txt")
Format = flag.String("format", "text", "Result format: text=>ip:port,json=>{\"ip\":\"port\"}")
MaxCheck = flag.Int("check", 1, "MaxCheck:Connect check the maximum number")
//Port_list = flag.Int("p", 0, "Port:80|80,443|1-1024")
)

func main() {
//network.MaxThread = *Thread
//network.Timeout = *Timeout
//network.MaxCheck = *MaxCheck
//AScanPort()
//os.Exit(1)
flag.Parse()
if len(os.Args) <= 1 {
flag.Usage()
Expand All @@ -30,21 +32,12 @@ func main() {
network.Timeout = *Timeout
network.MaxCheck = *MaxCheck
config.Init()
AScan()
AScanPort()
}
}
func AScan() {
fmt.Print("AScanPort (Version:1.0.1)\n")
start := time.Now()
for i := 1; i < 65536; i++ {
network.Pool.Add(1)
go network.ScanPort(*Target, strconv.Itoa(i))
}
network.Pool.Wait()

log.Println("over", time.Since(start))
log.Println("Open Ports:", network.Port_count)
func AScanPort() {
network.Go(*Target)
if *Outfile != "" {
data.Save(*Outfile, *Format, *Target)
data.Save(*Outfile, *Format)
}
}
151 changes: 151 additions & 0 deletions network/ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package network

import (
"bytes"
"log"
"math"
"net"
"os"
"regexp"
"strconv"
"strings"
)

var (
// IP x.x.x.x
IPLIST_TYPE_1 = regexp.MustCompile(`^(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$`)

// IP Range x.x.x.x-255
IPLIST_TYPE_2 = regexp.MustCompile(`^(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])-([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$`)

// IP Range x.x.x.x-x.x.x.x
IPLIST_TYPE_3 = regexp.MustCompile(`^(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])-(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])$`)

// IP Range x.x.x.x/x
IPLIST_TYPE_4 = regexp.MustCompile(`^(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])/([1-9]|[1-2][0-9]|3[0-2])$`)

// IP Range x.x.x.*
IPLIST_TYPE_5 = regexp.MustCompile(`^(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)(([1-9]|[1-9][0-9]|1[0-9][0-9]|1[0-4][0-9]|25[0-5])\.)\*$`)
)

func ParseList(IPLIST string) interface{} {

switch {
case IPLIST_TYPE_1.MatchString(IPLIST):
//log.Println("IPLIST_TYPE_1")
return IPLIST
case IPLIST_TYPE_2.MatchString(IPLIST):
//log.Println("IPLIST_TYPE_2")
var IPLIST_RANGE []string
IPLIST_RANGE_Regexp := regexp.MustCompile(`([\d]+\.[\d]+\.[\d]+\.)([\d]+)-([\d]+)`)
IPLIST_RANGEs := IPLIST_RANGE_Regexp.FindStringSubmatch(IPLIST)
if len(IPLIST_RANGEs) < 4 {
return nil
}
a, _ := strconv.Atoi(IPLIST_RANGEs[2])
b, _ := strconv.Atoi(IPLIST_RANGEs[3])
if a > b {
return nil
} else if b > 255 {
b = 254
}
if len(IPLIST_RANGEs) == 4 {
for i := a; i <= b; i++ {
IPLIST_RANGE = append(IPLIST_RANGE, IPLIST_RANGEs[1]+strconv.Itoa(i))
}
}
return IPLIST_RANGE
case IPLIST_TYPE_3.MatchString(IPLIST):
//log.Println("IPLIST_TYPE_3")
var IPLIST_RANGE []string
IPLIST_A := IP2Int(strings.Split(IPLIST, "-")[0])
IPLIST_B := IP2Int(strings.Split(IPLIST, "-")[1])
for i := IPLIST_A; i < IPLIST_B+1; i++ {
IPLIST_RANGE = append(IPLIST_RANGE, Int2IP(i))
}
return IPLIST_RANGE
case IPLIST_TYPE_4.MatchString(IPLIST):
//log.Println("IPLIST_TYPE_4")
var IPLIST_RANGE []string
IPLIST_RANGE_Regexp := regexp.MustCompile(`([\d]+\.[\d]+\.[\d]+\.)([\d]+)/([\d]+)`)
IPLIST_RANGEs := IPLIST_RANGE_Regexp.FindStringSubmatch(IPLIST)
if len(IPLIST_RANGEs) < 4 {
return nil
}
n, _ := strconv.Atoi(IPLIST_RANGEs[3])
IP := IPLIST_RANGEs[1] + IPLIST_RANGEs[2]
CIDR_n := 32 - n
Subnet_length := int(math.Pow(2, float64(CIDR_n)))
CIDR_count := (IP2Int(IP) - IP2Int(IPLIST_RANGEs[1]+"0")) / Subnet_length
log.Println("CIDR_count", CIDR_count)

IPLIST_A2int := int64(IP2Int(IPLIST_RANGEs[1] + "0"))
IPLIST_A2bin := strconv.FormatInt(IPLIST_A2int, 2)[:len(strconv.FormatInt(IPLIST_A2int, 2))-CIDR_n] + strings.Repeat("0", CIDR_n)
IPLIST_A, _ := strconv.ParseInt(IPLIST_A2bin, 2, 64)
IPLIST_A = IPLIST_A + int64(CIDR_count*Subnet_length)
IPLIST_B := int(IPLIST_A) + Subnet_length - 1
for i := int(IPLIST_A + 1); i < IPLIST_B; i++ {
IPLIST_RANGE = append(IPLIST_RANGE, Int2IP(i))
}
return IPLIST_RANGE
case IPLIST_TYPE_5.MatchString(IPLIST):
//log.Println("IPLIST_TYPE_5")
var IPLIST_RANGE []string
IPLIST_RANGE_Regexp := regexp.MustCompile(`([\d]+\.[\d]+\.[\d]+\.)\*`)
IPLIST_RANGEs := IPLIST_RANGE_Regexp.FindStringSubmatch(IPLIST)
if len(IPLIST_RANGEs) < 2 {
return nil
}
for i := 0; i < 256; i++ {
IPLIST_RANGE = append(IPLIST_RANGE, IPLIST_RANGEs[1]+strconv.Itoa(i))
}
return IPLIST_RANGE
default:
addr, err := net.ResolveIPAddr("ip", IPLIST)
if err != nil {
log.Println("Resolvtion error", err.Error())
os.Exit(1)
}
return addr.String()
}
return nil
}
func IP2Int(ip string) int {
ipSegs := strings.Split(ip, ".")
var ipInt int = 0
var pos uint = 24
for _, ipSeg := range ipSegs {
tempInt, _ := strconv.Atoi(ipSeg)
tempInt = tempInt << pos
ipInt = ipInt | tempInt
pos -= 8
}
return ipInt
}
func Int2IP(ipInt int) string {
ipSegs := make([]string, 4)
var ipSegs_len = len(ipSegs)
buffer := bytes.NewBufferString("")
for i := 0; i < ipSegs_len; i++ {
tempInt := ipInt & 0xFF
ipSegs[ipSegs_len-i-1] = strconv.Itoa(tempInt)
ipInt = ipInt >> 8
}
for i := 0; i < ipSegs_len; i++ {
buffer.WriteString(ipSegs[i])
if i < ipSegs_len-1 {
buffer.WriteString(".")
}
}
return buffer.String()
}
func IPLIST(ip string) []string {
IP_check := ParseList(ip)
switch IP_check.(type) {
case string:
return []string{IP_check.(string)}
case []string:
return IP_check.([]string)
}
return nil
}
49 changes: 32 additions & 17 deletions network/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,55 @@ package network

import (
"AlphaNet/data"
"encoding/json"
"fmt"
"log"
"net"
"strconv"
"sync"
"time"
)

var (
MaxThread = 14000
Timeout = 3
MaxCheck = 3
Scan_Pool_Max = make(chan bool, MaxThread)
neta = net.Dialer{Timeout: time.Duration(Timeout) * time.Second}
Pool sync.WaitGroup
Port_count = 0
MaxThread = 10000
Timeout = 3
MaxCheck = 3
Scan_Port_Pool_Max = make(chan bool, MaxThread)
neta = net.Dialer{Timeout: time.Duration(Timeout) * time.Second}
Pool sync.WaitGroup
Port_count = 0
)

func ScanPort(ip string, port string) {
defer Pool.Done()
Scan_Pool_Max <- true
Scan_Port_Pool_Max <- true
//client, err := neta.Dial("tcp", ip+":"+port)
for check := 0; check < MaxCheck; check++ {
_, err := neta.Dial("tcp", ip+":"+port)
if err == nil {
Port_count += 1
data.Result <- port
reslut := map[string]interface{}{
"ip": ip,
"port": port,
}
data, _ := json.Marshal(reslut)
log.Println(string(data))
data.Result <- ip + ":" + port
log.Printf("{\"%s\":\"%s\"}", ip, port)
break
}
}
<-Scan_Pool_Max
<-Scan_Port_Pool_Max
}
func HOSTScan(IP string) {
defer Pool.Done()
for i := 1; i < 65536; i++ {
Pool.Add(1)
go ScanPort(IP, strconv.Itoa(i))
}
}
func Go(Target string) {
fmt.Print("AScanPort (Version:1.0.1)\n")
IPLIST := IPLIST(Target)
start := time.Now()
for _, ip := range IPLIST {
Pool.Add(1)
go HOSTScan(ip)
}
Pool.Wait()
log.Println("over", time.Since(start))
log.Println("Open Ports:", Port_count)
}

0 comments on commit c450dea

Please sign in to comment.