Mercurial > mercurial > hgweb_golang.cgi
comparison src/netcat.go @ 10:b0784443ed87
add netcat and unsleep.
author | pyon@macmini |
---|---|
date | Sat, 04 Nov 2017 11:07:50 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:54a75ff1c288 | 10:b0784443ed87 |
---|---|
1 // OK : windows -> osx | |
2 // Not OK : osx -> windows | |
3 // OK : osx, win <-> yahoo.com | |
4 /* | |
5 Netcat | |
6 | |
7 -v verbose | |
8 -vv very verbose | |
9 -w timeout [ -w 2 2seconds ] | |
10 -i interval | |
11 -s local source address | |
12 -z zero-I/O mode ( for portscan ) | |
13 -u with UDP ( default use TCP ) | |
14 -n don't resolve name/address | |
15 -d stealth mode ( don't read from stdin ) | |
16 -l listen mode ( once ) | |
17 -L listen mode ( continuous ) | |
18 | |
19 Portscan | |
20 $ nc -v -z -w 1 192.168.1.1 1-140 | |
21 $ nc -u -v -z -w 1 192.168.1.1 1-140 | |
22 | |
23 Web-client | |
24 $ nc -v www.yahoo.com 80 | |
25 | |
26 File transfer | |
27 $ nc -l 1234 > file | |
28 % nc hoge.com 1234 < file | |
29 | |
30 Backdoor | |
31 C:\> nc -L -d -e cmd.exe -p 8080 | |
32 | |
33 */ | |
34 package main | |
35 | |
36 import ( | |
37 "flag" | |
38 "fmt" | |
39 "io" | |
40 "log" | |
41 "net" | |
42 "os" | |
43 ) | |
44 | |
45 func main() { | |
46 | |
47 verbose := flag.Bool( "v", false, "verbose" ) | |
48 vverbose := flag.Bool( "vv", false, "very verbose" ) | |
49 udp := flag.Bool( "u", false, "with udp" ) | |
50 // interval := flag.Int( "i", 0, "interval for lines sent, port scanned" ) | |
51 // timeout := flag.Int( "w", 7, "timeout for connects and final net reads" ) | |
52 listen := flag.Bool( "l", false, "listen mode" ) | |
53 zmode := flag.Bool( "z", false, "zero-I/O mode" ) | |
54 laddr := flag.String( "s", "", "local source address" ) | |
55 example := flag.Bool( "e", false, "print examples" ) | |
56 | |
57 flag.Parse() | |
58 | |
59 if *example { | |
60 printExample() | |
61 os.Exit( 0 ) | |
62 } | |
63 | |
64 if flag.NArg() == 0 { | |
65 fmt.Fprintf( os.Stderr, "netcat v0.1 (20170604)\n" ) | |
66 fmt.Fprintf( os.Stderr, " -h or --help option\n" ) | |
67 os.Exit( 0 ) | |
68 } | |
69 | |
70 p := "tcp" | |
71 if *udp { | |
72 p = "udp" | |
73 } | |
74 | |
75 if *listen { | |
76 | |
77 if flag.NArg() != 1 { | |
78 flag.PrintDefaults() | |
79 os.Exit( 1 ) | |
80 } | |
81 | |
82 port := flag.Arg( 0 ) | |
83 | |
84 addr := getLocalIP() | |
85 if *laddr == "" { | |
86 addr = *laddr | |
87 } | |
88 | |
89 if *verbose || *vverbose { | |
90 msg := fmt.Sprintf( "listening... %s:%s[%s]", addr, port, p ) | |
91 printVerbose( msg, "" ) | |
92 } | |
93 | |
94 l, err := net.Listen( p, net.JoinHostPort( addr, port ) ) | |
95 if err != nil { | |
96 log.Fatal( err ) | |
97 } | |
98 defer l.Close() | |
99 | |
100 conn, err := l.Accept() | |
101 if err != nil { | |
102 log.Fatal( err ) | |
103 } | |
104 io.Copy( os.Stdout, conn ) | |
105 conn.Close() | |
106 | |
107 } else { | |
108 | |
109 if flag.NArg() != 2 { | |
110 flag.PrintDefaults() | |
111 os.Exit( 1 ) | |
112 } | |
113 | |
114 addr := flag.Arg( 0 ) | |
115 port := flag.Arg( 1 ) | |
116 | |
117 conn, err := net.Dial( p, net.JoinHostPort( addr, port ) ) | |
118 if err != nil { | |
119 log.Fatal( err ) | |
120 } | |
121 defer conn.Close() | |
122 go mustCopy( os.Stdout, conn ) | |
123 mustCopy( conn, os.Stdin ) | |
124 } | |
125 | |
126 if *zmode { | |
127 /* | |
128 address := "" // for me. | |
129 port := "1-1024" | |
130 server := address + ":" + port | |
131 */ | |
132 } | |
133 } | |
134 | |
135 func mustCopy( dst io.Writer, src io.Reader ) { | |
136 if _, err := io.Copy( dst, src ); err != nil { | |
137 log.Fatal( err ) | |
138 } | |
139 } | |
140 | |
141 func printVerbose( vmsg, vvmsg string ) { | |
142 fmt.Fprintln( os.Stderr, vmsg, vvmsg ) | |
143 } | |
144 | |
145 func printExample() { | |
146 fmt.Fprintln( os.Stderr, "\n[Exaples]" ) | |
147 fmt.Fprintln( os.Stderr, " Portscan" ) | |
148 fmt.Fprintln( os.Stderr, " $ nc -v -z -w 1 192.168.1.1 1-140" ) | |
149 fmt.Fprintln( os.Stderr, " $ nc -u -v -z -w 1 192.168.1.1 1-140\n" ) | |
150 fmt.Fprintln( os.Stderr, " Web-client" ) | |
151 fmt.Fprintln( os.Stderr, " $ nc -v www.yahoo.com 80" ) | |
152 fmt.Fprintln( os.Stderr, " GET / HTTP/1.0[Enter]" ) | |
153 fmt.Fprintln( os.Stderr, " [ENTER]\n" ) | |
154 fmt.Fprintln( os.Stderr, " File transfer" ) | |
155 fmt.Fprintln( os.Stderr, " $ nc -l 1234 > file" ) | |
156 fmt.Fprintln( os.Stderr, " % nc hoge.com 1234 < file\n" ) | |
157 fmt.Fprintln( os.Stderr, " Backdoor" ) | |
158 fmt.Fprintln( os.Stderr, " C:\\> nc -L -d -e cmd.exe -p 8080" ) | |
159 } | |
160 | |
161 func getLocalIP() string { | |
162 addrs, err := net.InterfaceAddrs() | |
163 if err != nil { | |
164 log.Fatal( err ) | |
165 } | |
166 for _, addr := range addrs { | |
167 if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { | |
168 if ipnet.IP.To4() != nil { | |
169 return ipnet.IP.String() | |
170 } | |
171 } | |
172 } | |
173 return "127.0.0.1" | |
174 } | |
175 |