reducing number of http requests per #186. Rewriting the hooked browser manager and the event updater.

git-svn-id: https://beef.googlecode.com/svn/trunk@611 b87d56ec-f9c0-11de-8c8a-61c5e9addfc9
This commit is contained in:
mosse.benjamin
2010-12-16 09:10:49 +00:00
parent fdbeb437e4
commit 9552f259b3
5 changed files with 140 additions and 99 deletions

View File

@@ -36,6 +36,7 @@ require 'lib/model/autoloading'
require 'lib/model/plugin'
require 'lib/model/http'
require 'lib/model/browserdetails'
require 'lib/model/distributedenginerules'
require 'lib/crypto'

View File

@@ -9,17 +9,81 @@ class Panel < BeEF::HttpController
def initialize
super({
'paths' => {
'/' => method(:index)
'/' => method(:index),
'/hooked-browser-tree-update.json' => method(:hooked_browser_tree_update)
}
})
end
#
def index
# should be rendered with Erubis::FastEruby
@body = 'a'
# default index page
def index; end
# return a JSON object contains all the updates for the hooked browser trees
def hooked_browser_tree_update
# retrieve the hbs that are online
hooked_browsers_online = zombies2json_simple(BeEF::Models::Zombie.all(:lastseen.gte => (Time.new.to_i - 30)))
# retrieve the hbs that are offline
hooked_browsers_offline = zombies2json_simple(BeEF::Models::Zombie.all(:lastseen.lt => (Time.new.to_i - 30)))
# retrieve the distributed engine rules that are enabled
distributed_engine_rules = distributed_engine_rules_2_json_simple(BeEF::Models::DistributedEngineRules.all(:enabled => true))
# hash that gets populated with all the information for the hb trees
ret = {
'success' => true,
# the list of hb
'hooked-browsers' => {
'online' => hooked_browsers_online,
'offline' => hooked_browsers_offline
},
# the rules for the distributed engine
'ditributed-engine-rules' => distributed_engine_rules
}
@body = ret.to_json
end
# Takes a list distributed engine rules and format the results into JSON
def distributed_engine_rules_2_json_simple(rules)
#TODO
end
# TODO: we have duplicate functions here (see ui/zombies/zombies.rb), maybe we need to
# organize the code differently.
# Takes a list of zombies and format the results in a JSON array.
def zombies2json_simple(zombies)
zombies_hash = {}
i = 0
zombies.each do |zombie|
# create hash of zombie details
zombies_hash[i] = (get_simple_hooked_browser_hash(zombie))
i+=1
end
zombies_hash
end
# create a hash of simple hooked browser details
def get_simple_hooked_browser_hash(hooked_browser)
browser_icon = BeEF::Models::BrowserDetails.browser_icon(hooked_browser.session)
os_icon = BeEF::Models::BrowserDetails.os_icon(hooked_browser.session)
domain = BeEF::Models::BrowserDetails.get(hooked_browser.session, 'HostName')
return {
'session' => hooked_browser.session,
'ip' => hooked_browser.ip,
'domain' => domain,
'browser_icon' => browser_icon,
'os_icon' => os_icon
}
end
end
end

View File

@@ -11,7 +11,6 @@ DataGrid = function(url, page, base) {
url: this.url,
storeId: 'myStore',
baseParams: this.base,
//autoLoad: {params:{start:0, limit:this.page, sort:"date", dir:"DESC"}},
idProperty: 'id',
fields: ['id','type','event','date'],
totalProperty: 'count',

View File

@@ -1,5 +1,5 @@
PanelViewer = {};
var mainPanel, zombiesTreeLists, zombieTabs;
var mainPanel, zombiesTreeLists, zombieTabs, zombiesManager;
Ext.onReady(function() {
@@ -11,6 +11,7 @@ Ext.onReady(function() {
};
zombieTabs = new ZombieTabs(zombiesTreeLists);
zombiesManager = new ZombiesMgr(zombiesTreeLists);
mainPanel = new MainPanel();
var viewport = new Ext.Viewport({
@@ -28,5 +29,30 @@ Ext.onReady(function() {
new DoLogout();
new AboutWindow();
new ZombiesMgr(zombiesTreeLists);
});
/*
* Panel Events Updater
*
* This event updater retrieves updates every 8 seconds. Those updates
* are then pushed to various managers (i.e. the zombie manager).
*/
Ext.TaskMgr.start({
run: function() {
Ext.Ajax.request({
url: '/ui/panel/hooked-browser-tree-update.json',
method: 'POST',
success: function(response) {
var updates = Ext.util.JSON.decode(response.responseText);
var distributed_engine_rules = (updates['ditributed-engine-rules']) ? updates['ditributed-engine-rules'] : null;
var hooked_browsers = (updates['hooked-browsers']) ? updates['hooked-browsers'] : null;
if(zombiesManager && hooked_browsers) {
zombiesManager.updateZombies(hooked_browsers, distributed_engine_rules);
}
}
});
},
interval: 8000
});

View File

@@ -1,24 +1,10 @@
var ZombiesMgr = function(zombies_tree_lists) {
var selectedZombie = null;
var addZombie = function(zombie){
selectedZombie = zombie;
}
var delZombie = function(zombie){
if (selectedZombie.session == zombie.session) {
selectedZombie = null;
}
return null;
}
var getZombie = function(){
return selectedZombie;
}
//save the list of trees in the object
this.zombies_tree_lists = zombies_tree_lists;
// this is a helper class to create a zombie object from a JSON hash index
var zombieFactory = function(index, zombie_array){
this.zombieFactory = function(index, zombie_array){
text = "<img src='/ui/public/images/icons/"+escape(zombie_array[index]["browser_icon"])+"' style='padding-top:3px;' width='13px' height='13px'/> ";
text += "<img src='/ui/public/images/icons/"+escape(zombie_array[index]["os_icon"])+"' style='padding-top:3px;' width='13px' height='13px'/> ";
text += zombie_array[index]["ip"];
@@ -35,79 +21,44 @@ var ZombiesMgr = function(zombies_tree_lists) {
return new_zombie;
}
var updateZombies = function(){
Ext.Ajax.request({
url: '/ui/zombies/select/offline/simple.json',
method: 'POST',
success: function(response) {
var offline_zombies = Ext.util.JSON.decode(response.responseText);
for(tree_type in zombies_tree_lists) {
zombies = zombies_tree_lists[tree_type];
zombies.compareAndRemove(offline_zombies, false);
}
for(tree_type in zombies_tree_lists) {
zombies = zombies_tree_lists[tree_type];
for(var i in offline_zombies) {
var zombie = zombieFactory(i, offline_zombies);
if(tree_type=='requester') {
//TODO logic for the requester starts here
zombie['checked'] = true;
}
zombies.addZombie(zombie, false);
}
}
}
});
Ext.Ajax.request({
url: '/ui/zombies/select/online/simple.json',
method: 'POST',
success: function(response){
var online_zombies = Ext.util.JSON.decode(response.responseText);
for(tree_type in zombies_tree_lists) {
zombies = zombies_tree_lists[tree_type];
zombies.compareAndRemove(online_zombies, true);
}
for(tree_type in zombies_tree_lists) {
zombies = zombies_tree_lists[tree_type];
for(var i in online_zombies) {
var zombie = zombieFactory(i, online_zombies);
if(tree_type=='requester') {
//TODO logic for the requester starts here
zombie['checked'] = true;
}
zombies.addZombie(zombie, true);
}
}
for(tree_type in zombies_tree_lists) {
zombies = Ext.getCmp(zombies_tree_lists[tree_type].id);
if(zombies.online_zombies.childNodes.length > 0) {
//TODO: find a way to destroy folders that are empty
zombies.online_zombies.expand(true);
}
if(zombies.offline_zombies.childNodes.length > 0) {
zombies.offline_zombies.expand(true);
}
}
/*
* Update the hooked browser trees
* @param: {Literal Object} an object containing the list of offline and online hooked browsers.
* @param: {Literal Object} an object containing the list of rules from the distributed engine.
*/
this.updateZombies = function(zombies, rules){
var offline_zombies = zombies["offline"];
var online_zombies = zombies["online"];
for(tree_type in this.zombies_tree_lists) {
hooked_browsers_tree = this.zombies_tree_lists[tree_type];
//we compare and remove the hooked browsers from online and offline branches for each tree.
hooked_browsers_tree.compareAndRemove(offline_zombies, false);
hooked_browsers_tree.compareAndRemove(online_zombies, true);
//add an offline browser to the tree
for(var i in offline_zombies) {
var offline_hooked_browser = this.zombieFactory(i, offline_zombies);
hooked_browsers_tree.addZombie(offline_hooked_browser, false);
}
});
//add an online browser to the tree
for(var i in online_zombies) {
var online_hooked_browser = this.zombieFactory(i, online_zombies);
hooked_browsers_tree.addZombie(online_hooked_browser, true);
//TODO: add the rules here
}
//expand the online hooked browser tree lists
if(hooked_browsers_tree.online_zombies.childNodes.length > 0) {
hooked_browsers_tree.online_zombies.expand(true);
}
//expand the offline hooked browser tree lists
if(hooked_browsers_tree.offline_zombies.childNodes.length > 0) {
hooked_browsers_tree.offline_zombies.expand(true);
}
}
}
Ext.TaskMgr.start({
run: updateZombies,
interval: 8000
});
}
};