-
Notifications
You must be signed in to change notification settings - Fork 0
/
botnetstat.tcl
306 lines (298 loc) · 9.36 KB
/
botnetstat.tcl
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
encoding system utf-8
::ck::require botnet 0.3
::ck::require cache
namespace eval botnetstat {
variable version 1.0
variable author "Chpock <chpock@gmail.com>"
namespace import -force ::ck::*
namespace import -force ::ck::botnet::botnet
namespace import -force ::ck::config::config
namespace import -force ::ck::cache::cache
}
proc ::botnetstat::init { } {
config register -id update -default 999d -type time \
-desc "Update interval." -access "n" -folder "bnstat" -hook chkconfig
config register -id file.tree -default "botnet.tree.php" -type str \
-desc "File where save bots tree." -access "n" -folder "bnstat"
config register -id file.data -default "botnet.data.php" -type str \
-desc "File where save additional data about bot." -access "n" -folder "bnstat"
config register -id file.encoding -default utf-8 -type encoding \
-desc "Encoding for output files." -access "n" -folder "bnstat"
cache register -maxrec 1000 -ttl 0
etimer -norestart -interval [config get .bnstat.update] ::botnetstat::update
}
proc ::botnetstat::parseversion { str } {
if { ![regexp {(\d)(\d\d)(\d\d)(\d\d)} $str - 1 2 3 4] } { return $str }
set ver [list $1]
lappend ver [string trimleft $2 0]
lappend ver [string trimleft $3 0]
lappend ver [string trimleft $4 0]
return [string trimright [join $ver .] .]
}
proc ::botnetstat::escape { str } {
return [string map [list {'} {\'} {\\} {\\\\}] $str]
}
proc ::botnetstat::update { {sid ""} } {
if { $sid eq "" } {
session create -proc ::botnetstat::update
session hook BotNetResponse ::botnetstat::checkresponse
session hook !onDestroy ::botnetstat::rebuild
session event StartUpdate
return
}
session import
#nick
#|--nick
#| |--nick
#| | `--nick
#| |--nick
#| | |--nick
#| | `--nick
#| `--nick
#`--nick
debug -debug "Register all bots in cache..."
array set online [list]
foreach_ [botlist] {
cache makeid "![lindex_ 0]"
if { [cache get xdata] } {
array set {} $xdata
} {
array set {} [list]
}
set (_status) "on"
set (_uplink) "![lindex_ 1]"
set (_version) [parseversion [lindex_ 2]]
cache put [array get {}]
unset {}
debug -debug "Registered bot: %s" [lindex_ 0]
set online(![lindex_ 0]) 1
}
array set childs [list]
set info(!${::botnet-nick}) [list "on" [parseversion $::numversion]]
debug -debug "Mark other knowns bot as offline..."
foreachkv [cache getindex] {
if { [lindex $k 0] == "!-local" || ![cache get -uid $k xdata] } continue
set k [lindex $k 0]
array set {} $xdata
if { ![info exists online($k)] } {
set (_status) "off"
debug -debug "Mark offline: %s" [string range $k 1 end]
}
if { ![info exists childs($(_uplink))] } {
set childs($(_uplink)) [list $k]
} {
lappend childs($(_uplink)) $k
}
if { ![info exists childs($k)] } {
set childs($k) [list $(_uplink)]
} {
lappend childs($k) $(_uplink)
}
set info($k) [list $(_status) $(_version)]
unset {}
}
set d [list {<?}]
foreacharray childs {
set x [list]
foreach_ $v { lappend x [escape $_] }
set x [join $x {', '}]
lappend d "\$b\['[escape $k]'\] = array\( 0 => '${x}'\);"
set_ $info($k)
set_ "'_status' => '[lindex_ 0]', '_version' => '[escape [lindex_ 1]]'"
lappend d "\$n\['[escape $k]'\] = array\(${_}\);"
}
lappend d "\$defbot = '![escape ${::botnet-nick}]';"
lappend d {?>}
set fn [config get file.tree]
if { [catch {set fid [open $fn w]} m] } {
debug -err "Error while save tree data to file <%s>." $fn
return
}
fconfigure $fid -encoding [config get "file.encoding"]
puts $fid [join $d "\n"]
close $fid
debug -debug "Tree data saved to file <%s>." $fn
botnet request -bot "-local" -service "_ck_pubinfo_" -cmd fullinfo
# TODO: поставить вменяемый timeout на запрос
botnet request -timeout 60 -global -service "_ck_pubinfo_" -cmd info
return
}
proc ::botnetstat::checkresponse { sid } {
session import
if { $BotNetStatus < 0 } return
cache makeid "!$Bot"
#TODO: $Response может быть не листом, поэтому нужно проверить что б не повиснуть
array set res $Response
debug -debug "rcvd info from bot <%s>:" $Bot
foreacharray res {
debug -raw- "%20s : %s" $k $v
}
if { ![info exists res(rt)] } { set res(rt) "i" }
if { [cache get _] } {
array set {} $_
}
# Тут будет проверка на то, как давно были получены "полные" данные от бота, если давно -> unset {}
if { $res(rt) ne "f" } {
if { ![array exists {}] || ![info exists (_timestampf)] } {
if { $Mark eq "2try" } {
debug -error "No full info after second try, ignore bots info."
} {
debug -debug "No saved info about bot and info is not full, retry full."
botnet request -bot $Bot -service "_ck_pubinfo_" -cmd "fullinfo" -mark "2try"
}
return
}
debug -debug "Got partial info from bot <%s>, is ok." $Bot
} {
debug -debug "Got full info from bot <%s>, is ok." $Bot
}
# переносим всю фигню
foreacharray res {
if { [string match "_*" $k] } continue
set ($k) $v
if { $k eq "rt" } { set (_timestamp$v) [clock seconds] }
}
if { $res(rt) eq "f" } {
foreacharray {} {
if { [string match "_*" $k] } continue
if { ![info exists res($k)] } { unset ($k) }
}
}
cache put [array get {}]
}
proc ::botnetstat::rebuild { sid } {
debug -debug "Rebuilding bots infos..."
# 4 раздела
# инфа о владельце
# инфа о боте (включает irc инфу и инфу о хосте)
# инфа о траффике
set d [list {<?}]
# array set info [list]
proc deltatime { num } {
upvar {} {}
if { ![info exists (sendtime)] } { return "" }
if { [catch {expr { $(sendtime) - $num }} v] } { return "" }
return [duration $v]
}
proc makesize { num } {
if { $num < 2048 } { return $num }
set num [expr { 1.0 * $num / 1024 }]
if { $num < 2048 } { return "[format %.2f $num]Kb" }
set num [expr { 1.0 * $num / 1024 }]
return "[format %.2f $num]Mb"
}
foreachkv [cache getindex] {
if { ![cache get -uid $k xdata] } continue
array set {} $xdata
set k [lindex $k 0]
if { $k eq "!-local" } { set k "!${::botnet-nick}" }
set b [escape $k]
set_ [list]
array set arr2 [list "_owner" 0 "_traffic" 0 "_irc" 0 "_system" 0 "_addinfo" 0]
foreacharray {} {
switch -exact -- $k {
"network" - "ircnick" -
"owners" - "masters" - "users" { set arr2(_irc) 1; set arr2(_addinfo) 1 }
"channels" {
set v [join $v {, }]
set arr2(_irc) 1; set arr2(_addinfo) 1
}
"owner.email" - "owner.city" - "owner.realname" -
"owner" { set arr2(_owner) 1; set arr2(_addinfo) 1 }
"owner.url" {
if { ![string equal -nocase -length 7 {http://} $v] } {
set v "http://$v"
}
set arr2(_owner) 1; set arr2(_addinfo) 1
}
"modules" {
if { [catch {
set x [list]
foreach mod $v {
lappend x "[lindex $mod 0] v[lindex $mod 1]"
}
}] } {
debug -warn "Error while parsing <module> field from bot <%s>: %s" $b $v
continue
}
set v [join $x {, }]
set arr2(_system) 1; set arr2(_addinfo) 1
}
"tclver" - "tclthd" - codepage -
"handlen" - "os" { set arr2(_system) 1; set arr2(_addinfo) 1 }
"eggver" {
if { [catch {lreplace [split $v " "] 1 1} x] } {
debug -warn "Error while parsing <eggver> field from bot <%s>: %s" $b $v
continue
}
set v [join $x "+"]
set arr2(_system) 1; set arr2(_addinfo) 1
}
"serveronline" - "uptime" {
if { [set x [deltatime $v]] == "" } {
debug -warn "Error while parsing <%s> field from bot <%s>: %s" $k $b $v
continue
}
set v $x
set arr2(_system) 1; set arr2(_addinfo) 1
}
"traffic" {
catch { unset x }
array set x [list]
if { [catch {
foreach rec $v {
if { ![lexists {irc botnet partyline transfer misc total} [lindex $rec 0]] } continue
if { [llength [set 1 [lrange $rec 1 end]]] != 4 } continue
set 2 [list]
foreach 3 $1 { lappend 2 [makesize $3] }
set x([lindex $rec 0]) [join $2 { }]
}
}] || [array size x] == 0 } {
unset x
debug -warn "Error while parsing <traffic> field from bot <%s>: %s" $b $v
continue
}
set v [list]
foreach {1 2} [array get x] {
lappend v "'$1' => '$2'"
}
lappend_ "'traffic' => array\([join $v {, }]\)"
set arr2(_traffic) 1; set arr2(_addinfo) 1
unset x
continue
}
"sendtime" {
if { [catch {clock format $v} x] } {
debug -warn "Error while parsing <sendtime> field from bot <%s>: %s" $b $v
continue
}
set v $x
}
}
lappend_ "'[escape $k]' => '[escape $v]'"
}
foreacharray arr2 {
lappend_ "'${k}' => $v"
}
set_ [join $_ {, }]
lappend d "\$n\['${b}'\] = array\(${_}\);"
unset {} arr2
}
rename deltatime ""
rename makesize ""
lappend d {?>}
set fn [config get file.data]
if { [catch {set fid [open $fn w]} m] } {
debug -err "Error while save data data to file <%s>." $fn
return
}
fconfigure $fid -encoding [config get "file.encoding"]
puts $fid [join $d "\n"]
close $fid
debug -debug "Bots data saved to file <%s>." $fn
}
proc ::botnetstat::chkconfig { mode var oldv newv hand } {
if { ![string equal -length 3 $mode "set"] } return
etimer -interval $newv ::botnetstat::update
return
}