-
Notifications
You must be signed in to change notification settings - Fork 0
/
telnetd.go
181 lines (159 loc) · 4.86 KB
/
telnetd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
//Author: Sami Yessou
// Yet Another Fake Honeypot, this is my first honeypot written in Go so anything is beta even this comment, i will try to make it more or less just console i/o interaction
// If you want to contribute check what's on my todo & any issue/request is accepted :)
import (
"bufio"
"fmt"
"log"
"math/rand"
"net"
"os"
"strconv"
"strings"
"time"
)
const PORT = 23
var hostname = "GW-EXTERNAL"
func FloatToString(input_num float64) string {
// to convert a float number to a string
return strconv.FormatFloat(input_num, 'f', 6, 64)
}
func main() {
//create your file with desired read/write permissions
f, err := os.OpenFile("yafh-telnet.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
//defer to close when you're done with it, not because you think it's idiomatic!
defer f.Close()
//set output of logs to f
log.SetOutput(f)
server, _ := net.Listen("tcp", ":"+strconv.Itoa(PORT))
if server == nil {
panic("couldn't start listening: ")
log.Println("couldn't start listening: ")
}
conns := clientConns(server)
for {
go handleConn(<-conns)
}
}
func clientConns(listener net.Listener) chan net.Conn {
ch := make(chan net.Conn)
i := 0
go func() {
for {
client, _ := listener.Accept()
if client == nil {
fmt.Printf("couldn't accept: ")
log.Println("couldn't accept: ")
continue
}
i++
fmt.Printf("%d: %v <-> %v\n", i, client.LocalAddr(), client.RemoteAddr())
log.Printf("CLIENT OPENED CONNECTION %d: %v <-> %v\n", i, client.LocalAddr(), client.RemoteAddr())
ch <- client
}
}()
return ch
}
func ping(command string, client net.Conn) {
cmd := command
if strings.Contains(cmd, "ping") {
result := strings.Split(cmd, " ")
host := result[1]
fmt.Println(result[0])
fmt.Println(result[1])
fmt.Printf("PING %v (%[1]v) 56(84) bytes of data.\n", host)
for i := 0; i < 4; i++ {
time.Sleep(2 * time.Second)
time_ping := FloatToString((rand.Float64() * 10) + 20)
ttl := strconv.Itoa((rand.Intn(5) * 10))
fmt.Printf("64 bytes from %v: icmp_seq=%v ttl=%v time=%.2v ms\n", host, i, ttl, time_ping)
stringArray := []string{"64 bytes from ", host, ": icmp_seq =", strconv.Itoa(i), " ttl =", ttl, " time=", time_ping[0:3], " ms \n"}
s := strings.Join(stringArray, " ")
client.Write([]byte(s))
}
}
}
func handleConn(client net.Conn) {
ena := "nope"
conft := "nope"
b := bufio.NewReader(client)
c := bufio.NewReader(client)
motd := "This device is for authorized personnel only. \n" +
"If you have not been provided with permission to \n" +
"access this device - disconnect at once. \n" +
"*** Login Required. Unauthorized use is prohibited *** \n" +
"*** Ensure that you update the system configuration *** \n" +
"*** documentation after making system changes. *** \n" +
"User Access Verification: "
// "Password: "
log.Println("Sending MOTD to client")
client.Write([]byte(motd))
user, _ := b.ReadBytes('\n')
client.Write([]byte("Password: "))
pass, _ := b.ReadBytes('\n')
// client.Write([]byte("\n\n\n\n\n\n\n\n\n\n\n\n"))
fmt.Println("user : ", string(user))
fmt.Println("pass :", string(pass))
log.Printf("user : %v ", string(user))
log.Printf("pass : %v", string(pass))
for {
line, err := c.ReadBytes('\n')
if err != nil { // EOF, or worse
break
}
stringa := string(line)
cmd := strings.Trim(stringa, " \r\n")
fmt.Println(cmd)
if cmd == "ena" || cmd == "enab" || cmd == "enabl" || cmd == "enable" || cmd == "sudo su" {
ena = "yes"
fmt.Println("ENA ", ena)
}
if cmd == "configuration terminal" || cmd == "configure terminal" || cmd == "conf termi" || cmd == "conf t" {
conft = "yes"
fmt.Println("Confetti", conft)
}
if ena == "yes" && conft == "nope" {
fmt.Println("ENABLE USER")
ping(cmd, client)
stringArray1 := []string{hostname, "#"}
s1 := strings.Join(stringArray1, " ")
client.Write([]byte(s1))
}
if conft == "nope" && ena == "nope" {
fmt.Println("ENABLE USER")
stringArray3 := []string{hostname, ">"}
s3 := strings.Join(stringArray3, " ")
client.Write([]byte(s3))
ping(cmd, client)
}
if conft == "yes" && ena == "nope" {
fmt.Println("ENABLE USER")
client.Write([]byte("You do not have sufficient privileges to execute this command"))
conft = "nope"
}
if conft == "yes" && ena == "yes" {
fmt.Println("ENABLE USER")
ping(cmd, client)
stringArray := []string{hostname, "(config)#"}
s := strings.Join(stringArray, " ")
client.Write([]byte(s))
}
// if strings.Contains(stringa, "exit") {
fmt.Println("CMD: ", cmd)
log.Println("CMD Sent by client:", cmd)
if cmd == "exit" && ena == "nope" {
fmt.Printf("CLOSING conn")
client.Write([]byte("Bye..\n"))
client.Close()
}
if cmd == "exit" && ena == "yes" {
ena = "nope"
conft = "nope"
cmd = ""
}
}
}