通过JPcap捕获网络TCP数据包,并将解析的信息写入execl文件中
来源:互联网 发布:淘宝上哪家卖红酒靠谱 编辑:程序博客网 时间:2024/05/20 18:46
以下代码主要实现功能:
1.利用JPcap捕获指定网卡的所有TCP数据包,并解析出"SRC_MAC","DST_MAC", "SRC_IP", "DST_IP", "GETInfo", "RefererInfo", "HostInfo"等信息。
2. 将这些信息不覆盖的写入到execl文件,并且将每天的数据信息记录在以年月日命名的execl工作表(sheet)中。
package execl;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import jpcap.*;import jpcap.packet.EthernetPacket;import jpcap.packet.Packet;import jpcap.packet.TCPPacket;import jxl.Workbook;import jxl.write.Label;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;public class HttpMessageReceive implements PacketReceiver {public static File file = new File("G:" + File.separator + "Experiment"+ File.separator + "HTTP" + File.separator+ "DetailHTTPData.xls");public static File tempfile = new File("G:" + File.separator + "Experiment"+ File.separator + "HTTP" + File.separator + "temp.xls");public static String[] HttpInfoStr = {"0","0","0","0","0","0","0","0"};public static final String[] HTTPStart = {"GET","POST","OPTIONS"}; //HTTP协议有效信息开始的三个标志public static final String[] StrLabel = { "TimeOnLine", "SRC_MAC","DST_MAC", "SRC_IP", "DST_IP", "GETInfo", "RefererInfo", "HostInfo" };public static void main(String[] args) throws Exception {NetworkInterface[] devices = JpcapCaptor.getDeviceList();if(args.length<1){System.out.println("usage: java Tcpdump <select a number from the following>");for (int i = 0; i < devices.length; i++) {System.out.print(i+" :"+devices[i].name + "(" + devices[i].description+")");System.out.println("data link:"+devices[i].datalink_name + "("+ devices[i].datalink_description+")");System.out.print("MAC address:");for (byte b : devices[i].mac_address)System.out.print(Integer.toHexString(b&0xff) + ":");System.out.println("");for (NetworkInterfaceAddress a : devices[i].addresses)System.out.println("address:"+a.address + " " + a.subnet + " "+ a.broadcast);}}else{JpcapCaptor jpcap = JpcapCaptor.openDevice(devices[Integer.parseInt(args[0])], 2000, false, 20);jpcap.setFilter("tcp", true); //设置过滤规则,只抓取tcps数据包jpcap.loopPacket(-1, new HttpMessageReceive());}}public void receivePacket(Packet packet) {try {String TimeOnLine = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());HttpInfoStr[0] = TimeOnLine; //时间戳TCPPacket tcpPacket = (TCPPacket) packet;EthernetPacket ethernetPacket = (EthernetPacket) packet.datalink;HttpInfoStr[1] = ethernetPacket.getSourceAddress(); //SRC_MAC:HttpInfoStr[2] = ethernetPacket.getDestinationAddress();//DST_MACHttpInfoStr[3] = tcpPacket.src_ip.toString().substring(1);//SRC_IPHttpInfoStr[4] = tcpPacket.dst_ip.toString().substring(1);//DST_IPString HTTPData = new String(tcpPacket.data,"utf-8");if(HTTPData.startsWith(HTTPStart[0])){ //GET方法访问int GETInfoStart = 0;int GETInfoEnd = HTTPData.indexOf("\r\n",GETInfoStart);String GETInfo = HTTPData.substring(GETInfoStart, GETInfoEnd);HttpInfoStr[5] = GETInfo;int RefererInfoStart = HTTPData.indexOf("Referer");int RefererInfoEnd = HTTPData.indexOf("\r\n",RefererInfoStart);String RefererInfo = HTTPData.substring(RefererInfoStart, RefererInfoEnd);HttpInfoStr[6] = RefererInfo;int HostInfoStart = HTTPData.indexOf("Host");int HostInfoEnd = HTTPData.indexOf("\r\n",HostInfoStart);String HostInfo = HTTPData.substring(HostInfoStart, HostInfoEnd);HttpInfoStr[7] = HostInfo;}else if(HTTPData.startsWith(HTTPStart[1])){ //POST方法访问//后续改进}//将sbIPv4Data,GETInfo,HostInfo,RefererInfo信息写入文件DetailHTTPData.xlsif(!"0".equals(HttpInfoStr[6])&& !"0".equals(HttpInfoStr[7])){ //当Host和Referer不为空时写入文件try {String sheetName = HttpInfoStr[0].substring(0, 8);int indexStrLabel = 0;int indexHttpInfo = 0;if(!file.exists()){WritableWorkbook wwb = Workbook.createWorkbook(file);WritableSheet ws = wwb.createSheet(sheetName, 0);while(indexStrLabel < StrLabel.length){Label label = new Label(indexStrLabel, 0, StrLabel[indexStrLabel]);ws.setColumnView(indexHttpInfo, HttpInfoStr[indexHttpInfo].length()+2);ws.addCell(label);indexStrLabel++;}while(indexHttpInfo < HttpInfoStr.length ){Label label = new Label(indexHttpInfo, 1, HttpInfoStr[indexHttpInfo]);ws.setColumnView(indexHttpInfo, HttpInfoStr[indexHttpInfo].length()+2);ws.addCell(label);indexHttpInfo++;}//写入Exel工作表 wwb.write(); //关闭Excel工作薄对象 wwb.close(); System.out.println("=============================================");}else{Workbook rwb = Workbook.getWorkbook(file);WritableWorkbook wwb = Workbook.createWorkbook(tempfile, rwb);int sheetNum = rwb.getNumberOfSheets();if(wwb.getSheet(sheetName) == null){WritableSheet ws = wwb.createSheet(sheetName, sheetNum);while(indexStrLabel < StrLabel.length){Label label = new Label(indexStrLabel, 0, StrLabel[indexStrLabel]);ws.setColumnView(indexHttpInfo, HttpInfoStr[indexHttpInfo].length()+2);ws.addCell(label);indexStrLabel++;}while(indexHttpInfo < HttpInfoStr.length ){Label label = new Label(indexHttpInfo, 1, HttpInfoStr[indexHttpInfo]);ws.setColumnView(indexHttpInfo, HttpInfoStr[indexHttpInfo].length()+2);ws.addCell(label);indexHttpInfo++;}}else{int CurrentSheetNumRows = rwb.getSheet(sheetName).getRows();WritableSheet ws = wwb.getSheet(sheetName);while(indexHttpInfo < HttpInfoStr.length ){Label label = new Label(indexHttpInfo, CurrentSheetNumRows, HttpInfoStr[indexHttpInfo]);ws.setColumnView(indexHttpInfo, HttpInfoStr[indexHttpInfo].length()+2);ws.addCell(label);indexHttpInfo++;}}wwb.write();wwb.close();rwb.close();String filename = file.getPath();System.out.println("filename:" + filename);file.delete();tempfile.renameTo(file);System.out.println("tempfile:" + tempfile.getPath());System.out.println(tempfile.exists());System.out.println(file.exists());}} catch (WriteException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} }}
1 0
- 通过JPcap捕获网络TCP数据包,并将解析的信息写入execl文件中
- 捕获IP数据包的Java类Jpcap
- JPCAP捕获ARP数据包
- Jpcap JAVA捕捉并分析网络数据包
- 捕获异常信息并写入日志文件
- Jnetpcap 官方实例(4)- 将捕获的数据包写入文件
- jpcap IP数据包捕获程序
- Python:监控键盘输入、鼠标操作,并将捕获到的信息记录到文件中
- android 通过UncaughtExceptionHandler 捕获RuntimeException,并将捕获到的信息上传到友盟
- DirectShow中利用SampleGrabber捕获摄像头每一帧图像,并转为BMP文件信息写入内存
- DirectShow中利用SampleGrabber捕获摄像头每一帧图像,并转为BMP文件信息写入内存
- DirectShow中利用SampleGrabber捕获摄像头每一帧图像,并转为BMP文件信息写入内存
- 通过java的io流将本地文件读取到控制台,并将文件内容再次写入另一个文件中
- Winpcap打开适配器并捕获数据包:解析所捕获的数据包的协议首部
- OpenCV - 将图片的二进制信息写入到文件中
- 统计系统的登录信息 并写入到文件中
- wincap网络数据包的捕获
- 将控制台的信息写入到文件
- Java方法的值传递机制
- org.springframework.beans.factory.BeanCreationException
- linux系统查看各类服务及改变服务状态解决
- android用户权限获取
- ContentProvider(内容提供者),UriMatcher(Uri匹配器)
- 通过JPcap捕获网络TCP数据包,并将解析的信息写入execl文件中
- tjut 4940
- JavaSE学习笔记之-----数组
- VS2013中的getch()和scanf()报错问题
- 【杭电2098】分拆素数和
- hdu 4635 Strongly connected Kosaraju/Tarjan求强联通分量大小
- MySQL配置
- OC编码风格
- 图片处理类(剪裁、缩放、水印)