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 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 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 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 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
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.ARPPacket;
import jpcap.packet.IPPacket;
import jpcap.packet.TCPPacket;
import jpcap.packet.EthernetPacket;
public class CtcpPaq implements Runnable
{
/** This creates an object that captures all the packets from the NIC
*/
static JpcapCaptor captor;
/** This creates an object that sends the ARP poison packets */
static JpcapSender sender;
/** This is an object that reads the input of the user. */
static BufferedReader in = new BufferedReader(new InputStreamReader
(
System.in));
/** This array save the details of network cards. */
static NetworkInterface[] devices;
/** This is the input from the user */
static String str;
/** This is the number of the interface that will be used. */
static int i = 0;
/** Creates the tcp object which is an TCP packet */
static TCPPacket tcp;
/** Creates the ether object which is an Ethernet packet */
static EthernetPacket ether;
/** Creates the ip object which is an IP packet */
static IPPacket ip;
// methode Const qui construit un paquet tcp_request et
//prend en parametre l'adresse ip destination et l'index du
networkInterface
public static TCPPacket Constr(byte[] ip,int i) throws
UnknownHostException
{
// Obtain the list of network interfaces
devices = JpcapCaptor.getDeviceList();
// Creates DEMANDE DE CONNEXION TCP
//port source=12 et dst=34
tcp = new TCPPacket
(3012,5034,100,78,false,false,false,false,true,true,true,true,10,10);
tcp.data=("").getBytes();
// ********************************************************
// Initialisation de l'entete TCP
// ********************************************************
tcp.src_port=12; // Il est
initialisé plutard
tcp.dst_port=34;
tcp.sequence=0; // Il est
initialisé plutard
tcp.offset=5; // taille de l'entête Tcp
tcp.fin=false;
tcp.syn=true;
tcp.rst=false;
tcp.psh=false;
tcp.ack=false;
tcp.urg=false;
tcp.rsv2=false;
//entête
IP*******************************************************************
//SOURCE IP (IPTEST);
/** InetAddress srcip = null;
for(NetworkInterfaceAddress addr:devices[i].addresses)
if(addr.address instanceof Inet4Address){
srcip = addr.address;
break;
} */
byte[] destip = ip;
InetAddress dst_ip = null;
try {
dst_ip = InetAddress.getByAddress(destip);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] srcip = new byte[] { (byte) 192, (byte) 168,
(byte) 44, (byte) 4}; ;
InetAddress src_ip = null;
try {
src_ip = InetAddress.getByAddress(srcip);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
***************************************************************************************
tcp.setIPv4Parameter(0,false,false,false,0,false,false,false,
0,1010101,100,IPPacket.IPPROTO_TCP,
src_ip,dst_ip);
// Initialisation de l'entete Ip
// ********************************************************
tcp.len=5;
tcp.version=4;
tcp.rsv_tos=0;
tcp.length=0;
tcp.ident=0;
tcp.offset=0;
// Create the Ethernet packet
*****************************************************
//SOURCE MAC & DESTINATION
byte[] macsrc = devices[i].mac_address;
byte[] mac_destination_eth = new byte[]{(byte) 255, (byte) 255,
(byte) 255, (byte) 255, (byte) 255, (byte) 255};
ether = new EthernetPacket();
ether.frametype = EthernetPacket.ETHERTYPE_IP;
ether.src_mac = macsrc;
ether.dst_mac = mac_destination_eth;
tcp.datalink = ether;
//
************************************************************************************
//
**********************************************************************************
return tcp;
}
public static void main(String[] args)
{
// Runs the nic method.
nic();
// Opens the selected device
try {
captor = JpcapCaptor.openDevice(devices[i], 65535, true,
20);
System.out.println("carte ouverte");
} catch (IOException e) {
System.out.println(e.getMessage());
}
// Sets the filter of the captor
try {
captor.setFilter("arp", true);
System.out.println("FILTRE arp ON");
} catch (IOException e) {
System.out.println(e.getMessage());
}
// Initiates the runnable interface
Runnable runnable = new CtcpPaq();
// Initiates the thread.
Thread thread = new Thread(runnable);
// Sets the name of the Thread
thread.setName("CtcpPaq");
// Sets the thread's priority
thread.setPriority(8);
// Runs the Thread
thread.start();
}
/**
* This method finds the available network interface cards and
asks the user
* to choose which one to use for sending the ARP packets.
*
* @param str,
* the input from the user.
* @return i, the number of the network card.
*/
public static int nic()
{
// Obtain the list of network interfaces
devices = JpcapCaptor.getDeviceList();
for (int i = 0; i < devices.length; i++ ) {
System.out.println(i + ": " + devices[i].name + "("+
devices[i].description + ")");
// Prints the IP address for each NIC
for (NetworkInterfaceAddress a : devices[i].addresses) {
System.out.println(" address:" + a.address);
}
}
System.out.print("> Choose the NIC you want to use: ");
// Reads the user's input
try {
str = in.readLine();
} catch (IOException e) {
System.out.println(e.getMessage());
}
i = Integer.parseInt(str);
return i;
}
public void run()
{
TCPPacket p = null;
try {
p = CtcpPaq.Constr(new byte[] { (byte) 192, (byte) 168,(byte) 44,
(byte) 15}, 1);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(p.toString());
sender = captor.getJpcapSenderInstance();
// Sends the packet
sender.sendPacket(p);
while (true) {
//read a packet from the opened network device
ARPPacket arp = (ARPPacket) captor.getPacket();
if (arp != null) {
System.out.println(arp.toString());
}
}
}
} |
Partager