Changeset 5735

Show
Ignore:
Timestamp:
07/12/10 21:23:58 (2 months ago)
Author:
diogo
Message:

zion: fingerprint matching with database

Location:
zion
Files:
1 added
11 modified

Legend:

Unmodified
Added
Removed
  • zion/branches/clann/bind/setup.py

    r5306 r5735  
    3030                                        '../code/clann.o', 
    3131                                        '../code/metric.o', 
    32                                         '../code/matrix.o'], 
     32                                        '../code/matrix.o', 
     33                                        '../code/function.o'], 
    3334                       extra_compile_args = ['-Wall', '-ggdb'], 
    3435                       sources = ['som.c']) 
  • zion/branches/clann/bind/som.c

    r5306 r5735  
    112112     * Convert output 
    113113     */ 
    114     return PyCObject_FromVoidPtr(n, (void *) delete_matrix); 
     114    return PyCObject_FromVoidPtr(n, NULL); 
    115115} 
    116116 
     
    142142 
    143143    som_training(a, n, i); 
     144 
     145    /** 
     146     * Convert output 
     147     */ 
     148    Py_INCREF(Py_None); 
     149    return Py_None; 
     150} 
     151 
     152PyObject* 
     153classification(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 
     216PyObject* 
     217caracterization(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); 
    144243 
    145244    /** 
  • zion/branches/clann/bind/som.h

    r5306 r5735  
    6363 * 
    6464 */ 
     65static char caracterization__doc__[] = "Start training SOM and fingerpriting"; 
     66 
     67static PyObject* 
     68caracterization(PyObject *self, PyObject *args); 
     69 
     70/** 
     71 * 
     72 */ 
     73static char classification__doc__[] = "Database matching"; 
     74 
     75static PyObject* 
     76classification(PyObject *self, PyObject *args); 
     77 
     78/** 
     79 * 
     80 */ 
    6581static PyMethodDef SOMMethods[] = 
    6682{ 
     
    6985    {"get", get, METH_VARARGS, get__doc__}, 
    7086    {"train", train, METH_VARARGS, train__doc__}, 
     87    {"caracterization", caracterization, METH_VARARGS, caracterization__doc__}, 
     88    {"classification", classification, METH_VARARGS, classification__doc__}, 
    7189    {NULL, NULL, 0, NULL} 
    7290}; 
  • zion/branches/clann/code/som.c

    r5307 r5735  
    2121#include "som.h" 
    2222 
     23clann_type *z, 
     24           *o, 
     25           sz = 0.07, 
     26           so = 0.5, 
     27           r = 0.07; 
     28 
    2329 
    2430void 
     
    7682    while (ann->epoch < epochs) 
    7783    { 
     84        printf("epoch: %d\n",ann->epoch); 
    7885        clann_shuffle(mess, x->rows); 
    7986 
     
    192199    return 0; 
    193200} 
     201 
     202void 
     203som_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  
    3232#include "reader.h" 
    3333#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 
    3443 
    3544 
     
    4251    unsigned int y_len; 
    4352    struct matrix weights; 
     53    clann_type *density; 
     54    clann_type *orientation; 
    4455}; 
    4556 
     
    8394 * 
    8495 */ 
     96inline void 
     97som_caracterization(struct som *ann, 
     98                    struct matrix *x, 
     99                    unsigned int epochs); 
     100 
     101/** 
     102 * 
     103 */ 
    85104inline void  
    86105som_adjust_weights(struct som *ann, 
  • zion/trunk/umit/scan/zion/gui/ZionScanNotebookPage.py

    r5716 r5735  
    2323import gobject 
    2424import netifaces 
     25import thread 
    2526 
    2627from higwidgets.higframe import HIGFrameRNet 
     
    4344 
    4445ICON_DIR = 'share/pixmaps/zion/' 
     46ICON_DIR_UMIT = 'share/pixmaps/umit/' 
    4547 
    4648PIXBUF_FIREWALL = gtk.gdk.pixbuf_new_from_file(ICON_DIR + 'firewall.png') 
    4749PIXBUF_SYNPROXY = gtk.gdk.pixbuf_new_from_file(ICON_DIR + 'shield.png') 
    4850PIXBUF_HONEYD = gtk.gdk.pixbuf_new_from_file(ICON_DIR + 'honey.png') 
     51PIXBUF_UNKNOWN = gtk.gdk.pixbuf_new_from_file(ICON_DIR_UMIT + 'unknown_32.png') 
     52 
     53SCANNING = _("Scanning") 
    4954 
    5055class ZionHostsView(gtk.Notebook): 
     
    7176 
    7277        self.append_page(self.__scans_page, gtk.Label(_('Scans'))) 
     78        self.append_page(self.__ident_page, gtk.Label(_('Identification'))) 
    7379        self.append_page(self.__ports_page, gtk.Label(_('Ports'))) 
    74         self.append_page(self.__ident_page, gtk.Label(_('Identification'))) 
    7580         
    7681        self.__ports_page.add(self.open_ports) 
     
    8388        """ 
    8489        return self.__ident_page 
     90     
     91    def get_scans_page(self): 
     92        """ 
     93        """ 
     94        return self.__scans_page 
    8595 
    8696class ZionScansPage(HIGVBox): 
     
    91101        """ 
    92102        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) 
    93139 
    94140class ZionPortsPage(HIGVBox): 
     
    149195        self.__cell_pixbuf = gtk.CellRendererPixbuf() 
    150196 
    151         self.__hosts_store = gtk.ListStore(gtk.gdk.Pixbuf, 
    152                                            gobject.TYPE_STRING) 
     197        self.__hosts_store = gtk.ListStore(gtk.gdk.Pixbuf, str) 
    153198 
    154199        self.__hosts_treeview = gtk.TreeView(self.__hosts_store) 
     
    164209                                                text=1) 
    165210 
    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  
    173211        self.__column_type.set_reorderable(True) 
    174212        self.__column_type.set_resizable(False) 
     
    208246        """ 
    209247        """ 
    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]) 
    212251         
    213252    def add_host(self, name, host_type=None): 
     
    215254        """ 
    216255        self.__hosts_store.append([host_type,name]) 
     256        pass 
    217257 
    218258class ZionResultsPage(gtk.HPaned): 
     
    355395         
    356396        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 
    358402        if address.recognize(self.target) == address.Unknown: 
    359403            l = probe.get_addr_from_name(self.target) 
     
    361405                try: 
    362406                    z.append_target(host.Host(i, self.target)) 
     407                    host_str = '%s\n%s' % (i, self.target) 
    363408                except: 
    364409                    print "Unimplemented support to address: %s." % i 
    365410        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 
    368417        device = get_default_device() 
    369418        saddr = get_ip_address(device) 
     
    372421        z.get_option_object().add("--forge-addr",saddr) 
    373422        z.run() 
     423        #z.start() 
    374424         
    375425        # update host information 
     
    560610 
    561611    # TODO: read device from options 
    562     device = "wlan0" 
     612    device = "eth0" 
    563613    #device = netifaces.interfaces()[0] 
    564614    return device 
  • zion/trunk/umit/zion/core/zion.py

    r5714 r5735  
    2424import random 
    2525import time 
     26import thread, threading 
     27import sqlite3 
     28import sys 
     29import json 
     30import cPickle 
    2631from math import sqrt 
    2732 
    2833from umit.clann import som, matrix 
    29 from umit.zion.core import options, host 
     34from umit.zion.core import options, host, pmatrix 
    3035from umit.zion.scan import sniff, portscan, forge 
    3136 
    3237FORGE_FILTER = 'src host %s and src port %s and dst host %s and dst port %s' 
    33 AMOUNT_OS_DETECTION = 2000 
     38AMOUNT_OS_DETECTION = 50 
    3439AMOUNT_HONEYD_DETECTION = 25 
    3540SEND_INTERVAL = 0.1 
    3641SYNPROXY_INTERVAL = 1 
    37  
    38 class Zion(object): 
     42METHOD_ALPHA = 1 
     43METHOD_BETA = 2 
     44EPOCHS = 500#1800 
     45ALPHA_LIMIT = 0.1 
     46 
     47class Zion(threading.Thread): 
    3948    """ 
    4049    """ 
     
    4655        self.__capture_result = [] 
    4756        self.__attractors = [] 
     57        threading.Thread.__init__ (self) 
    4858 
    4959    def get_option_object(self): 
     
    173183        """ 
    174184        """ 
    175         self.synproxy_detection(self.__target[0]) 
    176185        if self.__option.has(options.OPTION_HELP): 
    177186 
     
    215224            print 'Creating attractors' 
    216225            self.__classification(Rt) 
     226             
     227            print 'Matching' 
     228            return self.__matching() 
    217229                 
    218230        elif self.__option.has(options.OPTION_CAPTURE): 
     
    300312        isn3 = self.__capture_result[0][1] 
    301313         
    302         print 'isns:' 
    303         print isn1 
    304         print isn2 
    305         print isn3 
    306          
    307314        if isn1!=isn2 and isn1==isn3: 
    308315            return True 
     
    351358        ratio = 2/(max_val-min_val) 
    352359         
    353         self.__som = som.new(10,(30,30)) 
     360        self.__som = som.new(2,(30,30)) 
    354361        self.__matrix = matrix.new(len(Rt)-1,2) 
    355          
     362                 
    356363        for i in range(len(Rt)-1): 
    357             x = (Rt[i+1]-min_val)*ratio-1 
    358             y = (Rt[i]-min_val)*ratio-1 
     364            x = Rt[i+1] 
     365            y = Rt[i] 
    359366            self.__attractors.append((x, y)) 
    360367            matrix.set(self.__matrix, i, 0, x) 
    361368            matrix.set(self.__matrix, i, 1, y) 
    362369 
    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 
    366400         
    367401    def get_attractors(self): 
    368         """ 
    369         """ 
     402        """ Return the list of attractors. """ 
    370403        return self.__attractors 
     404