Last active
April 21, 2017 09:59
-
-
Save RickGray/3076478f3b689df195c8023e66f1c9a0 to your computer and use it in GitHub Desktop.
go tutorial "zgrab" simple code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
//"crypto/tls" | |
"encoding/json" | |
"flag" | |
"fmt" | |
"io" | |
"net" | |
"os" | |
"strings" | |
"sync" | |
"time" | |
) | |
var threads int | |
var infile string | |
var timeout int | |
var ignore bool | |
type GrabData struct { | |
IP string `json:"ip,omitempty"` | |
Port string `json:"port,omitempty"` | |
Banner string `json:"banner,omitempty"` | |
Tls bool `json:"tls,omitempty"` | |
} | |
type GrabWorker struct { | |
in chan string | |
out chan GrabData | |
} | |
//func grabBannerTLS(address string) string { | |
// timeout := time.Duration(timeout) * time.Second | |
// dialer := net.Dialer{Timeout: timeout} | |
// conf := tls.Config{ | |
// InsecureSkipVerify: true, | |
// } | |
// conn, err := tls.DialWithDialer(&dialer, "tcp", address, &conf) | |
// if err != nil { | |
// return "" | |
// } | |
// defer conn.Close() | |
// conn.SetWriteDeadline(time.Now().Add(time.Duration(timeout) * time.Second)) | |
// _, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: " + address + "\r\n\r\n")) | |
// if err != nil { | |
// return "" | |
// } | |
// conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second)) | |
// buff := make([]byte, 65535) | |
// n, err := conn.Read(buff) | |
// if err != nil { | |
// return "" | |
// } | |
// banner := string(buff[:n]) | |
// return banner | |
//} | |
func (gw *GrabWorker) IOLoop(wg *sync.WaitGroup) { | |
go func() { | |
for { | |
data, ok := <-gw.in | |
if !ok { | |
break | |
} | |
address := data | |
conn, err := net.DialTimeout("tcp", address, time.Duration(timeout)*time.Second) | |
if err != nil { | |
continue | |
} | |
conn.SetWriteDeadline(time.Now().Add(time.Duration(timeout) * time.Second)) | |
_, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: " + address + "\r\n\r\n")) | |
if err != nil { | |
continue | |
} | |
conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second)) | |
buff := make([]byte, 65535) | |
n, err := conn.Read(buff) | |
if err != nil { | |
continue | |
} | |
banner := string(buff[:n]) | |
conn.Close() | |
s := strings.Split(address, ":") | |
ip, port := s[0], s[1] | |
use_tls := false | |
//if banner == "" { | |
// banner = grabBannerTLS(address) | |
// use_tls = true | |
//} | |
gw.out <- GrabData{ip, port, banner, use_tls} | |
} | |
wg.Done() | |
}() | |
} | |
func main() { | |
flag.IntVar(&threads, "threads", 100, "threads number to use") | |
flag.StringVar(&infile, "infile", "", "input to process") | |
flag.IntVar(&timeout, "timeout", 5, "timeout to connect, write and read") | |
flag.BoolVar(&ignore, "ignore", false, "ignore result to tests") | |
flag.Parse() | |
if infile == "" { | |
fmt.Println("no input provided, type \"-h\" for help") | |
os.Exit(1) | |
} | |
in := make(chan string, threads*3) | |
out := make(chan GrabData, threads*3) | |
inFd, _ := os.Open(infile) | |
defer inFd.Close() | |
wg := sync.WaitGroup{} | |
wg.Add(threads) | |
for i := 0; i < threads; i++ { | |
worker := GrabWorker{in, out} | |
worker.IOLoop(&wg) | |
} | |
go func() { | |
for { | |
data, ok := <-out | |
if !ok { | |
break | |
} | |
output, _ := json.Marshal(data) | |
if ignore { | |
_ = output | |
} else { | |
fmt.Println(string(output)) | |
} | |
} | |
}() | |
reader := bufio.NewReader(inFd) | |
for { | |
line, err := reader.ReadString('\n') | |
if err == io.EOF { | |
break | |
} | |
line = strings.TrimSpace(line) | |
if len(line) == 0 { | |
continue | |
} else { | |
in <- line | |
} | |
} | |
close(in) | |
wg.Wait() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go build zgrab-mini-demo.go | |
./zgrab-mini-demo.go -threads 1000 -timeout 10 -infile ip_port.targets | |
... | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment