diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fe16cd5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +temp +*.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..1b897d5 --- /dev/null +++ b/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 + + +![goNmapTelegramBot](https://raw.githubusercontent.com/arnaucode/goNmapTelegramBot/master/screenrecording1.gif "goNmapTelegramBot") diff --git a/goNmapTelegramBot b/goNmapTelegramBot new file mode 100755 index 0000000..a651052 Binary files /dev/null and b/goNmapTelegramBot differ diff --git a/main.go b/main.go new file mode 100644 index 0000000..31f6bae --- /dev/null +++ b/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() +} diff --git a/nmap.go b/nmap.go new file mode 100644 index 0000000..0fee482 --- /dev/null +++ b/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) +} diff --git a/readConfig.go b/readConfig.go new file mode 100644 index 0000000..84cc016 --- /dev/null +++ b/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") + +} diff --git a/screenrecording1.gif b/screenrecording1.gif new file mode 100644 index 0000000..d5b6d3a Binary files /dev/null and b/screenrecording1.gif differ diff --git a/telegramBot.go b/telegramBot.go new file mode 100644 index 0000000..2d03a4c --- /dev/null +++ b/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) + } +}