Class: Dorothy::Insertdb
- Inherits:
-
Object
- Object
- Dorothy::Insertdb
- Defined in:
- lib/dorothy2/do-utils.rb
Class Method Summary collapse
Instance Method Summary collapse
- #begin_t ⇒ Object
- #close ⇒ Object
- #commit ⇒ Object
- #find_pcap ⇒ Object
- #find_seq(seq) ⇒ Object
- #find_vm ⇒ Object
- #flush_table(table) ⇒ Object
- #free_vm(vmid) ⇒ Object
- #get_anal_id ⇒ Object
-
#initialize ⇒ Insertdb
constructor
A new instance of Insertdb.
- #insert(table, values) ⇒ Object
- #malware_list ⇒ Object
- #raw_insert(table, data) ⇒ Object
- #rollback ⇒ Object
- #select(table, column, value, column2 = nil, value2 = nil, column3 = nil, value3 = nil) ⇒ Object
- #set_analyzed(hash) ⇒ Object
- #status ⇒ Object
- #table_empty?(table) ⇒ Boolean
- #update_proto(role, ip) ⇒ Object
- #vm_init ⇒ Object
Constructor Details
#initialize ⇒ Insertdb
Returns a new instance of Insertdb.
53 54 55 |
# File 'lib/dorothy2/do-utils.rb', line 53 def initialize @db = PGconn.open(:host=> DoroSettings.dorothive[:dbhost], :dbname=>DoroSettings.dorothive[:dbname], :user=>DoroSettings.dorothive[:dbuser], :password=>DoroSettings.dorothive[:dbpass]) end |
Class Method Details
.escape_bytea(data) ⇒ Object
141 142 143 144 |
# File 'lib/dorothy2/do-utils.rb', line 141 def self.escape_bytea(data) escaped = PGconn.escape_bytea data return escaped end |
Instance Method Details
#begin_t ⇒ Object
57 58 59 |
# File 'lib/dorothy2/do-utils.rb', line 57 def begin_t @db.exec("BEGIN") end |
#close ⇒ Object
69 70 71 |
# File 'lib/dorothy2/do-utils.rb', line 69 def close @db.close end |
#commit ⇒ Object
61 62 63 |
# File 'lib/dorothy2/do-utils.rb', line 61 def commit @db.exec("COMMIT") end |
#find_pcap ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/dorothy2/do-utils.rb', line 174 def find_pcap @pcaps = [] begin @db.exec("SELECT traffic_dumps.hash, traffic_dumps.pcapr_id, traffic_dumps.size, traffic_dumps.binary, traffic_dumps.parsed, samples.md5 as \"sample\", analyses.date as \"date\", analyses.id as \"anal_id\" FROM dorothy.traffic_dumps, dorothy.samples, dorothy.analyses WHERE analyses.traffic_dump = traffic_dumps.hash AND analyses.sample = samples.hash AND traffic_dumps.parsed = false").each do |q| @pcaps.push q end rescue LOGGER.error "DB","Error while fetching traffic_dumps table\n " + $! end end |
#find_seq(seq) ⇒ Object
158 159 160 |
# File 'lib/dorothy2/do-utils.rb', line 158 def find_seq(seq) @db.exec("SELECT currval('dorothy.#{seq}')") end |
#find_vm ⇒ Object
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/dorothy2/do-utils.rb', line 186 def find_vm vm = @db.exec("SELECT id, hostname, ipaddress, username, password FROM dorothy.sandboxes where is_available is true").first if vm.nil? LOGGER.warn "DB","At this time there are no free VM available" return false else @db.exec("UPDATE dorothy.sandboxes set is_available = false where id = '#{vm["id"]}'") return vm["id"].to_i, vm["hostname"], vm["ipaddress"], vm["username"], vm["password"] end end |
#flush_table(table) ⇒ Object
162 163 164 |
# File 'lib/dorothy2/do-utils.rb', line 162 def flush_table(table) @db.exec("TRUNCATE dorothy.#{table} CASCADE") end |
#free_vm(vmid) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/dorothy2/do-utils.rb', line 197 def free_vm(vmid) r = @db.exec("SELECT hostname FROM dorothy.sandboxes where id = '#{vmid}' AND is_available is false") if !r.first.nil? #check if the issued VM is already free begin @db.exec("UPDATE dorothy.sandboxes set is_available = true where id = '#{vmid}'") LOGGER.info "DB", "VM #{vmid} succesfully released" return true rescue LOGGER.error "DB", "An error occurred while releasing the VM" LOGGER.debug "DB", $! return false end else LOGGER.warn "DB", "Dorothy is trying to release the VM #{vmid} that is already available!!" return false end end |
#get_anal_id ⇒ Object
137 138 139 |
# File 'lib/dorothy2/do-utils.rb', line 137 def get_anal_id @db.exec("SELECT nextval('dorothy.analyses_id_seq')").first["nextval"].to_i end |
#insert(table, values) ⇒ Object
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 |
# File 'lib/dorothy2/do-utils.rb', line 79 def insert(table,values) n = 1 @sqlstring = "" values.each { |value| if value == "default" value1 = value elsif value == "null" value1 = value elsif value == "lastval()" value1 = value elsif value =~ /currval/ value1 = value else value1 = "'#{value}'" end if n == values.size @sqlstring << value1 elsif @sqlstring << value1 + "," end n += 1 } #p "Inserting in dorothy.#{table}:" #p "#{@sqlstring}" begin @db.exec("INSERT into dorothy.#{table} values (#{@sqlstring})") rescue => e LOGGER.debug "DB", $! LOGGER.debug "DB", e.inspect #self.rollback return false #exit 1 end #p "Insertion OK" end |
#malware_list ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/dorothy2/do-utils.rb', line 166 def malware_list malwares = [] @db.exec("SELECT samples.hash FROM dorothy.samples").each do |q| malwares.push q end return malwares end |
#raw_insert(table, data) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/dorothy2/do-utils.rb', line 119 def raw_insert(table, data) begin @db.exec("INSERT into dorothy.#{table} values (#{data})") rescue LOGGER.error "DB", "#{$!}" #self.rollback return false #exit 1 end end |
#rollback ⇒ Object
73 74 75 76 |
# File 'lib/dorothy2/do-utils.rb', line 73 def rollback LOGGER.error "DB", "DB ROLLBACK" @db.exec("ROLLBACK") end |
#select(table, column, value, column2 = nil, value2 = nil, column3 = nil, value3 = nil) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/dorothy2/do-utils.rb', line 130 def select(table, column, value, column2=nil, value2=nil, column3=nil, value3=nil) column2&&value2 ? ( column3&&value3 ? chk = @db.exec("SELECT * from dorothy.#{table} where #{column} = '#{value}' AND #{column2} = '#{value2}' AND #{column3} = '#{value3}' ") : chk = @db.exec("SELECT * from dorothy.#{table} where #{column} = '#{value}' AND #{column2} = '#{value2}'")) : chk = @db.exec("SELECT * from dorothy.#{table} where #{column} = '#{value}'") #puts ".::WARNING #{value} already present in dorothy.#{table}".red.bold if chk return chk end |
#set_analyzed(hash) ⇒ Object
154 155 156 |
# File 'lib/dorothy2/do-utils.rb', line 154 def set_analyzed(hash) @db.exec("UPDATE dorothy.traffic_dumps set parsed = true where hash = '#{hash}'") end |
#status ⇒ Object
65 66 67 |
# File 'lib/dorothy2/do-utils.rb', line 65 def status @db.transaction_status end |
#table_empty?(table) ⇒ Boolean
146 147 148 |
# File 'lib/dorothy2/do-utils.rb', line 146 def table_empty?(table) @db.exec("SELECT CASE WHEN EXISTS (SELECT * FROM dorothy.#{table} LIMIT 1) THEN FALSE ELSE TRUE END").first["case"] == "t" ? true : false end |
#update_proto(role, ip) ⇒ Object
150 151 152 |
# File 'lib/dorothy2/do-utils.rb', line 150 def update_proto(role, ip) @db.exec("UPDATE dorothy.host_roles set app_protocol = '#{proto}' where id = currval('connections_id_seq')") end |
#vm_init ⇒ Object
215 216 217 218 219 |
# File 'lib/dorothy2/do-utils.rb', line 215 def vm_init @db.exec("UPDATE dorothy.sandboxes set is_available = true") LOGGER.debug "DB", "All VM are now available" #TODO - revert them too? end |