root/branch/ggpolo/umitDB/Store.py @ 1400

Revision 1400, 15.6 kB (checked in by ggpolo, 6 years ago)

Fixed missing import in umit, fixed some other bug that could case to start two Scheduler instances

Line 
1# Copyright (C) 2007 Insecure.Com LLC.
2#
3# Author:  Guilherme Polo <ggpolo@gmail.com>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18# USA
19
20from umitDB.Utils import empty
21from umitDB.Utils import debug
22   
23"""
24Missing methods for:
25    Traceroute insertion.
26"""
27
28class RawStore:
29    """
30    Store data into database.
31    """
32   
33    def __init__(self, conn, cursor):
34        """
35        Expects a conn and cursor from database connection.   
36        """
37        self.conn = conn
38        self.cursor = cursor
39
40
41    def insert_scan_db(self, scan_d):
42        """
43        Creates new record in scan with data from scan dict.
44        """
45        debug("Inserting new scan into database")
46
47        self.cursor.execute("INSERT INTO scan (args, start, startstr, finish, \
48                    finishstr, xmloutputversion, xmloutput, verbose, \
49                    debugging, hosts_up, hosts_down, fk_scanner) VALUES \
50                    (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (scan_d["args"],
51                    scan_d["start"], scan_d["startstr"], scan_d["finish"],
52                    scan_d["finishstr"], scan_d["xmloutputversion"],
53                    scan_d["xmloutput"], scan_d["verbose"], 
54                    scan_d["debugging"], scan_d["hosts_up"], 
55                    scan_d["hosts_down"], scan_d["scanner"]))
56
57        self.conn.commit()
58
59   
60    def insert_scaninfo_db(self, scaninfo):
61        """
62        Creates new record in scaninfo with data from scaninfo dict.
63        """
64        debug("Inserting new scaninfo into database")
65
66        self.cursor.execute("INSERT INTO scaninfo (numservices, services, \
67                        fk_scan, fk_scan_type, fk_protocol) VALUES \
68                        (?, ?, ?, ?, ?)", (scaninfo["numservices"], 
69                        scaninfo["services"], scaninfo["fk_scan"],
70                        scaninfo["type"], scaninfo["protocol"]))
71        self.conn.commit()
72
73
74    def insert_scan_type_db(self, scan_name):
75        """
76        Insert new record in scan_type.
77        """
78        debug("Inserting new scan_type into database")
79       
80        self.cursor.execute("INSERT INTO scan_type (name) VALUES \
81                                 (?)", (scan_name, ))
82        self.conn.commit()
83
84
85    def insert_scanner_db(self, scanner_name, scanner_version):
86        """
87        Creates new record in scanner.
88        """
89        debug("Inserting new scanner into database")
90       
91        self.cursor.execute("INSERT INTO scanner (name, version) VALUES \
92                                 (?, ?)", (scanner_name, scanner_version))
93        self.conn.commit()
94       
95
96    def insert_port_db(self, portid, service_info_id, protocol_id, 
97                                      port_state_id):
98        """
99        Creates new record in port.
100        """
101        debug("Inserting new port into database")
102       
103        self.cursor.execute("INSERT INTO port (portid, fk_service_info, \
104                       fk_protocol, fk_port_state) VALUES (?, ?, ?, ?)",
105                       (portid, service_info_id, protocol_id,
106                        port_state_id))
107        self.conn.commit()
108           
109   
110    def insert_port_state_db(self, port_state):
111        """
112        Creates new record in port_state.
113        """
114        debug("Inserting new port_state into database")
115       
116        self.cursor.execute("INSERT INTO port_state (state) VALUES \
117                                (?)", (port_state, ))
118        self.conn.commit()
119   
120   
121    def insert_protocol_db(self, protocol):
122        """
123        Creates new record in protocol.
124        """
125        debug("Inserting new protocol into database")
126       
127        self.cursor.execute("INSERT INTO protocol (name) VALUES \
128                                 (?)", (protocol, ))
129        self.conn.commit()
130       
131   
132    def insert_host_port_db(self, fk_host, fk_port):
133        """
134        Creates new record in _host_port based on fk_host and fk_port.
135        """
136        debug("Inserting new _host_port into database")
137       
138        self.cursor.execute("INSERT INTO _host_port (fk_host, fk_port) \
139                    VALUES (?, ?)", (fk_host, fk_port))
140        self.conn.commit()
141       
142
143    def insert_extraports_db(self, count, fk_host, fk_port_state):
144        """
145        Creates new record in extraports.
146        """
147        debug("Inserting new extraports into database")
148       
149        self.cursor.execute("INSERT INTO extraports (count, fk_host, \
150                            fk_port_state) VALUES (?, ?, ?)",
151                            (count, fk_host, fk_port_state))
152        self.conn.commit()
153
154
155    def insert_portused_db(self, portid, fk_port_state, fk_protocol, 
156                           fk_host):
157        """
158        Create new record in portused.
159        """
160        debug("Inserting new portused into database")
161       
162        self.cursor.execute("INSERT INTO portused (portid, fk_port_state, \
163                        fk_protocol, fk_host) VALUES (?, ?, ?, ?)",
164                        (portid, fk_port_state, fk_protocol, fk_host))
165        self.conn.commit()
166
167
168    def insert_osclass_db(self, osclass_accuracy, fk_osgen, fk_osfamily, 
169                          fk_osvendor, fk_ostype, fk_host):
170        """
171        Create new record in osclass.
172        """
173        debug("Inserting new osclass into database")
174       
175        self.cursor.execute("INSERT INTO osclass (accuracy, fk_osgen, \
176                        fk_osfamily, fk_osvendor, fk_ostype, fk_host) VALUES \
177                        (?, ?, ?, ?, ?, ?)", (osclass_accuracy, fk_osgen,
178                        fk_osfamily, fk_osvendor, fk_ostype, fk_host))
179        self.conn.commit()
180
181   
182    def insert_osmatch_db(self, host, osmatch):
183        """
184        Create new record in osmatch with data from osmatch dict.
185        """
186        debug("Inserting new osmatch into database")
187       
188        osmatch["line"] = empty() # ToFix: Parser isnt storing this
189        self.cursor.execute("INSERT INTO osmatch (name, accuracy, line, \
190                    fk_host) VALUES (?, ?, ?, ?)", (osmatch["name"], 
191                    osmatch["accuracy"], osmatch["line"], host))
192        self.conn.commit()
193       
194       
195    def insert_osgen_db(self, osgen):
196        """
197        Creates new record in osgen.
198        """
199        debug("Inserting new osgen into database")
200       
201        self.cursor.execute("INSERT INTO osgen (gen) VALUES (?)", (osgen, ))
202        self.conn.commit()
203       
204    def insert_osfamily_db(self, osfamily):
205        """
206        Creates new record in osfamily.
207        """
208        debug("Inserting new osfamily into database")
209       
210        self.cursor.execute("INSERT INTO osfamily (family) VALUES (?)", 
211                        (osfamily, ))
212        self.conn.commit()
213           
214
215    def insert_osvendor_db(self, osvendor):
216        """
217        Creates new record in osvendor.
218        """
219        debug("Inserting new osvendor into database")
220       
221        self.cursor.execute("INSERT INTO osvendor (vendor) VALUES (?)", 
222                        (osvendor, ))
223        self.conn.commit()
224
225   
226    def insert_ostype_db(self, ostype):
227        """
228        Creates new record in ostype.
229        """
230        debug("Inserting new ostype into database")
231       
232        self.cursor.execute("INSERT INTO ostype (type) VALUES (?)", 
233                        (ostype, ))
234        self.conn.commit()
235       
236
237    def insert_host_db(self, host_d):
238        """
239        Create new record in host with data from host dict.
240        """
241        debug("Inserting new host into database")
242
243        self.cursor.execute("INSERT INTO host (distance, fk_scan, \
244                     fk_host_state) VALUES (?, ?, ?)", (host_d["distance"],
245                     host_d["fk_scan"], host_d["fk_host_state"]))
246        self.conn.commit()
247
248
249    def insert_host_address_db(self, fk_host, fk_address):
250        """
251        Creates new record in _host_address.
252        """
253        debug("Inserting new _host_address into database")
254       
255        self.cursor.execute("INSERT INTO _host_address (fk_host, fk_address) \
256                            VALUES (?, ?)", (fk_host, fk_address))
257        self.conn.commit()
258
259
260    def insert_host_hostname_db(self, fk_host, fk_hostname):
261        """
262        Creates new record in _host_hostname.
263        """
264        debug("Inserting new _host_hostname into database")
265       
266        self.cursor.execute("INSERT INTO _host_hostname (fk_host, \
267                                fk_hostname) VALUES (?, ?)", (fk_host, 
268                                fk_hostname))
269        self.conn.commit()
270   
271   
272    def insert_hostname_db(self, hostname):
273        """
274        Insert new record in hostnamed based on data from hostname.
275        """
276        debug("Inserting new hostname into database")
277       
278        self.cursor.execute("INSERT INTO hostname (type, name) VALUES \
279                                 (?, ?)", (hostname["hostname_type"], 
280                                           hostname["hostname"]))
281        self.conn.commit()
282   
283
284    def insert_fingerprint_info_db(self, fp_d):
285        """
286        Creates new record in fingerprint_info with data from fp_d.
287        """
288        debug("Inserting new fingerprint information for host into database")
289
290        columns = ( "uptime", "lastboot", "tcp_sequence_class", 
291                    "tcp_sequence_index", "tcp_sequence_value",
292                    "tcp_sequence_difficulty", "tcp_ts_sequence_class",
293                    "tcp_ts_sequence_value", "ip_id_sequence_class",
294                    "ip_id_sequence_value", "fk_host" )
295
296        data = [fp_d[column] for column in columns]
297
298        self.cursor.execute("INSERT INTO fingerprint_info (uptime, lastboot, \
299                tcp_sequence_class, tcp_sequence_index, tcp_sequence_value, \
300                tcp_sequence_difficulty, tcp_ts_sequence_class, \
301                tcp_ts_sequence_value, ip_id_sequence_class, \
302                ip_id_sequence_value, fk_host) VALUES (?, ?, ?, ?, ?, ?, ?, \
303                ?, ?, ?, ?)", (data))
304        self.conn.commit()
305
306   
307    def insert_service_name_db(self, service_name):
308        """
309        Creates new record in service_name.
310        """       
311        debug("Inserting new service_name into database")
312       
313        self.cursor.execute("INSERT INTO service_name (name) VALUES \
314                            (?)", (service_name, ))
315        self.conn.commit()
316       
317       
318    def insert_service_info_db(self, service_data):
319        """
320        Creates new record in service_info based on service_data
321        """
322        debug("Inserting new service_info into database")
323       
324        self.cursor.execute("INSERT INTO service_info (product, version, \
325                    extrainfo, method, conf, fk_service_name) VALUES (?, ?, ?,\
326                    ?, ?, ?)", service_data)
327        self.conn.commit()
328       
329   
330    def insert_address_db(self, address_addr, address_type, vendor):
331        """
332        Creates new record on address.
333        """
334        debug("Inserting new address into database")
335       
336        self.cursor.execute("INSERT INTO address (address, type, \
337                        fk_vendor) VALUES (?, ?, ?)", (address_addr,
338                        address_type, vendor))
339        self.conn.commit()
340
341   
342    def insert_vendor_db(self, vendor_name):
343        """
344        Creates new record in  vendor.
345        """
346        debug("Inserting new vendor into database")
347       
348        self.cursor.execute("INSERT INTO vendor (name) \
349                                         VALUES (?)", (vendor_name, ))
350        self.conn.commit()
351
352       
353    def insert_host_state_db(self, host_state):
354        """
355        Creates new record in host_state.
356        """
357        debug("Inserting new host_state into database")
358       
359        self.cursor.execute("INSERT INTO host_state (state) \
360                        VALUES (?)", (host_state, ))
361        self.conn.commit()
362
363
364    def insert_tcp_sequence_db(self, tcpseq_dict):
365        """
366        Creates new record in tcp_sequence based on data from tcpseq_dict.
367        """
368        debug("Inserting new tcp_sequence into database")
369       
370        self.cursor.execute("INSERT INTO tcp_sequence (tcp_index, \
371                        class, difficulty, tcp_values) VALUES (?, ?, ?, ?)", (
372                        tcpseq_dict["index"], tcpseq_dict["class"], 
373                        tcpseq_dict["difficulty"], tcpseq_dict["values"]))
374        self.conn.commit()
375
376
377    def insert_tcp_ts_sequence_db(self, tcptsseq_dict):
378        """
379        Creates new record in tcp_ts_sequence based on data from tcptsseq
380        dict.
381        """
382        debug("Inserting new tcp_ts_sequence into database")
383       
384        self.cursor.execute("INSERT INTO tcp_ts_sequence (class, \
385                      tcp_ts_values)  VALUES (?, ?)", (tcptsseq_dict["class"], 
386                      tcptsseq_dict["values"]))
387        self.conn.commit()
388
389
390    def insert_ip_id_sequence_db(self, ipidseq_dict):
391        """
392        Creates new record in ip_id_sequence based on data from ipidseq dict.
393        """
394        debug("Inserting new ip_id_sequence into database")
395       
396        self.cursor.execute("INSERT INTO ip_id_sequence (class, \
397                      ip_id_values)  VALUES (?, ?)", (ipidseq_dict["class"], 
398                      ipidseq_dict["values"]))
399        self.conn.commit()
400
401
402    def insert_inventory_db(self, inventory):
403        """
404        Creates new record in inventory.
405        """
406        debug("Inserting new inventory into database")
407
408        self.cursor.execute("INSERT INTO inventory (name) VALUES (?)", 
409                    (inventory, ))
410        self.conn.commit()
411       
412   
413    def insert_inventory_scan_db(self, scan, inventory):
414        """
415        Creates new record in _inventory_scan.
416        """
417        debug("Inserting new _inventory_scan into database")
418
419        self.cursor.execute("INSERT INTO _inventory_scan (fk_scan, \
420                    fk_inventory) VALUES (?, ?)", (scan, inventory))
421        self.conn.commit()
422       
423       
424    def insert_inventory_change_category_db(self, category):
425        """
426        Creates new record in inventory_change_category.
427        """
428        debug("Inserting new category '%s' into \
429inventory_change_category" % category)
430       
431        self.cursor.execute("INSERT INTO inventory_change_category (name) \
432                             VALUES (?)", (category, ))
433        self.conn.commit()
434       
435   
436    def insert_inventory_comparison_db(self, old_hid, new_hid, date, 
437                                       short_descr, fk_inventory, fk_category,
438                                       fk_address):
439        """
440        Creates new record in _inventory_changes.
441        """
442        debug("Inserting new change into _inventory_changes")
443       
444        self.cursor.execute("INSERT INTO _inventory_changes (old_hostid, \
445                             new_hostid, entry_date, short_description, \
446                             fk_inventory, fk_category, fk_address) VALUES \
447                             (?, ?, ?, ?, ?, ?, ?)", (old_hid, new_hid, date,
448                                                      short_descr, fk_inventory,
449                                                      fk_category, fk_address))
450        self.conn.commit()
451         
Note: See TracBrowser for help on using the browser.