mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 14:06:44 +08:00
优化icmp模块
This commit is contained in:
parent
818102a814
commit
b4fb1efb3a
@ -2,7 +2,6 @@ package Plugins
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
@ -14,20 +13,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var icmp ICMP
|
|
||||||
|
|
||||||
var AliveHosts []string
|
var AliveHosts []string
|
||||||
|
|
||||||
var SysInfo = GetSys()
|
var SysInfo = GetSys()
|
||||||
|
|
||||||
type ICMP struct {
|
|
||||||
Type uint8
|
|
||||||
Code uint8
|
|
||||||
Checksum uint16
|
|
||||||
Identifier uint16
|
|
||||||
SequenceNum uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
type SystemInfo struct {
|
type SystemInfo struct {
|
||||||
OS string
|
OS string
|
||||||
ARCH string
|
ARCH string
|
||||||
@ -58,39 +47,31 @@ func GetSys() SystemInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isping(ip string) bool {
|
func isping(ip string) bool {
|
||||||
icmp.Type = 8
|
IcmpByte := []byte{8, 0, 247, 255, 0, 0, 0, 0}
|
||||||
icmp.Code = 0
|
|
||||||
icmp.Checksum = 0
|
|
||||||
icmp.Identifier = 0
|
|
||||||
icmp.SequenceNum = 0
|
|
||||||
|
|
||||||
recvBuf := make([]byte, 32)
|
|
||||||
var buffer bytes.Buffer
|
|
||||||
|
|
||||||
binary.Write(&buffer, binary.BigEndian, icmp)
|
|
||||||
icmp.Checksum = CheckSum(buffer.Bytes())
|
|
||||||
|
|
||||||
buffer.Reset()
|
|
||||||
binary.Write(&buffer, binary.BigEndian, icmp)
|
|
||||||
|
|
||||||
Time, _ := time.ParseDuration("3s")
|
Time, _ := time.ParseDuration("3s")
|
||||||
conn, err := net.DialTimeout("ip4:icmp", ip, Time)
|
conn, err := net.DialTimeout("ip4:icmp", ip, Time)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
_, err = conn.Write(buffer.Bytes())
|
_, err = conn.Write(IcmpByte)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
conn.SetReadDeadline(time.Now().Add(time.Second * 3))
|
|
||||||
|
if err := conn.SetReadDeadline(time.Now().Add(time.Second * 3)); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
recvBuf := make([]byte, 32)
|
||||||
num, err := conn.Read(recvBuf)
|
num, err := conn.Read(recvBuf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.SetReadDeadline(time.Time{})
|
if err := conn.SetReadDeadline(time.Time{}); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
if string(recvBuf[0:num]) != "" {
|
if string(recvBuf[0:num]) != "" {
|
||||||
fmt.Printf("(ICMP) Target '%s' is alive\n", ip)
|
fmt.Printf("(ICMP) Target '%s' is alive\n", ip)
|
||||||
return true
|
return true
|
||||||
@ -99,32 +80,13 @@ func isping(ip string) bool {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckSum(data []byte) uint16 {
|
|
||||||
var (
|
|
||||||
sum uint32
|
|
||||||
length int = len(data)
|
|
||||||
index int
|
|
||||||
)
|
|
||||||
for length > 1 {
|
|
||||||
sum += uint32(data[index])<<8 + uint32(data[index+1])
|
|
||||||
index += 2
|
|
||||||
length -= 2
|
|
||||||
}
|
|
||||||
if length > 0 {
|
|
||||||
sum += uint32(data[index])
|
|
||||||
}
|
|
||||||
sum += (sum >> 16)
|
|
||||||
|
|
||||||
return uint16(^sum)
|
|
||||||
}
|
|
||||||
|
|
||||||
func IcmpCheck(hostslist []string, IcmpThreads int) {
|
func IcmpCheck(hostslist []string, IcmpThreads int) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
mutex := &sync.Mutex{}
|
mutex := &sync.Mutex{}
|
||||||
limiter := make(chan int, IcmpThreads)
|
limiter := make(chan struct{}, IcmpThreads)
|
||||||
for _, host := range hostslist {
|
for _, host := range hostslist {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
limiter <- 1
|
limiter <- struct{}{}
|
||||||
go func(host string) {
|
go func(host string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if isping(host) {
|
if isping(host) {
|
||||||
|
@ -53,7 +53,7 @@ var PORTList_bak = map[string]int{
|
|||||||
var Outputfile = "result.txt"
|
var Outputfile = "result.txt"
|
||||||
var IsSave = true
|
var IsSave = true
|
||||||
|
|
||||||
var DefaultPorts = "21,22,23,80,135,443,445,1433,1521,3306,5432,6379,7001,8080,8089,9000,9200,11211,27017"
|
var DefaultPorts = "21,22,80,81,135,443,445,1433,1521,3306,5432,6379,7001,8000,8080,8089,11211,27017"
|
||||||
|
|
||||||
type HostInfo struct {
|
type HostInfo struct {
|
||||||
Host string
|
Host string
|
||||||
|
Loading…
Reference in New Issue
Block a user