-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelegram.go
More file actions
177 lines (154 loc) · 4.32 KB
/
telegram.go
File metadata and controls
177 lines (154 loc) · 4.32 KB
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
package main
import (
"fmt"
"log"
"strings"
"os"
"io/ioutil"
"regexp"
"encoding/json"
"strconv"
"bufio"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
const telegramKey = "teste" //this is the passphare that users need send to register (/monitor passphase)
const ruleNewUser = "\\/monitor [A-Za-z0-9]*\\z"
const ruleShowReport = "/lastreport"
const APIKEY = ""
type FileSignature struct {
Signature string `json:"Signature"`
Text string `json:"Text"`
}
type Filedata struct {
Name string `json:"Filename"`
Malware string `json:"Malware_Name"`
Signature []FileSignature `json:"Description"`
}
type FinalReport struct {
Path string `json:"Path"`
TotalFiles int64 `json:"Total"`
Positives int64 `json:"Positives"`
Date string `json:"Date"`
Files []Filedata `json:"Infected_Files"`
}
func NewUser(idUser int) {
if ! CheckUserPermission(idUser) {
fmt.Println("Register New User:",strconv.Itoa(idUser))
file, err := os.OpenFile(
"telegram.users",
os.O_APPEND|os.O_WRONLY|os.O_CREATE,
0666,
)
if err != nil {
fmt.Println(err)
}
defer file.Close()
// Write bytes to file
byteSlice := []byte(strconv.Itoa(idUser)+"\n")
_, err = file.Write(byteSlice)
if err != nil {
fmt.Println(err)
}
//log.Printf("Wrote %d bytes.\n", bytesWritten)
}
}
func SendLastReport(bot *tgbotapi.BotAPI, chatID int64) {
var lastreport FinalReport
if _,err := os.Stat("LastReport.json"); err == nil {
fmt.Println("Sending Last Report user ",chatID)
cg,_:= ioutil.ReadFile("lastreport.json")
_= json.Unmarshal([]byte(cg),&lastreport)
message := "<b>LAST REPORT</b>\n"
for _,file := range lastreport.Files {
//telegram API limit message size 4096
if (len(message)+len(file.Name)+len(file.Malware)) > 4000 {
fmt.Println(message)
msg := tgbotapi.NewMessage(chatID, message)
msg.ParseMode = "html"
bot.Send(msg)
message = ""
}
message += "<b>File:</b> <i>"+file.Name+"</i>\n"
message += "<b>Malware:</b><i>"+file.Malware+"</i>\n"
}
// if len(message) > 0 {
// fmt.Println("SEND MESSAGE to:",chatID)
// fmt.Println(message)
// msg := tgbotapi.NewMessage(chatID, message)
// msg.ParseMode = "html"
// bot.Send(msg)
// }
message += "<b>END</b>"
msg := tgbotapi.NewMessage(chatID, message)
msg.ParseMode = "html"
bot.Send(msg)
} else if os.IsNotExist(err) {
fmt.Println("There isn't Whitelist file")
} else {
fmt.Println("Something goes wrong")
fmt.Println(err)
}
}
func CheckUserPermission(userID int) bool {
fd, err := os.Open("telegram.users")
if err != nil {
return false
}
defer fd.Close()
if err != nil {
return false
}
scanner := bufio.NewScanner(fd)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
x, _ := strconv.Atoi(scanner.Text())
if userID == x {
fmt.Println("User ",userID," Autorized")
return true
}
}
fmt.Println("User ",userID,"Not Autorized")
return false
}
func main() {
bot, err := tgbotapi.NewBotAPI(APIKEY)
if err != nil {
log.Panic(err)
}
bot.Debug = false
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
//var chatid int64
for update := range updates {
if update.Message == nil { // ignore any non-Message Updates
continue
}
//Check New User
re := regexp.MustCompile(ruleNewUser)
result := re.FindStringSubmatch(update.Message.Text)
if len(result) > 0 {
text := strings.Fields(result[0])
if text[0] == "/monitor" && text[1] == telegramKey {
NewUser(int(update.Message.Chat.ID))
//fmt.Println(update.Message.Chat.ID)
//chatid = update.Message.Chat.ID
//break
}
}
re = regexp.MustCompile(ruleShowReport)
result = re.FindStringSubmatch(update.Message.Text)
if len(result) > 0 {
//teste := strings.Fields(result[0])
if CheckUserPermission(int(update.Message.Chat.ID)) {
SendLastReport(bot, update.Message.Chat.ID)
}
}
//msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
//msg.ReplyToMessageID = update.Message.MessageID
//bot.Send(msg)
}
//msg := tgbotapi.NewMessage(chatid, "OK")
//bot.Send(msg)
}