SlideShare ist ein Scribd-Unternehmen logo
1 von 5
Packet Filtering using JPCAP

import java.net.*;
import java.io.*;
import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.*;

class Main
{
       /* variables */
       JpcapCaptor captor;
       NetworkInterface[] list;
       String str,info;
       int x, choice;

public static void main(String args[])
{
        new Main();
}

public Main()
{

       /* first fetch available interfaces to listen on */
        list = JpcapCaptor.getDeviceList();
       System.out.println("Available interfaces: ");

       for(x=0; x<list.length; x++)
       {
              System.out.println(x+" -> "+list[x].description);
       }
       System.out.println("-------------------------n");
       choice = Integer.parseInt(getInput("Choose interface (0,1..): "));
       System.out.println("Listening on interface -> "+list[choice].description);
       System.out.println("-------------------------n");

       /*Setup device listener */
       try
       {
              captor=JpcapCaptor.openDevice(list[choice], 65535, false, 20);

                /* listen for TCP/IP only */
                captor.setFilter("ip and tcp", true);
}
      catch(IOException ioe) { ioe.printStackTrace(); }


      /* start listening for packets */
      while (true)
      {
               Packet info = captor.getPacket();
               if(info != null)
               System.out.println(info);
      }
}

/* get user input */
public static String getInput(String q)
{
        String input = "";
        System.out.print(q);
        BufferedReader bufferedreader = new BufferedReader(new
        InputStreamReader(System.in));
        try
        {
                input = bufferedreader.readLine();
        }
        catch(IOException ioexception)
        {
        }
        return input;
        }
} /*end class*/
OUTPUT:
C:Packet CapturingjSniff>javac Main.java

C:Packet CapturingjSniff>java Main
Available interfaces:
0 -> MS Tunnel Interface Driver
1 -> Realtek 10/100/1000 Ethernet NIC
(Microsoft's Packet Scheduler)
-------------------------

Choose interface (0,1..): 1
Listening on interface -> Realtek 10/100/1000 Ethernet NIC
        (Microsoft's Packet Scheduler)
-------------------------

1319000427:719763 /172.10.0.81->/172.10.0.132 protocol(6) priority(0) hop(128)
 offset(0) ident(2203) TCP 445 > 1140 seq(2709085387) win(64592) ack 1006552375
 P
1319000427:720418 /172.10.0.132->/172.10.0.81 protocol(6) priority(0) hop(128)
 offset(0) ident(714) TCP 1140 > 445 seq(1006552375) win(64567) ack 2709085526
P
1319000427:721224 /172.10.0.81->/172.10.0.132 protocol(6) priority(0) hop(128)
 offset(0) ident(2204) TCP 445 > 1140 seq(2709085526) win(64452) ack 1006552515
 P
1319000427:721667 /172.10.0.132->/172.10.0.81 protocol(6) priority(0) hop(128)
 offset(0) ident(715) TCP 1140 > 445 seq(1006552515) win(64516) ack 2709085577
P
1319000427:721972 /172.10.0.81->/172.10.0.132 protocol(6) priority(0) hop(128)
 offset(0) ident(2205) TCP 445 > 1140 seq(2709085577) win(64389) ack 1006552578
 P
1319000427:722751 /172.10.0.132->/172.10.0.81 protocol(6) priority(0) hop(128)
 offset(0) ident(716) TCP 1140 > 445 seq(1006552578) win(64384) ack 2709085709
P
1319000427:930959 /172.10.0.81->/172.10.0.132 protocol(6) priority(0) hop(128)
 offset(0) ident(2206) TCP 445 > 1140 seq(2709085709) win(65535) ack 1006553370
ALGORITHM:

JPCAP
Jpcap can be used to develop many kinds of network applications, including (but not
limited to):

   •   network and protocol analyzers
   •   network monitors
   •   traffic loggers
   •   traffic generators
   •   user-level bridges and routers
   •   network intrusion detection systems (NIDS)
   •   network scanners
   •   security tools

Jpcap captures and sends packets independently from the host protocols (e.g., TCP/IP).
This means that Jpcap does not (cannot) block, filter or manipulate the traffic generated
by other programs on the same machine: it simply "sniffs" the packets that transit on the
wire. Therefore, it does not provide the appropriate support for applications like traffic
shapers, QoS schedulers and personal firewalls.

1. Obtain the list of network interfaces

To capture packets from a network, obtain the list of network interfaces.
JpcapCaptor.getDeviceList()
It returns an array of NetworkInterface objects.
A NetworkInterface object contains some information about the corresponding network
interface, such as its name, description, IP and MAC addresses, and datatlink name and
description.


2. Open a network interface

Choose which network interface to captuer packets from, open the interface by
using JpcapCaptor.openDevice() method.

JpcapCaptor.openDevice()
The following piece of code illustrates how to open an network interface

Name:                         Purpose
NetworkInterface intrface     Network interface that you want to open.
int snaplen                   Max number of bytes to capture at once.
boolean promics               True if you want to open the interface in promiscuous
                              mode, and otherwise false.
In promiscuous mode, you can capture packets every
                               packet from the wire
                               In non-promiscuous mode, you can only capture packets
                               send and received by your host.
int to_ms                      Set a capture timeout value in milliseconds.


3. Capture packets from the network interface

There are two major approaches to capture packets using a JpcapCaptor instance: using a
callback method, and capturing packets one-by-one.

Capturing packets one-by-one

capture packets using the JpcapCaptor.getPacket() method.

getPacket() method simply returns a captured packet.
getPacket() method multiple times to capture consecutive packets.

4. Set capturing filter

In Jpcap, you can set a filter so that Jpcap doesn't capture unwanted packets. For
example, if you only want to capture TCP/IPv4 packets, you can set a filter as following:

The filter expression "ip and tcp" means to to "keep only the packets that are both IPv4
and TCP and deliver them to the application".

By properly setting a filter, you can reduce the number of packets to examine, and thus
can improve the performance of your application.

Weitere ähnliche Inhalte

Was ist angesagt?

Virus & Computer security threats
Virus & Computer security threatsVirus & Computer security threats
Virus & Computer security threatsAzri Abdin
 
Web Application Penetration Tests - Information Gathering Stage
Web Application Penetration Tests - Information Gathering StageWeb Application Penetration Tests - Information Gathering Stage
Web Application Penetration Tests - Information Gathering StageNetsparker
 
Web application security I
Web application security IWeb application security I
Web application security IMd Syed Ahamad
 
Directory Traversal & File Inclusion Attacks
Directory Traversal & File Inclusion AttacksDirectory Traversal & File Inclusion Attacks
Directory Traversal & File Inclusion AttacksRaghav Bisht
 
Android Application Penetration Testing - Mohammed Adam
Android Application Penetration Testing - Mohammed AdamAndroid Application Penetration Testing - Mohammed Adam
Android Application Penetration Testing - Mohammed AdamMohammed Adam
 
What is Threat Hunting? - Panda Security
What is Threat Hunting? - Panda SecurityWhat is Threat Hunting? - Panda Security
What is Threat Hunting? - Panda SecurityPanda Security
 
Android Security & Penetration Testing
Android Security & Penetration TestingAndroid Security & Penetration Testing
Android Security & Penetration TestingSubho Halder
 
Intrusion detection system ppt
Intrusion detection system pptIntrusion detection system ppt
Intrusion detection system pptSheetal Verma
 
Web application security
Web application securityWeb application security
Web application securityKapil Sharma
 
Network Security Threats and Solutions
Network Security Threats and SolutionsNetwork Security Threats and Solutions
Network Security Threats and SolutionsColin058
 
BugBounty Roadmap with Mohammed Adam
BugBounty Roadmap with Mohammed AdamBugBounty Roadmap with Mohammed Adam
BugBounty Roadmap with Mohammed AdamMohammed Adam
 
Cross Site Scripting ( XSS)
Cross Site Scripting ( XSS)Cross Site Scripting ( XSS)
Cross Site Scripting ( XSS)Amit Tyagi
 
Cyber threat intelligence avec Open CTI
Cyber threat intelligence avec Open CTI Cyber threat intelligence avec Open CTI
Cyber threat intelligence avec Open CTI EyesOpen Association
 
OWASP - Dependency Check
OWASP - Dependency CheckOWASP - Dependency Check
OWASP - Dependency CheckVandana Verma
 
Vulnerability Assessment Presentation
Vulnerability Assessment PresentationVulnerability Assessment Presentation
Vulnerability Assessment PresentationLionel Medina
 
Benefits of Web Application Firewall
Benefits of Web Application FirewallBenefits of Web Application Firewall
Benefits of Web Application Firewalldavidjohnrace
 

Was ist angesagt? (20)

Virus & Computer security threats
Virus & Computer security threatsVirus & Computer security threats
Virus & Computer security threats
 
Web Application Penetration Tests - Information Gathering Stage
Web Application Penetration Tests - Information Gathering StageWeb Application Penetration Tests - Information Gathering Stage
Web Application Penetration Tests - Information Gathering Stage
 
Web application security I
Web application security IWeb application security I
Web application security I
 
Backdoor
BackdoorBackdoor
Backdoor
 
Directory Traversal & File Inclusion Attacks
Directory Traversal & File Inclusion AttacksDirectory Traversal & File Inclusion Attacks
Directory Traversal & File Inclusion Attacks
 
Android Application Penetration Testing - Mohammed Adam
Android Application Penetration Testing - Mohammed AdamAndroid Application Penetration Testing - Mohammed Adam
Android Application Penetration Testing - Mohammed Adam
 
Ssrf
SsrfSsrf
Ssrf
 
What is Threat Hunting? - Panda Security
What is Threat Hunting? - Panda SecurityWhat is Threat Hunting? - Panda Security
What is Threat Hunting? - Panda Security
 
Android Security & Penetration Testing
Android Security & Penetration TestingAndroid Security & Penetration Testing
Android Security & Penetration Testing
 
Intrusion detection system ppt
Intrusion detection system pptIntrusion detection system ppt
Intrusion detection system ppt
 
Web application security
Web application securityWeb application security
Web application security
 
Network Security Threats and Solutions
Network Security Threats and SolutionsNetwork Security Threats and Solutions
Network Security Threats and Solutions
 
OWASP Top Ten
OWASP Top TenOWASP Top Ten
OWASP Top Ten
 
BugBounty Roadmap with Mohammed Adam
BugBounty Roadmap with Mohammed AdamBugBounty Roadmap with Mohammed Adam
BugBounty Roadmap with Mohammed Adam
 
Cross Site Scripting ( XSS)
Cross Site Scripting ( XSS)Cross Site Scripting ( XSS)
Cross Site Scripting ( XSS)
 
Cyber threat intelligence avec Open CTI
Cyber threat intelligence avec Open CTI Cyber threat intelligence avec Open CTI
Cyber threat intelligence avec Open CTI
 
OWASP - Dependency Check
OWASP - Dependency CheckOWASP - Dependency Check
OWASP - Dependency Check
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Vulnerability Assessment Presentation
Vulnerability Assessment PresentationVulnerability Assessment Presentation
Vulnerability Assessment Presentation
 
Benefits of Web Application Firewall
Benefits of Web Application FirewallBenefits of Web Application Firewall
Benefits of Web Application Firewall
 

Ähnlich wie Packet filtering using jpcap

TCP IP
TCP IPTCP IP
TCP IPhivasu
 
Wireshark.ethereal
Wireshark.etherealWireshark.ethereal
Wireshark.etherealgh02
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernelKiran Divekar
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket ProgrammingVipin Yadav
 
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoCapturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoScyllaDB
 
Traffic monitoring
Traffic monitoringTraffic monitoring
Traffic monitoringRadu Galbenu
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programmingAnung Ariwibowo
 
Udp Programming
Udp ProgrammingUdp Programming
Udp Programmingphanleson
 
Cs423 raw sockets_bw
Cs423 raw sockets_bwCs423 raw sockets_bw
Cs423 raw sockets_bwjktjpc
 
Workshop Wireshark
Workshop Wireshark Workshop Wireshark
Workshop Wireshark Fabio Rosa
 
Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Andriy Berestovskyy
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.comphanleson
 
Cassandra 2.1 boot camp, Overview
Cassandra 2.1 boot camp, OverviewCassandra 2.1 boot camp, Overview
Cassandra 2.1 boot camp, OverviewJoshua McKenzie
 

Ähnlich wie Packet filtering using jpcap (20)

TCP IP
TCP IPTCP IP
TCP IP
 
Wireshark.ethereal
Wireshark.etherealWireshark.ethereal
Wireshark.ethereal
 
Ipc
IpcIpc
Ipc
 
#2 (UDP)
#2 (UDP)#2 (UDP)
#2 (UDP)
 
Geep networking stack-linuxkernel
Geep networking stack-linuxkernelGeep networking stack-linuxkernel
Geep networking stack-linuxkernel
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoCapturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
 
Traffic monitoring
Traffic monitoringTraffic monitoring
Traffic monitoring
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
 
Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Udp Programming
Udp ProgrammingUdp Programming
Udp Programming
 
Udp Programming
Udp ProgrammingUdp Programming
Udp Programming
 
Cs423 raw sockets_bw
Cs423 raw sockets_bwCs423 raw sockets_bw
Cs423 raw sockets_bw
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Workshop Wireshark
Workshop Wireshark Workshop Wireshark
Workshop Wireshark
 
Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)
 
#1 (TCPvs. UDP)
#1 (TCPvs. UDP)#1 (TCPvs. UDP)
#1 (TCPvs. UDP)
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
 
Cassandra 2.1 boot camp, Overview
Cassandra 2.1 boot camp, OverviewCassandra 2.1 boot camp, Overview
Cassandra 2.1 boot camp, Overview
 

Mehr von Elanthendral Mariappan (8)

Ad-HOc presentation
Ad-HOc presentationAd-HOc presentation
Ad-HOc presentation
 
Image+processing
Image+processingImage+processing
Image+processing
 
Ex11 mini project
Ex11 mini projectEx11 mini project
Ex11 mini project
 
Ex3 lisp likelist in java
Ex3 lisp likelist in javaEx3 lisp likelist in java
Ex3 lisp likelist in java
 
Cybercrimes
CybercrimesCybercrimes
Cybercrimes
 
Routing security in ad hoc wireless network
Routing security in ad hoc wireless networkRouting security in ad hoc wireless network
Routing security in ad hoc wireless network
 
Autonomic computer
Autonomic computerAutonomic computer
Autonomic computer
 
Autonomic computer
Autonomic computerAutonomic computer
Autonomic computer
 

Kürzlich hochgeladen

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Kürzlich hochgeladen (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Packet filtering using jpcap

  • 1. Packet Filtering using JPCAP import java.net.*; import java.io.*; import jpcap.JpcapCaptor; import jpcap.JpcapSender; import jpcap.NetworkInterface; import jpcap.NetworkInterfaceAddress; import jpcap.packet.*; class Main { /* variables */ JpcapCaptor captor; NetworkInterface[] list; String str,info; int x, choice; public static void main(String args[]) { new Main(); } public Main() { /* first fetch available interfaces to listen on */ list = JpcapCaptor.getDeviceList(); System.out.println("Available interfaces: "); for(x=0; x<list.length; x++) { System.out.println(x+" -> "+list[x].description); } System.out.println("-------------------------n"); choice = Integer.parseInt(getInput("Choose interface (0,1..): ")); System.out.println("Listening on interface -> "+list[choice].description); System.out.println("-------------------------n"); /*Setup device listener */ try { captor=JpcapCaptor.openDevice(list[choice], 65535, false, 20); /* listen for TCP/IP only */ captor.setFilter("ip and tcp", true);
  • 2. } catch(IOException ioe) { ioe.printStackTrace(); } /* start listening for packets */ while (true) { Packet info = captor.getPacket(); if(info != null) System.out.println(info); } } /* get user input */ public static String getInput(String q) { String input = ""; System.out.print(q); BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in)); try { input = bufferedreader.readLine(); } catch(IOException ioexception) { } return input; } } /*end class*/
  • 3. OUTPUT: C:Packet CapturingjSniff>javac Main.java C:Packet CapturingjSniff>java Main Available interfaces: 0 -> MS Tunnel Interface Driver 1 -> Realtek 10/100/1000 Ethernet NIC (Microsoft's Packet Scheduler) ------------------------- Choose interface (0,1..): 1 Listening on interface -> Realtek 10/100/1000 Ethernet NIC (Microsoft's Packet Scheduler) ------------------------- 1319000427:719763 /172.10.0.81->/172.10.0.132 protocol(6) priority(0) hop(128) offset(0) ident(2203) TCP 445 > 1140 seq(2709085387) win(64592) ack 1006552375 P 1319000427:720418 /172.10.0.132->/172.10.0.81 protocol(6) priority(0) hop(128) offset(0) ident(714) TCP 1140 > 445 seq(1006552375) win(64567) ack 2709085526 P 1319000427:721224 /172.10.0.81->/172.10.0.132 protocol(6) priority(0) hop(128) offset(0) ident(2204) TCP 445 > 1140 seq(2709085526) win(64452) ack 1006552515 P 1319000427:721667 /172.10.0.132->/172.10.0.81 protocol(6) priority(0) hop(128) offset(0) ident(715) TCP 1140 > 445 seq(1006552515) win(64516) ack 2709085577 P 1319000427:721972 /172.10.0.81->/172.10.0.132 protocol(6) priority(0) hop(128) offset(0) ident(2205) TCP 445 > 1140 seq(2709085577) win(64389) ack 1006552578 P 1319000427:722751 /172.10.0.132->/172.10.0.81 protocol(6) priority(0) hop(128) offset(0) ident(716) TCP 1140 > 445 seq(1006552578) win(64384) ack 2709085709 P 1319000427:930959 /172.10.0.81->/172.10.0.132 protocol(6) priority(0) hop(128) offset(0) ident(2206) TCP 445 > 1140 seq(2709085709) win(65535) ack 1006553370
  • 4. ALGORITHM: JPCAP Jpcap can be used to develop many kinds of network applications, including (but not limited to): • network and protocol analyzers • network monitors • traffic loggers • traffic generators • user-level bridges and routers • network intrusion detection systems (NIDS) • network scanners • security tools Jpcap captures and sends packets independently from the host protocols (e.g., TCP/IP). This means that Jpcap does not (cannot) block, filter or manipulate the traffic generated by other programs on the same machine: it simply "sniffs" the packets that transit on the wire. Therefore, it does not provide the appropriate support for applications like traffic shapers, QoS schedulers and personal firewalls. 1. Obtain the list of network interfaces To capture packets from a network, obtain the list of network interfaces. JpcapCaptor.getDeviceList() It returns an array of NetworkInterface objects. A NetworkInterface object contains some information about the corresponding network interface, such as its name, description, IP and MAC addresses, and datatlink name and description. 2. Open a network interface Choose which network interface to captuer packets from, open the interface by using JpcapCaptor.openDevice() method. JpcapCaptor.openDevice() The following piece of code illustrates how to open an network interface Name: Purpose NetworkInterface intrface Network interface that you want to open. int snaplen Max number of bytes to capture at once. boolean promics True if you want to open the interface in promiscuous mode, and otherwise false.
  • 5. In promiscuous mode, you can capture packets every packet from the wire In non-promiscuous mode, you can only capture packets send and received by your host. int to_ms Set a capture timeout value in milliseconds. 3. Capture packets from the network interface There are two major approaches to capture packets using a JpcapCaptor instance: using a callback method, and capturing packets one-by-one. Capturing packets one-by-one capture packets using the JpcapCaptor.getPacket() method. getPacket() method simply returns a captured packet. getPacket() method multiple times to capture consecutive packets. 4. Set capturing filter In Jpcap, you can set a filter so that Jpcap doesn't capture unwanted packets. For example, if you only want to capture TCP/IPv4 packets, you can set a filter as following: The filter expression "ip and tcp" means to to "keep only the packets that are both IPv4 and TCP and deliver them to the application". By properly setting a filter, you can reduce the number of packets to examine, and thus can improve the performance of your application.