• No results found

Anexo XI - Código para Processar PDML e Gerar Script SQL

N/A
N/A
Protected

Academic year: 2021

Share "Anexo XI - Código para Processar PDML e Gerar Script SQL"

Copied!
13
0
0

Loading.... (view fulltext now)

Full text

(1)

Anexo XI - Código para Processar PDML e Gerar Script SQL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.InputStream; import java.io.PrintWriter; import java.sql.BatchUpdateException; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.xml.namespace.QName; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; public class ScriptXML {

Packet packet = new Packet(); public ScriptXML(){

}

public void insertLines(String infilename, String outfilename) {

try {

// First create a new XMLInputFactory XMLInputFactory inputFactory =

XMLInputFactory.newInstance();

// Setup a new eventReader InputStream in = new

FileInputStream(infilename);

XMLEventReader eventReader = inputFactory.createXMLEventReader(in);

// Read the XML document String filename =

"C:\\Path\\scriptSQL";

(2)

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

QName qname = new QName("name"); QName qshow = new QName("show"); QName qpacket = new QName("packet"); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); // identificar o protocolo geninfo Attribute name_att = startElement.getAttributeByName(qname); Attribute show_att = startElement.getAttributeByName(qshow);

//pegar o packet len

if (name_att!=null){ if(name_att.getValue().toString().equals("frame.len")){ packet.setFrame_pkt_len(show_att.getValue()); }else //pegar o timestamp if (name_att.getValue().toString().equals("timestamp")){ packet.setTimestamp((startElement.getAttributeByName(new QName("value"))).getValue()); }else //pegar ip.src if (name_att.getValue().toString().equals("ip.src")){ packet.setIp_src(show_att.getValue()); }else if(name_att.getValue().toString().equals("ip.dst")){ packet.setIp_dst(show_att.getValue()); }else if(name_att.getValue().toString().equals("tcp.stream")){ packet.setTcp_stream(show_att.getValue()); }else if(name_att.getValue().toString().equals("tcp.dstport")){

(3)

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 packet.setDstport(show_att.getValue()); }else if(name_att.getValue().toString().equals("tcp.srcport")){ packet.setSrcport(show_att.getValue()); }else if(name_att.getValue().toString().equals("http.host")){ packet.setHttp_host(show_att.getValue()); }else if(name_att.getValue().toString().equals("http.request.uri")) { packet.setHttp_request_uri(show_att.getValue()); }else if(name_att.getValue().toString().equals("http.content_length ")){ packet.setHttp_content_length(show_att.getValue()); }else if(name_att.getValue().toString().equals("wlan.da")){ packet.setWlanDa(show_att.getValue()); }else if(name_att.getValue().toString().equals("wlan.sa")){ packet.setWlanSa(show_att.getValue()); }else if(name_att.getValue().toString().equals("wlan.bssid")){ packet.setWlanBssid(show_att.getValue()); }else if(name_att.getValue().toString().equals("eth.src")){ packet.setEthSrc(show_att.getValue()); }else if(name_att.getValue().toString().equals("eth.dst")){ packet.setEthDst(show_att.getValue()); } //pegar frame.protocols else if(name_att.getValue().toString().equals("frame.protocols")){

(4)

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 packet.setProtocols(show_att.getValue()); }else if(name_att.getValue().toString().equals("http.referer")){ packet.setHttp_referer(show_att.getValue()); }else if(name_att.getValue().toString().contains("image-jfif")){ packet.setHttp_content(Figure.processJpeg(eventReader, filename+filename_num)); filename_num++; }else if (name_att.getValue().toString().equals("image-gif")){ packet.setHttp_content(Figure.processGif(eventReader, filename+filename_num)); filename_num++; }else if (name_att.getValue().toString().equals("png")){ packet.setHttp_content(Figure.processPng(eventReader, filename+filename_num)); filename_num++; } else if (name_att.getValue().toString().equals("tcp.segment")){ Iterator attributes = startElement.getAttributes(); while(attributes.hasNext()){ Attribute attribute = (Attribute) attributes.next();

if (attribute.getName().toString().equals("value")){ try{ //if(attribute.getValue().length()<100000){ Figure.tcpsegment = Figure.tcpsegment + attribute.getValue();

// apenas para o campo content n~ao ficar vazio packet.setTcpData(attribute.getValue());

//}

}catch(java.lang.OutOfMemoryError e){ e.printStackTrace();

(5)

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 //packet.setTcpData("ERROR"); }catch(Exception e){ e.printStackTrace(); } } } }else if(name_att.getValue().toString().equals("http.content_type") ){ packet.setHttp_content_type(show_att.getValue()); QName showname = new QName("showname"); Attribute showname_att = startElement.getAttributeByName(showname); if(showname_att.getValue().toString().contains("icon") && !showname_att.getValue().toString().contains("x-icon")){ packet.setHttp_content(Figure.processIcon(eventReader, filename+filename_num)); filename_num++; }else if(showname_att.getValue().toString().contains("multipart")){ packet.setHttp_content(Text.processMimo(packet.getTcpDat a())); } }else if(name_att.getValue().toString().equals("")){ if (show_att!=null && show_att.getValue().toString().contains("bmp")){ packet.setHttp_content(Figure.processBmp(startElement, filename+filename_num)); filename_num++; }else if (show_att!=null && show_att.getValue().toString().contains("x-icon")){ packet.setHttp_content(Figure.processBmp(startElement, filename+filename_num)); filename_num++; }else if (

(6)

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 show_att!=null && show_att.getValue().toString().contains("octet-stream")){ packet.setHttp_content(Text.processOctet(startElement)); filename_num++; } } else if(name_att.getValue().toString().equals("data-text-lines")){ packet.setHttp_content(Text.processText(eventReader)); }else if(name_att.getValue().toString().equals("media")){ event = eventReader.nextTag(); startElement = event.asStartElement(); packet.setHttp_content(Figure.processMedia(startElement, filename+filename_num)); filename_num++; }else if(name_att.getValue().toString().equals("xml")){ packet.setHttp_content(Text.processXml(eventReader)); } //else if(show_att!= null){ // if(show_att.getValue().toString().contains("TCP segment data")){ // packet.setTcpData(startElement.getAttributeByName(new QName("value")).getValue()); // } //} } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (XMLStreamException e) { e.printStackTrace(); } }

(7)

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

public static void main(String args[]) { ScriptXML script = new ScriptXML(); //criar a tabela do script SQL

EscreveArquivo.escrever("C:\\Path\\ScriptSQL\\scriptsq0. sql", packet.toSQLPacketTable()); script.createTables(""); int i=1; while(i<23){ script.insertLines("C:\\DadosCapturados\\file"+i+".xml", "C:\\CVS\\scriptsql"+i+".sql"); i++; } } } import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.Raster; import java.awt.image.WritableRaster; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.plaf.IconUIResource; import javax.swing.plaf.basic.BasicInternalFrameTitlePane.IconifyAct ion; import javax.swing.text.IconView; import com.sun.org.apache.bcel.internal.generic.ICONST;

public class Packet { //Frame level

private String frame_pkt_len =""; private String timestamp =""; private String tcpdata =""; //enlace level

(8)

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

private String wlan_da = ""; private String wlan_sa = ""; private String wlan_bssid =""; private String eth_dst = ""; private String eth_src=""; //IP level

private String ip_src = ""; private String ip_dst = ""; //TCP level

private String tcp_stream = "0"; private String srcport = "0"; private String dstport = "0";

//HTTP level

private String http_host = "";

private String http_request_uri = ""; private String protocols = "";

//colocar figura

private String http_content_type = ""; private String http_content = "";

private String http_content_length ="0"; private String http_referer ="";

public void reset(){

this.setDstport("0"); this.setEthDst(""); this.setEthSrc(""); this.setFrame_pkt_len(""); this.setHttp_content(""); this.setHttp_content_length("0"); this.setHttp_content_type(""); this.setHttp_host(""); this.setHttp_request_uri(""); this.setIp_dst(""); this.setIp_src(""); this.setProtocols(""); this.setSrcport("0"); this.setTcp_stream("0"); this.setTcpData(""); this.setTimestamp(""); this.setWlanBssid(""); this.setWlanDa(""); this.setWlanSa(""); this.setHttp_referer(""); }

(9)

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 http_referer =newvalue; }

public String getHttp_referer(){ return http_referer;

}

public void setDstport(String dstport) { this.dstport = dstport;

}

public String getEthDst() { return eth_dst;

}

public String getEthSrc() { return eth_src;

}

public void setEthDst(String newvalue) { eth_dst = newvalue;

}

public void setEthSrc(String newvalue) { eth_src = newvalue;

}

public String getWlanDa() { return wlan_da;

}

public void setWlanDa(String newwlan_da) { wlan_da = newwlan_da;

}

public String getWlanSa() { return wlan_sa;

}

public void setWlanSa(String newwlan_sa) { wlan_sa = newwlan_sa;

}

public String getWlanBssid() { return wlan_bssid;

}

public void setWlanBssid(String newwlan_bssid) { wlan_bssid = newwlan_bssid;

}

public String getTcpData() { return tcpdata;

}

public void setTcpData(String tcpdata_){ tcpdata = tcpdata_;

}

public String getDstport() { return dstport;

(10)

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

public void setSrcport(String srcport) { this.srcport = srcport;

}

public String getSrcport() { return srcport;

}

public void setTcp_stream(String tcp_stream) { this.tcp_stream = tcp_stream;

}

public String getTcp_stream() { return tcp_stream;

}

public void setHttp_request_uri(String http_request_uri) {

this.http_request_uri = http_request_uri; }

public String getHttp_request_uri() { return http_request_uri;

}

public void setFrame_pkt_len(String frame_pkt_len) { this.frame_pkt_len = frame_pkt_len;

}

public String getFrame_pkt_len() { return frame_pkt_len;

}

public void setTimestamp(String timestamp) { this.timestamp = timestamp;

}

public String getTimestamp() { return timestamp;

}

public void setIp_src(String ip_src) { this.ip_src = ip_src;

}

public String getIp_src() { return ip_src;

(11)

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

public void setIp_dst(String ip_dst) { this.ip_dst = ip_dst;

}

public String getIp_dst() { return ip_dst;

}

public String toSQLPacketTable(){

// timestamp, frame_pkt_len,

ip_src, ip_dst, http_host, http_request_uri, protocols, tcp_stream , srcport, dstport, http_content_type,

http_content_length http_content , tcpdata wlan_da wlan_sa wlan_ssid

//php

//<?php ini_set('memory_limit', '100M');

$conexao=mysql_connect(\"localhost\",\"root\",\"\") or die (\"Erro na conexao com o banco\");

mysql_select_db(\"GreenNetwork\",$conexao) or die(\"Erro ao selecionar o banco\"); mysql_query(

return "CREATE TABLE PACKET (cod INT NOT NULL AUTO_INCREMENT PRIMARY KEY, timestamp varchar(40) NOT NULL, frame_pkt_len int NOT NULL, ip_src varchar(20),ip_dst

varchar(20), http_host varchar(60), http_request_uri varchar(200) , tcp_stream int, srcport int, dstport int, http_content_type varchar(40), http_content_length int, http_content varchar(3000), tcpdata varchar (2000), wlan_da varchar(17), wlan_sa varchar(17), wlan_ssid varchar(17), protocols varchar(50), eth_src varchar(17),eth_dst

varchar(17), http_referer varchar(1000));"; }

public String toSQLLine(){ //mysql_query('

return "insert into PACKET (timestamp,

frame_pkt_len, ip_src, ip_dst, http_host, http_request_uri, protocols, tcp_stream, srcport, dstport, http_content_type, http_content_length, http_content, tcpdata, wlan_da, wlan_sa, wlan_ssid, eth_dst, eth_src, http_referer) values

('"+getTimestamp()+"', "+getFrame_pkt_len()+", '"+getIp_src()+"','"+getIp_dst()+"','"+getHttp_host()+"', '"+getHttp_request_uri()+"','"+getProtocols()+"', "+getTcp_stream()+" , "+getSrcport()+","+getDstport()+",'"+getHttp_content_type()+" ',"+ getHttp_content_length()+",'"+getHttp_content()+"','"+getTcpD

(12)

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 ata()+"','"+getWlanDa()+"','"+getWlanSa()+"','"+getWlanBssid( )+"','"+getEthDst()+"','"+getEthSrc()+"','"+getHttp_referer() +"'); "; }

public String getHttp_content_type(){ return this.http_content_type; }

public void setHttp_content_type(String newhttp_content_type){

this.http_content_type = newhttp_content_type; }

public String getHttp_content_length(){ return this.http_content_length; }

public void setHttp_content_length(String newhttp_content_length){

this.http_content_length = newhttp_content_length; }

public String getHttp_content(){ return this.http_content; }

public void setHttp_content(String newhtt_content){ //coloquei esta restricao, pq nao eh possivel declara uma string muito grande. O limite 'e constant string values is limited to 65535 characters

if(newhtt_content.length()>=Integer.MAX_VALUE){ this.http_content = newhtt_content.substring(0, Integer.MAX_VALUE-1); }else{ this.http_content = newhtt_content; } }

public void setHttp_host(String http_host) { this.http_host = http_host;

}

public String getHttp_host() { return http_host;

}

public void setProtocols(String protocols) { this.protocols = protocols;

}

public String getProtocols() { return protocols;

} }

(13)

References

Related documents

The aim of this study was to examine cervical findings for the prediction and the comparative use of Arabin pessary or cerclage for the prevention of preterm birth in asymptomatic

Title Total mercury concentrations in liver and scales of European whitefish (Coregonus lavaretus (L.)) in a subarctic lake - assessing the factors driving year-round

Affected by the time of interviews psychology teacher, provide rich qualitative data to be collected for the people being reliable than the tests.. Detect the respondent comes up

Carrie Krenicky, Chief Financial Officer, stated that the Library had a 3year contract with Republic Services, Inc.. to provide waste material services for the Main Library, the

the core subjects in a self- contained classroom in an elementary or middle school.. SCENARIO 3: ESTABLISHING THAT AN EXPERIENCED TEACHER IS “HIGHLY QUALIFIED” TO TEACH THE CORE

Class IC drugs commonly used to treat AF include propafenone and fl ecainide. In patients with no history of a previous heart attack and normal heart function, these drugs are very

The main disease features addressed were breast cancer initiation and development at cellular levels by using mainly immortalised cell lines in particular, and ex vivo models

Anal- yses from these surveys may still be problematic, for example, while the 1985 GSS, 1987 GSS, and 2000 ANES all included social network batteries asking respondents to