-
Notifications
You must be signed in to change notification settings - Fork 2
/
gtalkbot.rb
344 lines (306 loc) · 10.2 KB
/
gtalkbot.rb
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
require 'eventmachine'
require 'xmpp4r/client'
require 'yaml'
include Jabber
puts "-> GtalkBot class loaded!"
class GtalkBot
#--------------------------------------------
# Initialize function
#--------------------------------------------
def initialize()
puts "-> Initialize GtalkBot method called!"
# get configurations from config.yml file
tree = YAML::parse( File.open( "config.yml" ) )
obj_tree = tree.transform
yml_un=obj_tree['username']
yml_pw=obj_tree['password']
yml_dn=obj_tree['devicename']
yml_to=obj_tree['target']
# defines message target address
@target_account = yml_to
# initialize variable for the array of strings
@stringlist = [""]
# initialize @callbacks object
@callbacks = {}
initialize_callbacks()
#Jabber::debug=true
jid = JID::new(yml_un+'/'+yml_dn)
password = yml_pw
@cl = Client::new(jid)
@cl.connect
@cl.auth(password)
@cl.send(Presence.new.set_type(:available))
# Add a message callback to respond to peer requests
@cl.add_message_callback do |inmsg|
puts "Received Message: "+inmsg.body
#self.sendmessage("You have sent me this message: #{inmsg.body}")
# adding message to string array
@stringlist = inmsg.body.split(' ')
#puts "Printing CMD array:"
#puts @stringlist.inspect
exec_cmd(@stringlist)
end
end
#------------------------------------
# function to send xmpp reply message
#------------------------------------
def sendmessage(text)
to = @target_account
subject = "XMPP4R test"
body = text
message = Message::new(to, body).set_type(:chat).set_id('1').set_subject(subject)
@cl.send message
end
#-----------------------------------------------
# function to add callbacks to @callbacks object
#-----------------------------------------------
def add_callback(name, &callback)
name = name.to_s
@callbacks[name] = callback
self
end
#------------------------------------------------------------
# function to run callbacks from @callback object (if exists)
#------------------------------------------------------------
def run_callback(name, *args)
name = name.to_s
@callbacks[name].call(*args) if @callbacks[name]
end
#--------------------------------------------------------------
# function called to execute a remote command (initial parsing)
#--------------------------------------------------------------
def exec_cmd(params)
#puts params.inspect
@callbacks.each do |name, callback|
if params[0].eql?(name) == true
callback.call(params)
return
end
end
puts "--> NO CMD FOUND!"
end
#-------------------------------------------------------------
# Initialize Callbacks - to be customized
#-------------------------------------------------------------
def initialize_callbacks
# help
add_callback(:help) do |arg|
commandlist()
end
# exit
add_callback(:exit) do |arg|
finalize()
end
# quit
add_callback(:quit) do |arg|
finalize()
end
# Gtalk Status Ready
add_callback(:ready) do |arg|
SetStatusReady()
end
# Gtalk Status Do Not Disturb
add_callback(:busy) do |arg|
SetStatusDND()
end
# ...
#add_callback(:dir) do |arg|
# puts arg.inspect
# if(arg[1] == nil)
# arg[1] = "/home"
# end
# Dir.chdir arg[1] # need to escape path from malicious ;rm -rf injection
# result = %x(ls -la)
# sendmessage(">ls -la #{arg[1]}\r\n#{result}")
# puts result
#end
# Network
add_callback(:network) do |arg|
ethernet_interfaces()
end
# List system active users
add_callback(:active_users) do |arg|
active_users()
end
# File System Usage
add_callback(:file_system_usage) do |arg|
file_system_usage()
end
# Processes Memory
add_callback(:proc_mem) do |arg|
processes_using_most_memory()
end
# Processes Cpu
add_callback(:proc_cpu) do |arg|
processes_using_most_cpu()
end
add_callback(:proc_zombie) do |arg|
zombie_processes()
end
#add_callback(:tcp_sockets_listen) do |arg|
# listening_tcp_sockets()
#end
#add_callback(:tcp_sockets_connected) do |arg|
# connected_tcp_sockets()
#end
end
#-------------------------------------------------------------
# other auxiliar functions
#--------------------------------------------------------------
#--------------------------------------------------------------
# List Callback Commands
def commandlist()
sendmessage("Available commands:")
@callbacks.each do |name|
str = "#{name[0]}"
puts "--> "+str
sendmessage(str)
end
end
#--------------------------------------------------------------
# Gtalk Status Ready
def SetStatusReady()
@cl.send(Presence.new.set_type(:available))
end
#--------------------------------------------------------------
# Gtalk Status Do Not Disturb
def SetStatusDND()
@cl.send(Jabber::Presence.new.set_show(:dnd))
end
#--------------------------------------------------------------
# Exit Application
def finalize()
puts "-> Finalize GtalkBot method called"
#@cl.close
EM.stop
return
end
#--------------------------------------------------------------
# File System Usage
def file_system_usage
fs_type_result = `df -T`
fsystem = ['hfs', 'ext4', 'ext3', 'ext', 'ext2'].select{|fst| /\s#{fst}\s/.match(fs_type_result)}.inject([]) do |result, fst|
`df --type=#{fst} -H`.split("\n")[1..-1].each do |row|
vals = row.split(/\s+/)
mount = vals[5..-1].join(" ")
size = vals[1]
used = vals[4]
str = "#{mount} | #{size} | #{used}"
puts "--> "+str
sendmessage(str)
end
end
end
#--------------------------------------------------------------
# Ethernet Interfaces configuration
def ethernet_interfaces
ifaces = /(eth\d)/.match(`ifconfig -a`).captures.inject([]) do |result, iface|
ifconfig = `ifconfig #{iface}`
ip = /inet\saddr:(\d+\.\d+\.\d+\.\d+)/.match(ifconfig).captures.last
mac = /HWaddr\s(\w+\:\w+\:\w+\:\w+\:\w+\:\w+)/.match(ifconfig).captures.last
str = "ip: #{ip} | mac: #{mac}"
puts "--> "+str
sendmessage(str)
end
end
#--------------------------------------------------------------
def active_users
users =`who`.split("\n").collect do |user|
user_data = user.strip.split(/\s+/)
ip = /\((.*)\)/.match(user_data.last).captures.last
active_since = user_data[2] + ' ' + user_data[3]
str = "[#{user_data[0]}] user active since [#{active_since}] from ip [#{ip}]"
puts "--> "+str
sendmessage(str)
end
end
#--------------------------------------------------------------
def processes_using_most_memory
procs = `ps -eo pid,pcpu,pmem,comm`.split("\n")[1..-1].collect do |p|
p_data = p.strip.split(/\s+/)
{:pid => p_data[0], :command => p_data[3], :cpu => p_data[1].to_f, :memory => p_data[2].to_f}
end
largest_items_by_attribute(procs, :memory)
for i in 1..10 do
str = "pid="+procs[i][:pid]+
" | cmd="+procs[i][:command]+
" | cpu="+procs[i][:cpu].to_s+
" | mem="+procs[i][:memory].to_s
puts "--> "+str
sendmessage(str)
end
end
#--------------------------------------------------------------
def processes_using_most_cpu
procs = `ps -eo pid,pcpu,pmem,comm`.split("\n")[1..-1].collect do |p|
p_data = p.strip.split(/\s+/)
{:pid => p_data[0], :command => p_data[3], :cpu => p_data[1].to_f, :memory => p_data[2].to_f}
end
largest_items_by_attribute(procs, :cpu)
for i in 1..10 do
str = "pid="+procs[i][:pid]+
" | cmd="+procs[i][:command]+
" | cpu="+procs[i][:cpu].to_s+
" | mem="+procs[i][:memory].to_s
puts "--> "+str
sendmessage(str)
end
end
#--------------------------------------------------------------
def largest_items_by_attribute(items, by_attr)
largest_items = items.sort_by {|i| -i[by_attr]}
largest_items = largest_items[0..4] if largest_items.count > 5
largest_items.count.eql?(1) ? largest_items.first : largest_items
end
#--------------------------------------------------------------
def zombie_processes
procs = `ps -eo pid,pcpu,pmem,state,comm`.split("\n")[1..-1].inject({}) do |result, p|
p_data = p.strip.split(/\s+/)
if (p_data[3].eql?('Z'))
str = "pid="+p_data[0]+
" | cmd="+p_data[4]+
" | cpu="+ p_data[1]+
" | mem="+ p_data[2]
puts "--> "+str
sendmessage(str)
end
end
end
#--------------------------------------------------------------
def services
`cat /etc/services`.split("\n").inject({}) do |result, serv|
unless /^#/.match(serv) or serv.eql?("")
serv_data = serv.split(/\t+/)
pp = serv_data[1].split("/")
result[pp[0]] = {:service => serv_data[0], :port => pp[0], :protocol => pp[1]}
end
result
end
end
#--------------------------------------------------------------
def socket_processes
`lsof -i -n`.split("\n")[1..-1].inject({}) do |result, s|
unless /^lsof/.match(s)
s_data = s.strip.split(/\s+/)
m = /(.*)->.*/.match(s_data[7]) || /.*:(.*)/.match(s_data[7])
local_port = m.captures.first.split(":").last
result[local_port] = s_data[0]
end
result
end
end
#--------------------------------------------------------------
def listening_tcp_sockets
# TODO
end
#--------------------------------------------------------------
def connected_tcp_sockets
# TODO
end
#--------------------------------------------------------------
end
EM.run {
puts "-> Launching EventMachine Run Event"
chatbot = GtalkBot.new()
chatbot.sendmessage("GTalkBot Initialized!")
}