Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
leech001 committed Dec 11, 2022
1 parent 7513955 commit 2512251
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
id: meta
uses: docker/metadata-action@v4
with:
images: leech001/telegram-site-monitor
images: leech001/nmap-telegram-bot
tags: |
type=ref,event=tag
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ telegram:

scan:
hosts:
- host: 8.8.8.0/24 #Scan network
- host: 8.8.8.0/24 #scan network
ports:
- 3389 #scan port
- host: google.com #Scan host
- host: google.com #scan host
ports:
- 80 #scan port
- 443 #scan port
Expand Down
13 changes: 9 additions & 4 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ func botUpdate(bot *tgbotapi.BotAPI, hosts []struct {
Ports []uint16
}) {

// Create string for HTTP(s) monitoring sites
hostString := ""
// Create string for list scanning hosts
listString := ""
for _, host := range hosts {
hostString += host.Host + "\n"
ports := ""
for _, port := range host.Ports {
ports += strconv.FormatUint(uint64(port), 10) + ","
}

listString += host.Host + " ports: " + ports + "\n"
}

// Telegram bot listener
Expand All @@ -89,7 +94,7 @@ func botUpdate(bot *tgbotapi.BotAPI, hosts []struct {
case "start":
msg.Text = "Hi, I am a NMAP scanner bot! Your (group) ID = " + strconv.FormatInt(update.Message.Chat.ID, 10)
case "list":
msg.Text = "Scanned hosts:\n" + hostString
msg.Text = "Scanned hosts:\n" + listString
default:
msg.Text = "I don't know that command"
}
Expand Down
41 changes: 22 additions & 19 deletions app/nmapScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

// Checking the open ports
// Scanning hosts for open ports
func nmapScan(update uint16, bot *tgbotapi.BotAPI, group int64, host struct {
Host string
Ports []uint16
Expand All @@ -28,29 +28,32 @@ func nmapScan(update uint16, bot *tgbotapi.BotAPI, group int64, host struct {
log.Fatalf("Unable to create nmap scanner: %v", err)
}

result, warnings, err := scanner.Run()
if err != nil {
log.Fatalf("Unable to run nmap scan: %v", err)
}

if warnings != nil {
log.Printf("Warnings: \n %v", warnings)
}
// Infinity scan loop
for {
result, warnings, err := scanner.Run()
if err != nil {
log.Fatalf("Unable to run nmap scan: %v", err)
}

for _, host := range result.Hosts {
if len(host.Ports) == 0 || len(host.Addresses) == 0 {
continue
if warnings != nil {
log.Printf("Warnings: \n %v", warnings)
}

for _, port := range host.Ports {
if port.State.State == "open" {
fmt.Printf("Host %q has %d/%s %s %s\n", host.Addresses[0], port.ID, port.Protocol, port.State, port.Service.Name)
for _, host := range result.Hosts {
if len(host.Ports) == 0 || len(host.Addresses) == 0 {
continue
}

for _, port := range host.Ports {
if port.State.State == "open" {
fmt.Printf("Host %q has %d/%s %s %s port\n", host.Addresses[0], port.ID, port.Protocol, port.State, port.Service.Name)

msg := tgbotapi.NewMessage(group, fmt.Sprintf("Host %q has %d/%s %s %s\n", host.Addresses[0], port.ID, port.Protocol, port.State, port.Service.Name))
bot.Send(msg)
msg := tgbotapi.NewMessage(group, fmt.Sprintf("Host %q has %d/%s %s %s port\n", host.Addresses[0], port.ID, port.Protocol, port.State, port.Service.Name))
bot.Send(msg)
time.Sleep(time.Second)
}
}
}
time.Sleep(time.Duration(update) * time.Second)
}

time.Sleep(time.Duration(update) * time.Second)
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.9"
services:
app:
image: leech001/...
image: leech001/nmap-telegram-bot
restart: always
volumes:
- ./conf:/app/conf:ro

0 comments on commit 2512251

Please sign in to comment.