works
arnaucode idoctnef@openmailbox.org
Tue, 23 May 2017 23:19:02 +0200
8 files changed,
121 insertions(+),
0 deletions(-)
A
README.md
@@ -0,0 +1,8 @@
+# goNmapTelegramBot + +This is a simple telegram bot, that gets an ip, performs a nmap port scan, and returns the result to the telegram user. + +Warning: is not recommended to leave the Bot running, a possible attack to the Bot (and the server where is the Bot running) could be insect command lines outside the nmap + + +
A
main.go
@@ -0,0 +1,21 @@
+package main + +import ( + "strings" +) + +func checkIp(text string) bool { + if len(strings.Split(text, ".")) != 4 { + return false + } + if len(strings.Split(text, "")) > 16 { + return false + } + + return true +} + +func main() { + readConfig() + telegramBot() +}
A
nmap.go
@@ -0,0 +1,16 @@
+package main + +import ( + "fmt" + "os/exec" +) + +func nmap(ip string) string { + out, err := exec.Command("nmap", "-T4", "F", ip).Output() + if err != nil { + fmt.Println(err) + } + + fmt.Println(string(out)) + return string(out) +}
A
readConfig.go
@@ -0,0 +1,25 @@
+package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" +) + +//Config stores the data from json twitterConfig.json file +type Config struct { + TelegramBotToken string `json:"telegramBotToken"` +} + +var config Config + +func readConfig() { + file, e := ioutil.ReadFile("config.json") + if e != nil { + fmt.Println("error:", e) + } + content := string(file) + json.Unmarshal([]byte(content), &config) + fmt.Println("config.json read comlete, Telegram bot ready") + +}
A
telegramBot.go
@@ -0,0 +1,49 @@
+package main + +import ( + "fmt" + "log" + + "gopkg.in/telegram-bot-api.v4" +) + +func telegramBot() { + bot, err := tgbotapi.NewBotAPI(config.TelegramBotToken) + if err != nil { + log.Panic(err) + } + + //bot.Debug = true + //log.Printf("Authorized on account %s", bot.Self.UserName) + + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + updates, err := bot.GetUpdatesChan(u) + + for update := range updates { + if update.Message == nil { + continue + } + + //log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) + fmt.Println("new ip to scan: " + update.Message.Text) + var result string + ok := checkIp(update.Message.Text) + if update.Message.Text == "/start" { + result = "welcome to nmap telegram bot, written in Go lang" + } else { + + if ok { + result = nmap(update.Message.Text) + } else { + result = "ip not valid" + } + } + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, result) + msg.ReplyToMessageID = update.Message.MessageID + + bot.Send(msg) + } +}