Changeset 5735
- Timestamp:
- 07/12/10 21:23:58 (2 months ago)
- Location:
- zion
- Files:
-
- 1 added
- 11 modified
-
branches/clann/bind/setup.py (modified) (1 diff)
-
branches/clann/bind/som.c (modified) (2 diffs)
-
branches/clann/bind/som.h (modified) (2 diffs)
-
branches/clann/bind/umit/clann/matrix.so (modified) (previous)
-
branches/clann/bind/umit/clann/metric.so (modified) (previous)
-
branches/clann/bind/umit/clann/som.so (modified) (previous)
-
branches/clann/code/som.c (modified) (3 diffs)
-
branches/clann/code/som.h (modified) (3 diffs)
-
trunk/umit/scan/zion/gui/ZionScanNotebookPage.py (modified) (13 diffs)
-
trunk/umit/zion/core/pmatrix.py (added)
-
trunk/umit/zion/core/zion.py (modified) (6 diffs)
-
trunk/umit/zion/db/db.sqlite (modified) (previous)
Legend:
- Unmodified
- Added
- Removed
-
zion/branches/clann/bind/setup.py
r5306 r5735 30 30 '../code/clann.o', 31 31 '../code/metric.o', 32 '../code/matrix.o'], 32 '../code/matrix.o', 33 '../code/function.o'], 33 34 extra_compile_args = ['-Wall', '-ggdb'], 34 35 sources = ['som.c']) -
zion/branches/clann/bind/som.c
r5306 r5735 112 112 * Convert output 113 113 */ 114 return PyCObject_FromVoidPtr(n, (void *) delete_matrix);114 return PyCObject_FromVoidPtr(n, NULL); 115 115 } 116 116 … … 142 142 143 143 som_training(a, n, i); 144 145 /** 146 * Convert output 147 */ 148 Py_INCREF(Py_None); 149 return Py_None; 150 } 151 152 PyObject* 153 classification(PyObject *self, PyObject *args) 154 { 155 /** 156 * Convert input 157 */ 158 PyObject *s = NULL, 159 *m = NULL; 160 unsigned int metric, method, i, j; 161 float limit_aux; 162 struct matrix ba, pa; 163 clann_type limit; 164 165 166 if (!PyArg_ParseTuple(args, "OOfI", &s, &m, &limit_aux, &method)) 167 return NULL; 168 169 limit = (clann_type) limit_aux; 170 171 /** 172 * Call the function 173 */ 174 struct matrix *b = (struct matrix *) PyCObject_AsVoidPtr(m); 175 struct som *a = (struct som *) PyCObject_AsVoidPtr(s); 176 177 if(method==1) 178 { 179 180 /* for hausdorff_limit only P and Pi are needed */ 181 matrix_initialize(&pa, a->grid.weights.rows, a->grid.weights.cols); 182 matrix_initialize(&ba, b->rows, b->cols - 1); 183 184 for (i = 0; i < a->grid.weights.rows; i++) 185 { 186 for (j = 0; j < a->grid.weights.cols - 1; j++) 187 *matrix_value(&pa, i, j) = *matrix_value(&a->grid.weights, i, j); 188 *matrix_value(&pa, i, a->grid.weights.cols-1) = a->grid.density[i]; 189 } 190 191 for (i = 0; i < b->rows; i++) 192 for (j = 0; j < b->cols - 1; j++) 193 *matrix_value(&ba, i, j) = *matrix_value(b, i, j); 194 195 metric = metric_hausdorff_limit_symmetric(&pa, &ba, limit); 196 } 197 else 198 { 199 matrix_initialize(&pa, a->grid.weights.rows, a->grid.weights.cols + 2); 200 201 for (i = 0; i < a->grid.weights.rows; i++) 202 for (j = 0; j < a->grid.weights.cols - 1; j++) 203 *matrix_value(&pa, i, j) = *matrix_value(&(a->grid.weights), i, j); 204 *matrix_value(&pa, i, a->grid.weights.cols-1) = a->grid.density[i]; 205 *matrix_value(&pa, i, a->grid.weights.cols) = a->grid.orientation[i]; 206 207 metric = metric_hausdorff_angle_symmetric(&pa, b, limit); 208 } 209 210 /** 211 * Convert output 212 */ 213 return Py_BuildValue("I", metric); 214 } 215 216 PyObject* 217 caracterization(PyObject *self, PyObject *args) 218 { 219 /** 220 * Convert input 221 */ 222 PyObject *s = NULL, 223 *m = NULL; 224 unsigned int i; 225 226 if (!PyArg_ParseTuple(args, "OOI", &s, &m, &i)) 227 return NULL; 228 229 /** 230 * Call the function 231 */ 232 struct matrix *n = (struct matrix *) PyCObject_AsVoidPtr(m); 233 struct som *a = (struct som *) PyCObject_AsVoidPtr(s); 234 235 if (i < 1) 236 { 237 PyErr_SetString(PyExc_IndexError, 238 "number of iterations must be positive"); 239 return NULL; 240 } 241 242 som_caracterization(a, n, i); 144 243 145 244 /** -
zion/branches/clann/bind/som.h
r5306 r5735 63 63 * 64 64 */ 65 static char caracterization__doc__[] = "Start training SOM and fingerpriting"; 66 67 static PyObject* 68 caracterization(PyObject *self, PyObject *args); 69 70 /** 71 * 72 */ 73 static char classification__doc__[] = "Database matching"; 74 75 static PyObject* 76 classification(PyObject *self, PyObject *args); 77 78 /** 79 * 80 */ 65 81 static PyMethodDef SOMMethods[] = 66 82 { … … 69 85 {"get", get, METH_VARARGS, get__doc__}, 70 86 {"train", train, METH_VARARGS, train__doc__}, 87 {"caracterization", caracterization, METH_VARARGS, caracterization__doc__}, 88 {"classification", classification, METH_VARARGS, classification__doc__}, 71 89 {NULL, NULL, 0, NULL} 72 90 }; -
zion/branches/clann/code/som.c
r5307 r5735 21 21 #include "som.h" 22 22 23 clann_type *z, 24 *o, 25 sz = 0.07, 26 so = 0.5, 27 r = 0.07; 28 23 29 24 30 void … … 76 82 while (ann->epoch < epochs) 77 83 { 84 printf("epoch: %d\n",ann->epoch); 78 85 clann_shuffle(mess, x->rows); 79 86 … … 192 199 return 0; 193 200 } 201 202 void 203 som_caracterization(struct som *ann, 204 struct matrix *x, 205 unsigned int epochs) 206 { 207 clann_type n_scale[2], z_scale[2]; 208 unsigned int i, j; 209 clann_type d, cx, cy, *w, *t, *p, *a, *b; 210 211 ann->grid.orientation = malloc(sizeof(clann_type) * ann->grid.weights.rows); 212 ann->grid.density = malloc(sizeof(clann_type) * ann->grid.weights.rows); 213 214 for (i = 0; i < ann->grid.weights.rows; i++) 215 { 216 ann->grid.orientation[i] = 0; 217 ann->grid.density[i] = 0; 218 } 219 220 n_scale[MIN] = (clann_type) -1.0; 221 n_scale[MAX] = (clann_type) 1.0; 222 223 printf("Training SOM\n"); 224 som_training(ann, x, epochs); 225 226 t = malloc(sizeof(clann_type) * ann->grid.weights.rows); 227 228 for (i = 0; i < ann->grid.weights.rows; i++) 229 t[i] = 0; 230 231 /** 232 * Density 233 */ 234 printf("Calculating density\n"); 235 for (i = 0; i < x->rows; i++) 236 { 237 p = matrix_value(x, i, 0); 238 239 z_scale[MIN] = (clann_type) INT_MAX; 240 z_scale[MAX] = (clann_type) - INT_MAX; 241 242 for (j = 0; j < ann->grid.weights.rows; j++) 243 { 244 w = matrix_value(&ann->grid.weights, j, 0); 245 246 d = CLANN_POW(metric_euclidean(p, w, 2), 2.0); 247 d = function_green_gaussian(&sz, &d); 248 249 t[j] += d; 250 251 if (t[j] < z_scale[MIN]) 252 z_scale[MIN] = t[j]; 253 254 if (t[j] > z_scale[MAX]) 255 z_scale[MAX] = t[j]; 256 } 257 258 for (j = 0; j < ann->grid.weights.rows; j++) 259 ann->grid.density[j] = metric_scale(t[j], z_scale, n_scale); 260 } 261 262 /** 263 * Orientation 264 */ 265 printf("Calculating orientation\n"); 266 for (j = 0; j < ann->grid.weights.rows; j++) 267 { 268 w = matrix_value(&ann->grid.weights, j, 0); 269 270 cx = 0; 271 cy = 0; 272 273 for (i = 0; i < x->rows - 1; i++) 274 { 275 a = matrix_value(x, i, 0); 276 b = matrix_value(x, i + 1, 0); 277 278 d = CLANN_POW(metric_euclidean(a, w, 2), 2.0); 279 d = function_green_gaussian(&so, &d); 280 281 cx += (b[X] - a[X]) * d; 282 cy += (b[Y] - a[Y]) * d; 283 } 284 285 ann->grid.orientation[j] = CLANN_ATAN2(cy, cx); 286 } 287 288 /** 289 * Normalization 290 */ 291 printf("Normalization\n"); 292 clann_type x_scale[2], y_scale[2]; 293 294 x_scale[MIN] = (clann_type) INT_MAX; 295 x_scale[MAX] = (clann_type) - INT_MAX; 296 y_scale[MIN] = (clann_type) INT_MAX; 297 y_scale[MAX] = (clann_type) - INT_MAX; 298 299 for (j = 0; j < ann->grid.weights.rows; j++) 300 { 301 w = matrix_value(&ann->grid.weights, j, 0); 302 303 //if (z[j] < MIN_DENSITY) 304 // continue; 305 306 if (w[X] < x_scale[MIN]) 307 x_scale[MIN] = w[X]; 308 309 if (w[X] > x_scale[MAX]) 310 x_scale[MAX] = w[X]; 311 312 if (w[Y] < y_scale[MIN]) 313 y_scale[MIN] = w[Y]; 314 315 if (w[Y] > y_scale[MAX]) 316 y_scale[MAX] = w[Y]; 317 } 318 319 for (j = 0; j < ann->grid.weights.rows; j++) 320 { 321 w = matrix_value(&ann->grid.weights, j, 0); 322 323 w[X] = metric_scale(w[X], x_scale, n_scale); 324 w[Y] = metric_scale(w[Y], y_scale, n_scale); 325 } 326 } -
zion/branches/clann/code/som.h
r5307 r5735 32 32 #include "reader.h" 33 33 #include "clann.h" 34 #include "function.h" 35 36 #define MAX 0 37 #define MIN 1 38 39 #define X 0 40 #define Y 1 41 42 #define MIN_DENSITY -0.8 34 43 35 44 … … 42 51 unsigned int y_len; 43 52 struct matrix weights; 53 clann_type *density; 54 clann_type *orientation; 44 55 }; 45 56 … … 83 94 * 84 95 */ 96 inline void 97 som_caracterization(struct som *ann, 98 struct matrix *x, 99 unsigned int epochs); 100 101 /** 102 * 103 */ 85 104 inline void 86 105 som_adjust_weights(struct som *ann, -
zion/trunk/umit/scan/zion/gui/ZionScanNotebookPage.py
r5716 r5735 23 23 import gobject 24 24 import netifaces 25 import thread 25 26 26 27 from higwidgets.higframe import HIGFrameRNet … … 43 44 44 45 ICON_DIR = 'share/pixmaps/zion/' 46 ICON_DIR_UMIT = 'share/pixmaps/umit/' 45 47 46 48 PIXBUF_FIREWALL = gtk.gdk.pixbuf_new_from_file(ICON_DIR + 'firewall.png') 47 49 PIXBUF_SYNPROXY = gtk.gdk.pixbuf_new_from_file(ICON_DIR + 'shield.png') 48 50 PIXBUF_HONEYD = gtk.gdk.pixbuf_new_from_file(ICON_DIR + 'honey.png') 51 PIXBUF_UNKNOWN = gtk.gdk.pixbuf_new_from_file(ICON_DIR_UMIT + 'unknown_32.png') 52 53 SCANNING = _("Scanning") 49 54 50 55 class ZionHostsView(gtk.Notebook): … … 71 76 72 77 self.append_page(self.__scans_page, gtk.Label(_('Scans'))) 78 self.append_page(self.__ident_page, gtk.Label(_('Identification'))) 73 79 self.append_page(self.__ports_page, gtk.Label(_('Ports'))) 74 self.append_page(self.__ident_page, gtk.Label(_('Identification')))75 80 76 81 self.__ports_page.add(self.open_ports) … … 83 88 """ 84 89 return self.__ident_page 90 91 def get_scans_page(self): 92 """ 93 """ 94 return self.__scans_page 85 95 86 96 class ZionScansPage(HIGVBox): … … 91 101 """ 92 102 HIGVBox.__init__(self) 103 104 # Creating widgets 105 self.__create_widgets() 106 107 # Setting scrolled window 108 self.__set_scrolled_window() 109 110 # Setting text view 111 self.__set_text_view() 112 113 # Getting text buffer 114 self.text_buffer = self.text_view.get_buffer() 115 116 # Adding widgets to the VPaned 117 self._pack_expand_fill(self.scrolled) 118 119 def __create_widgets (self): 120 # Creating widgets 121 self.scrolled = gtk.ScrolledWindow() 122 self.text_view = gtk.TextView() 123 124 def __set_scrolled_window(self): 125 # By default the vertical scroller remains at bottom 126 self._scroll_at_bottom = True 127 128 # Seting scrolled window 129 self.scrolled.set_border_width(5) 130 self.scrolled.add(self.text_view) 131 self.scrolled.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) 132 133 def __set_text_view(self): 134 self.text_view.set_wrap_mode(gtk.WRAP_WORD) 135 self.text_view.set_editable(False) 136 137 def write(self, text): 138 self.text_buffer.insert(self.text_buffer.get_end_iter(), text) 93 139 94 140 class ZionPortsPage(HIGVBox): … … 149 195 self.__cell_pixbuf = gtk.CellRendererPixbuf() 150 196 151 self.__hosts_store = gtk.ListStore(gtk.gdk.Pixbuf, 152 gobject.TYPE_STRING) 197 self.__hosts_store = gtk.ListStore(gtk.gdk.Pixbuf, str) 153 198 154 199 self.__hosts_treeview = gtk.TreeView(self.__hosts_store) … … 164 209 text=1) 165 210 166 """self.__hosts_store.append([PIXBUF_FIREWALL,167 'firewall.example.com\n192.0.2.1'])168 self.__hosts_store.append([PIXBUF_SYNPROXY,169 'synproxy.example.com\n192.0.2.2'])170 self.__hosts_store.append([PIXBUF_HONEYD,171 'honeyd.example.com\n192.0.2.3'])"""172 173 211 self.__column_type.set_reorderable(True) 174 212 self.__column_type.set_resizable(False) … … 208 246 """ 209 247 """ 210 self.__hosts_store = gtk.ListStore(gtk.gdk.Pixbuf, 211 gobject.TYPE_STRING) 248 for i in range(len(self.__hosts_store)): 249 iter = self.__hosts_store.get_iter_root() 250 del(self.__hosts_store[iter]) 212 251 213 252 def add_host(self, name, host_type=None): … … 215 254 """ 216 255 self.__hosts_store.append([host_type,name]) 256 pass 217 257 218 258 class ZionResultsPage(gtk.HPaned): … … 355 395 356 396 self.result.clear_port_list() 357 397 398 # clear previous hosts in the list 399 self.result.get_hosts_list().clear_hosts() 400 401 # verify address to scan 358 402 if address.recognize(self.target) == address.Unknown: 359 403 l = probe.get_addr_from_name(self.target) … … 361 405 try: 362 406 z.append_target(host.Host(i, self.target)) 407 host_str = '%s\n%s' % (i, self.target) 363 408 except: 364 409 print "Unimplemented support to address: %s." % i 365 410 else: 366 z.append_target(host.Host(self.target)) 367 411 z.append_target(host.Host(self.target)) 412 self.result.get_hosts_list().add_host(self.target) 413 414 self.result.get_hosts_list().add_host(host_str) 415 416 # configure zion options 368 417 device = get_default_device() 369 418 saddr = get_ip_address(device) … … 372 421 z.get_option_object().add("--forge-addr",saddr) 373 422 z.run() 423 #z.start() 374 424 375 425 # update host information … … 560 610 561 611 # TODO: read device from options 562 device = " wlan0"612 device = "eth0" 563 613 #device = netifaces.interfaces()[0] 564 614 return device -
zion/trunk/umit/zion/core/zion.py
r5714 r5735 24 24 import random 25 25 import time 26 import thread, threading 27 import sqlite3 28 import sys 29 import json 30 import cPickle 26 31 from math import sqrt 27 32 28 33 from umit.clann import som, matrix 29 from umit.zion.core import options, host 34 from umit.zion.core import options, host, pmatrix 30 35 from umit.zion.scan import sniff, portscan, forge 31 36 32 37 FORGE_FILTER = 'src host %s and src port %s and dst host %s and dst port %s' 33 AMOUNT_OS_DETECTION = 200038 AMOUNT_OS_DETECTION = 50 34 39 AMOUNT_HONEYD_DETECTION = 25 35 40 SEND_INTERVAL = 0.1 36 41 SYNPROXY_INTERVAL = 1 37 38 class Zion(object): 42 METHOD_ALPHA = 1 43 METHOD_BETA = 2 44 EPOCHS = 500#1800 45 ALPHA_LIMIT = 0.1 46 47 class Zion(threading.Thread): 39 48 """ 40 49 """ … … 46 55 self.__capture_result = [] 47 56 self.__attractors = [] 57 threading.Thread.__init__ (self) 48 58 49 59 def get_option_object(self): … … 173 183 """ 174 184 """ 175 self.synproxy_detection(self.__target[0])176 185 if self.__option.has(options.OPTION_HELP): 177 186 … … 215 224 print 'Creating attractors' 216 225 self.__classification(Rt) 226 227 print 'Matching' 228 return self.__matching() 217 229 218 230 elif self.__option.has(options.OPTION_CAPTURE): … … 300 312 isn3 = self.__capture_result[0][1] 301 313 302 print 'isns:'303 print isn1304 print isn2305 print isn3306 307 314 if isn1!=isn2 and isn1==isn3: 308 315 return True … … 351 358 ratio = 2/(max_val-min_val) 352 359 353 self.__som = som.new( 10,(30,30))360 self.__som = som.new(2,(30,30)) 354 361 self.__matrix = matrix.new(len(Rt)-1,2) 355 362 356 363 for i in range(len(Rt)-1): 357 x = (Rt[i+1]-min_val)*ratio-1358 y = (Rt[i]-min_val)*ratio-1364 x = Rt[i+1] 365 y = Rt[i] 359 366 self.__attractors.append((x, y)) 360 367 matrix.set(self.__matrix, i, 0, x) 361 368 matrix.set(self.__matrix, i, 1, y) 362 369 363 # TODO: confirm how train works 364 som.train(self.__som, self.__matrix, 1800) 365 370 som.caracterization(self.__som, self.__matrix, EPOCHS) 371 372 def __matching(self): 373 """ 374 Match fingerprint with database 375 """ 376 dmin = sys.maxint 377 id_min = None 378 379 conn = sqlite3.connect('umit/zion/db/db.sqlite') 380 c = conn.cursor() 381 c.execute('SELECT software.pk, s_attractor.fp FROM software INNER JOIN fingerprint ON software.pk = fingerprint.fk_software INNER JOIN s_attractor ON s_attractor.pk = fingerprint.fk_sig1') 382 383 for fingerprint in c: 384 attractor = cPickle.loads(str(fingerprint[1])) 385 base = attractor.convert() 386 d = som.classification(self.__som, base, ALPHA_LIMIT, METHOD_ALPHA) 387 if d < dmin: 388 dmin = d 389 id_min = fingerprint[0] 390 391 details = None 392 if id_min!=None: 393 for vendor_name, os_name, os_version in c.execute('SELECT vendor.name, name.name, name.version FROM software INNER JOIN vendor ON software.fk_vendor = vendor.pk INNER JOIN name ON software.fk_name = name.pk WHERE software.pk = ?',(id_min,)): 394 details = {'vendor_name': vendor_name, 'os_name': os_name, 'os_version': os_version, 'metric': dmin} 395 print 'Vendor name: %s\nOS name: %s\nOS version: %s\nMetric: %d' % (vendor_name, os_name, os_version, dmin) 396 else: 397 print 'no fingerprints available in database' 398 399 return details 366 400 367 401 def get_attractors(self): 368 """ 369 """ 402 """ Return the list of attractors. """ 370 403 return self.__attractors 404
