本文整理汇总了Java中org.pcap4j.packet.Packet类的典型用法代码示例。如果您正苦于以下问题:Java Packet类的具体用法?Java Packet怎么用?Java Packet使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Packet类属于org.pcap4j.packet包,在下文中一共展示了Packet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: TCPTun
import org.pcap4j.packet.Packet; //导入依赖的package包/类
TCPTun(CapEnv capEnv,
Inet4Address serverAddress, short serverPort,
MacAddress srcAddress_mac, MacAddress dstAddrress_mac){
this.capEnv=capEnv;
sendHandle=capEnv.sendHandle;
this.remoteAddress=serverAddress;
this.remotePort=serverPort;
localAddress=capEnv.local_ipv4;
localPort=(short)(random.nextInt(64*1024-1-10000)+10000);
Packet syncPacket=null;
try {
syncPacket = PacketUtils.createSync(srcAddress_mac, dstAddrress_mac, localAddress, localPort,serverAddress, serverPort, localStartSequence,getIdent());
try {
sendHandle.sendPacket(syncPacket);
localSequence=localStartSequence+1;
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
MLog.println("发送第一次握手 "+" ident "+localIdent);
MLog.println(""+syncPacket);
}
示例2: onReceiveDataPacket
import org.pcap4j.packet.Packet; //导入依赖的package包/类
void onReceiveDataPacket(TcpPacket tcpPacket, TcpHeader tcpHeader, IpV4Header ipV4Header ){
if(System.currentTimeMillis()-lastSendAckTime>1000){
int rs=tcpHeader.getSequenceNumber()+tcpPacket.getPayload().getRawData().length;
if(rs>remoteSequence_max){
remoteSequence_max=rs;
}
Packet ackPacket=PacketUtils.createAck(
capEnv.local_mac,
capEnv.gateway_mac,
localAddress,(short)localPort,
ipV4Header.getSrcAddr(),tcpHeader.getSrcPort().value(),
remoteSequence_max, localSequence,getIdent());
try {
sendHandle.sendPacket(ackPacket);
} catch (Exception e) {
e.printStackTrace();
}
lastSendAckTime=System.currentTimeMillis();
lastReceiveDataTime=System.currentTimeMillis();
}
}
示例3: sendData
import org.pcap4j.packet.Packet; //导入依赖的package包/类
void sendData(byte[] data){
Packet dataPacket=PacketUtils.createDataPacket(capEnv.local_mac,
capEnv.gateway_mac,
localAddress,localPort,
remoteAddress,remotePort,
localSequence,remoteSequence_max, data, (short) getIdent());
synchronized (syn_send_data) {
try {
sendHandle.sendPacket(dataPacket);
localSequence+=data.length;
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例4: TCPTun
import org.pcap4j.packet.Packet; //导入依赖的package包/类
TCPTun(CapEnv capEnv,
Inet4Address serverAddress,short serverPort,
MacAddress srcAddress_mac,MacAddress dstAddrress_mac){
this.capEnv=capEnv;
sendHandle=capEnv.sendHandle;
this.remoteAddress=serverAddress;
this.remotePort=serverPort;
localAddress=capEnv.local_ipv4;
localPort=(short)(random.nextInt(64*1024-1-10000)+10000);
Packet syncPacket=null;
try {
syncPacket = PacketUtils.createSync(srcAddress_mac, dstAddrress_mac, localAddress, localPort,serverAddress, serverPort, localStartSequence,getIdent());
try {
sendHandle.sendPacket(syncPacket);
localSequence=localStartSequence+1;
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
MLog.println("发送第一次握手 "+" ident "+localIdent);
MLog.println(""+syncPacket);
}
示例5: onReceiveDataPacket
import org.pcap4j.packet.Packet; //导入依赖的package包/类
void onReceiveDataPacket(TcpPacket tcpPacket,TcpHeader tcpHeader,IpV4Header ipV4Header ){
if(System.currentTimeMillis()-lastSendAckTime>1000){
int rs=tcpHeader.getSequenceNumber()+tcpPacket.getPayload().getRawData().length;
if(rs>remoteSequence_max){
remoteSequence_max=rs;
}
Packet ackPacket=PacketUtils.createAck(
capEnv.local_mac,
capEnv.gateway_mac,
localAddress,(short)localPort,
ipV4Header.getSrcAddr(),tcpHeader.getSrcPort().value(),
remoteSequence_max, localSequence,getIdent());
try {
sendHandle.sendPacket(ackPacket);
} catch (Exception e) {
e.printStackTrace();
}
lastSendAckTime=System.currentTimeMillis();
lastReceiveDataTime=System.currentTimeMillis();
}
}
示例6: processUdpPacket
import org.pcap4j.packet.Packet; //导入依赖的package包/类
public static void processUdpPacket(Packet packet, PcapFileSummary pcapFileSummary, PacketInfo packetInfo, Mode mode) {
if (packet == null) {
return; // skip empty packets
}
String sourceAddress = packetInfo.get(PacketInfo.SOURCE_ADDRESS);
String destinationAddress = packetInfo.get(PacketInfo.DESTINATION_ADDRESS);
try {
log.trace("Converting to UDP packet");
UdpPacket udpPacket = UdpPacket.newPacket(packet.getRawData(), 0, packet.length());
UdpPacket.UdpHeader udpHeader = udpPacket.getHeader();
UdpPort sourcePort = udpHeader.getSrcPort();
UdpPort destinationPort = udpHeader.getDstPort();
packetInfo.put(PacketInfo.SOURCE_PORT, sourcePort.valueAsString());
packetInfo.put(PacketInfo.DESTINATION_PORT, destinationPort.valueAsString());
if (mode == Mode.POSSIBLE_ATTACKS_ANALYSIS) {
//pcapFileSummary.portScanDetector.add(packetInfo);
}
String udpSource = sourceAddress + ":" + sourcePort.toString();
String udpDestination = destinationAddress + ":" + destinationPort.toString();
log.trace("Adding UDP source to set: " + udpSource);
pcapFileSummary.udpSources.add(udpSource);
log.trace("UDP{ source: " + udpSource + ", destination: " + udpDestination + " }");
} catch (IllegalRawDataException e) {
log.error("Exception occurred while processing a packet. Exception was: " + e);
}
}
示例7: processArpPacket
import org.pcap4j.packet.Packet; //导入依赖的package包/类
public static void processArpPacket(Packet packet, PcapFileSummary pcapFileSummary) {
if (packet == null) {
return; // skip empty packets
}
log.trace("Converting to ARP packet");
try {
ArpPacket arpPacket = ArpPacket.newPacket(packet.getRawData(), 0, packet.length());
ArpPacket.ArpHeader arpHeader = arpPacket.getHeader();
ArpOperation arpOperation = arpHeader.getOperation();
InetAddress sourceIp = arpHeader.getSrcProtocolAddr();
MacAddress sourceMac = arpHeader.getSrcHardwareAddr();
if (arpOperation == ArpOperation.REQUEST) {
// only add / check the source addresses for an ARP request
//IpMacTrackerResult result = pcapFileSummary.ipMacTracker.query(sourceIp.getHostAddress(), sourceMac.toString());
}
} catch (IllegalRawDataException e) {
log.error("Exception occurred while processing a packet. Exception was: " + e);
}
}
示例8: detect
import org.pcap4j.packet.Packet; //导入依赖的package包/类
public boolean detect(Packet packet, PcapFileSummary pcapFileSummary, PacketInfo packetInfo) {
String sourceAddress = packetInfo.get(PacketInfo.SOURCE_ADDRESS);
String destinationAddress = packetInfo.get(PacketInfo.DESTINATION_ADDRESS);
if (isPingOfDeath(packet) && !attackInProgress) { // attack first detected
log.trace("*** PING OF DEATH detected!");
attackInProgress = true;
attackSummary = new AttackSummary();
attackSummary.setAttackName("PING OF DEATH");
attackSummary.addSourceIpAndPort(sourceAddress);
attackSummary.addTargetIpAndPort(destinationAddress);
attackSummary.setStartTimestamp(packetInfo.get(PacketInfo.TIMESTAMP));
} else if (isPingOfDeath(packet) && attackInProgress) { // add more details while attack in progress
attackSummary.addSourceIpAndPort(sourceAddress);
attackSummary.addTargetIpAndPort(destinationAddress);
} else if (!isPingOfDeath(packet) && attackInProgress) { // attack ended, close out attack details
attackInProgress = false;
attackSummary.setEndTimestamp(packetInfo.get(PacketInfo.TIMESTAMP));
pcapFileSummary.attackSummaries.add(attackSummary);
this.attackSummary = null;
}
return this.attackInProgress;
}
示例9: TCPTun
import org.pcap4j.packet.Packet; //导入依赖的package包/类
TCPTun(CapEnv capEnv,
Inet4Address serverAddress,short serverPort,
MacAddress srcAddress_mac,MacAddress dstAddrress_mac){
this.capEnv=capEnv;
sendHandle=capEnv.sendHandle;
this.remoteAddress=serverAddress;
this.remotePort=serverPort;
localAddress=capEnv.local_ipv4;
localPort=(short)(random.nextInt(64*1024-1-10000)+10000);
Packet syncPacket=null;
try {
syncPacket = PacketUtils.createSync(srcAddress_mac, dstAddrress_mac, localAddress, localPort,serverAddress, serverPort, localStartSequence,getIdent());
try {
sendHandle.sendPacket(syncPacket);
localSequence=localStartSequence+1;
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
MLog.println("Sending first handshake ident "+localIdent);
MLog.println(""+syncPacket);
}
示例10: validatePacket
import org.pcap4j.packet.Packet; //导入依赖的package包/类
/**
* Validate packet
*
* @param packet
* @return
*/
public boolean validatePacket(Packet packet) {
validPacket = true;
if(packet.get(IpV4Packet.class) == null){
validPacket = false;
}else if (defaultSrcAddress == null) {
return true;
} else if (!packet.get(IpV4Packet.class).getHeader().getProtocol().name().equals(protocolType)) {
validPacket = false;
} else {
// check src/dst port combination
currentSrcAddressPort = packet.get(IpV4Packet.class).getHeader().getSrcAddr().getHostAddress() + ":" + getSrcPort(packet);
currentDstAddressPort = packet.get(IpV4Packet.class).getHeader().getDstAddr().getHostAddress() + ":" + getDstPort(packet);
if ((!currentSrcAddressPort.equals(defaultSrcAddressPort) && !currentSrcAddressPort.equals(defaultDstAddressPort))
|| (!currentDstAddressPort.equals(defaultSrcAddressPort) && !currentDstAddressPort.equals(defaultDstAddressPort))) {
validPacket = false;
}
}
return validPacket;
}
示例11: updateSrcAddress
import org.pcap4j.packet.Packet; //导入依赖的package包/类
/**
* Update source IP address
*/
private Packet updateSrcAddress(Packet packet) {
try {
if (importedProperties.isSourceEnabled()) {
Inet4Address modifiedAddress = (Inet4Address) Inet4Address.getByAddress(convertIPToByte(importedProperties.getSrcAddress()));
Packet.Builder builder = packet.getBuilder();
if (packet.get(IpV4Packet.class).getHeader().getSrcAddr().getHostAddress().equals(defaultSrcAddress)) {
builder.get(IpV4Packet.Builder.class).srcAddr(modifiedAddress);
} else {
builder.get(IpV4Packet.Builder.class).dstAddr(modifiedAddress);
}
packet = builder.build();
}
} catch (Exception ex) {
LOG.error("Error updating source IP", ex);
}
return packet;
}
示例12: updateDstAddress
import org.pcap4j.packet.Packet; //导入依赖的package包/类
/**
* Update destination IP address
*/
private Packet updateDstAddress(Packet packet) {
try {
if (importedProperties.isDestinationEnabled()) {
Inet4Address modifiedAddress = (Inet4Address) Inet4Address.getByAddress(convertIPToByte(importedProperties.getDstAddress()));
Packet.Builder builder = packet.getBuilder();
if (packet.get(IpV4Packet.class).getHeader().getDstAddr().getHostAddress().equals(defaultDstAddress)) {
builder.get(IpV4Packet.Builder.class).dstAddr(modifiedAddress);
} else {
builder.get(IpV4Packet.Builder.class).srcAddr(modifiedAddress);
}
packet = builder.build();
}
} catch (Exception ex) {
LOG.error("Error updating destination IP", ex);
}
return packet;
}
示例13: handleExportPcapFile
import org.pcap4j.packet.Packet; //导入依赖的package包/类
/**
* Export stream to pcap file
*/
public void handleExportPcapFile() {
try {
Profile p = tabledata.getProfiles().get(streamPacketTableView.getSelectionModel().getSelectedIndex());
String packetBinary = p.getStream().getPacket().getBinary();
byte[] pkt = Base64.decodeBase64(packetBinary);
Packet packet = EthernetPacket.newPacket(pkt, 0, pkt.length);
File pcapFile = File.createTempFile("temp-file-name", ".pcap");
PcapHandle handle = Pcaps.openDead(DataLinkType.EN10MB, 65536);
PcapDumper dumper = handle.dumpOpen(pcapFile.getAbsolutePath());
Timestamp ts = new Timestamp(0);
dumper.dump(packet, ts);
dumper.close();
handle.close();
String fileName = p.getName() + ".pcap";
Window owner = streamPacketTableView.getScene().getWindow();
FileManager.exportFile("Save Pcap File", fileName, pcapFile, owner, FileType.PCAP);
} catch (IllegalRawDataException | IOException | PcapNativeException | NotOpenException ex) {
LOG.error("Error during generate JSON file", ex);
}
}
示例14: getPacketInfo
import org.pcap4j.packet.Packet; //导入依赖的package包/类
/**
*
* @param packetBinary
* @return
*/
public PacketInfo getPacketInfo(String packetBinary) {
PacketInfo packetInfo = null;
try {
byte[] pkt = Base64.decodeBase64(packetBinary);
Packet packet = EthernetPacket.newPacket(pkt, 0, pkt.length);
packetInfo = getPacketTypeText(packet);
} catch (IllegalRawDataException ex) {
LOG.error("Error reading packet info", ex);
}
return packetInfo;
}
示例15: encodePcapFile
import org.pcap4j.packet.Packet; //导入依赖的package包/类
/**
* @param binaryFile
* @return Encodes the bytes array of a PCAP File using Base64
*/
public String encodePcapFile(String binaryFile) {
try {
PcapHandle handle = Pcaps.openOffline(binaryFile);
Packet packet = handle.getNextPacketEx();
handle.close();
byte[] pkt = packet.getRawData();
byte[] bytesEncoded = Base64.encodeBase64(pkt);
return new String(bytesEncoded);
} catch (IOException | PcapNativeException | TimeoutException | NotOpenException ex) {
LOG.error("Error encoding pcap file", ex);
return binaryFile;
}
}