Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
leech001 committed Dec 11, 2022
1 parent 1cf7701 commit 7513955
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 17 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Publish Docker image

on:
push:
tags:
- "v*"

jobs:
build_and_push_to_registry:
name: Build and push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: leech001/telegram-site-monitor
tags: |
type=ref,event=tag
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
36 changes: 36 additions & 0 deletions .github/workflows/go_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Go release builder

on:
push:
tags:
- "v*"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ^1.19

- name: Build amd64
run: cd app && go get main && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o monitor-amd64

- name: Build arm
run: cd app && go get main && CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o monitor-arm

- name: Build arm64
run: cd app && go get main && CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o monitor-arm64

- name: Build release
uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: false
files: |
app/monitor-amd64
app/monitor-arm
app/monitor-arm64
17 changes: 2 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
app/go.sum
app/conf
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## [0.0.0] - 2022-12-11
- [ ADD ] Initial project;
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:latest AS builder
WORKDIR /build
COPY app .
RUN go get main
RUN CGO_ENABLED=0 go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o monitor


FROM alpine:latest
RUN apk add --no-cache bash nmap
WORKDIR /app
COPY --from=builder /build/monitor .
CMD sh -c "./monitor"
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2022 Leech001 <[email protected]>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# nmap-telegram-bot
Telegram bot for organizing constant monitoring of open ports on the network
# Telegram bot for organizing constant monitoring of open ports on the network

## Simple use
Download the required repository;

```bash
$ git clone https://github.com/grfc-ru/nmap-telegram-bot.git
```

Change `conf/config.yaml`

Run `sudo docker-compose up -d`

## Configure

```yaml
app:
update: 30 #time to rechecking hosts (sec)

telegram:
token: 244516775:AAGZп55654ASsFFpbjyNA9su6gQU-Qs #Token for you Telegram BOT
group: 123456 # Telegram you ID or group ID (use command for BOT /start

scan:
hosts:
- host: 8.8.8.0/24 #Scan network
ports:
- 3389 #scan port
- host: google.com #Scan host
ports:
- 80 #scan port
- 443 #scan port
```
## Telegram BOT command
```
/start # Print you ID or group ID need you for config
/list # Print scanning hosts
```
14 changes: 14 additions & 0 deletions app/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module main

go 1.19

require (
github.com/Ullaakut/nmap/v2 v2.2.2
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect
)
101 changes: 101 additions & 0 deletions app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"log"
"os"
"path/filepath"
"strconv"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"gopkg.in/yaml.v3"
)

type Config struct {
App struct {
Update uint16
}
Telegram struct {
Token string
Group int64
}
Scan struct {
Hosts []struct {
Host string
Ports []uint16
}
}
}

func main() {
// Read config from yaml
config := Config{}
filename, _ := filepath.Abs("./conf/config.yaml")
yamlFile, err := os.ReadFile(filename)
if err != nil {
panic(err)
}

// Parse yaml
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
panic(err)
}

// Telegram bot
bot, err := tgbotapi.NewBotAPI(config.Telegram.Token)
if err != nil {
panic(err)
}

// Debug Telegram bot
bot.Debug = false

// Running NMAP scaner
for _, host := range config.Scan.Hosts {
go nmapScan(config.App.Update, bot, config.Telegram.Group, host)
}

botUpdate(bot, config.Scan.Hosts)
}

// Telegram bot for listening to incoming commands
func botUpdate(bot *tgbotapi.BotAPI, hosts []struct {
Host string
Ports []uint16
}) {

// Create string for HTTP(s) monitoring sites
hostString := ""
for _, host := range hosts {
hostString += host.Host + "\n"
}

// Telegram bot listener
u := tgbotapi.NewUpdate(0)
u.Timeout = 300
updates := bot.GetUpdatesChan(u)

for update := range updates {
if update.Message == nil { // ignore any non-Message updates
continue
}

if !update.Message.IsCommand() { // ignore any non-command Messages
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")

switch update.Message.Command() {
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
default:
msg.Text = "I don't know that command"
}

if _, err := bot.Send(msg); err != nil {
log.Panic(err)
}
}
}
56 changes: 56 additions & 0 deletions app/nmapScan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"
"log"
"strconv"
"time"

"github.com/Ullaakut/nmap/v2"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

// Checking the open ports
func nmapScan(update uint16, bot *tgbotapi.BotAPI, group int64, host struct {
Host string
Ports []uint16
}) {
ports := ""
for _, port := range host.Ports {
ports += strconv.FormatUint(uint64(port), 10) + ","
}

scanner, err := nmap.NewScanner(
nmap.WithTargets(host.Host),
nmap.WithPorts(ports),
)
if err != nil {
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)
}

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\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)
}
}
}

time.Sleep(time.Duration(update) * time.Second)
}
16 changes: 16 additions & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
app:
update: 60

telegram:
token: 244516775:AAGZUrt9eQjhGSsFFpbjyNA9su6gQU-Qs
group: -6596563379

scan:
hosts:
- host: 8.8.8.0/24
ports:
- 3389
- host: google.com
ports:
- 80
- 443
8 changes: 8 additions & 0 deletions docker-compose-source.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3.9"
services:
app:
restart: always
build:
context: .
volumes:
- ./conf:/app/conf:ro
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3.9"
services:
app:
image: leech001/...
restart: always
volumes:
- ./conf:/app/conf:ro

0 comments on commit 7513955

Please sign in to comment.