package ZClient;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import jpcap.JpcapSender;
import jpcap.packet.EthernetPacket;
import jpcap.packet.Packet;
/**
* 802.1x packet sender
* @author Lingle<p>
* Email: mea08@126.com<p>
*/
public class EAPSender {
private final EthernetPacket etherpacket = new EthernetPacket();
private byte[] localIp =null;
private JpcapSender capSender =null;
private byte[] username =null;
private byte[] password =null;
private final byte[] lenovoMAC = { 0x01, (byte) 0x80, (byte) 0xc2, 0x00, 0x00, 0x03 };
public class EAPNameExcetion extends Exception {
private static final long serialVersionUID = 4518311057238635826L;
public EAPNameExcetion(String e) {
super(e + ": No Name or Password!");
}
}
public EAPSender() {}
public void setSender(JpcapSender sender, byte[] ip, byte[] mac){
etherpacket.frametype = (short) 0x888e;
etherpacket.src_mac = mac;
etherpacket.dst_mac = lenovoMAC;
localIp = ip;
capSender = sender;
}
public void setName(String name, String pass) {
if (name!=null) username = name.getBytes();
if (pass!=null) password = pass.getBytes();
}
public boolean ckeckName() {
if (username==null || password==null) {
return false;
}
if (username.length==0 || password.length==0) {
return false;
}
return true;
}
private byte[] MD5PwFill(byte id, byte[] attachkey) {
byte md5pw[] = new byte[16];
byte pwkey[] = new byte[17 + password.length];
pwkey[0] = id;
System.arraycopy(password, 0, pwkey, 1, password.length);
System.arraycopy(attachkey, 0, pwkey, 1 + password.length, 16);// MD5-Attach-KEY
try {
md5pw = MessageDigest.getInstance("MD5").digest(pwkey);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return md5pw;
}
public void sendEapStart() throws EAPNameExcetion {
if (ckeckName()) {
Packet EAP_start = new Packet();
EAP_start.data = new byte[50];
byte startdata[] = { 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2f,(byte) 0xfc, 0x03, 0x01 };
System.arraycopy(startdata, 0, EAP_start.data, 0, startdata.length);
EAP_start.datalink = etherpacket;
capSender.sendPacket(EAP_start);
} else {
throw new EAPNameExcetion("Error to send EAP_START");
}
}
public void sendEapOnline() {
Packet EAP_online = new Packet();
EAP_online.data = new byte[50];// 64-14
byte[] onlinedata = { 0x01, (byte) 0xfc, 0x00, 0x0c };// 0xfc=252
System.arraycopy(onlinedata, 0, EAP_online.data, 0, onlinedata.length);
System.arraycopy(localIp, 0, EAP_online.data, onlinedata.length + 8,
localIp.length);// ip offset:8 byte
EAP_online.datalink = etherpacket;
capSender.sendPacket(EAP_online);
}
public void sendEapLogOff() {
Packet EAP_logoff = new Packet();
EAP_logoff.data = new byte[50];// 64-14
byte logoffdata[] = { 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, (byte) 0x2f,
(byte) 0xfc, 0x00, 0x00 };
System.arraycopy(logoffdata, 0, EAP_logoff.data, 0, logoffdata.length);
EAP_logoff.datalink = etherpacket;
capSender.sendPacket(EAP_logoff);
}
public void sendEapName(byte id) throws EAPNameExcetion {
if (ckeckName()) {
Packet EAP_UserName = new Packet();
EAP_UserName.data = new byte[50];// 64-14
short bodylen = (short) (username.length + 5);// Extensible Authentication Protocol Length
byte[] namehead = { 0x01, 0x00, (byte) (bodylen >> 8),
(byte) bodylen, 0x02, id, (byte) (bodylen >> 8),
(byte) bodylen, 0x01 };
System.arraycopy(namehead, 0, EAP_UserName.data, 0, namehead.length);
System.arraycopy(username, 0, EAP_UserName.data, namehead.length,
username.length);
EAP_UserName.datalink = etherpacket;
capSender.sendPacket(EAP_UserName);
} else {
throw new EAPNameExcetion("Error to send EAP_USERNAME");
}
}
public void sendEapPassWord(byte id, byte[] attachkey)throws EAPNameExcetion {
if (ckeckName()) {
Packet EAP_PassWord = new Packet();
byte passtail[] = { 0x00, 0x00, 0x2f, (byte) 0xfc, 0x00, 0x15,
0x01, 0x01, 0x00 };
short bodylen = (short) (username.length + 22);// Extensible Authentication Protocol Length
byte[] namehead = { 0x01, 0x00, (byte) (bodylen >> 8),
(byte) bodylen, 0x02, id, (byte) (bodylen >> 8),
(byte) bodylen, 0x04, 0x10 };
byte[] md5 = MD5PwFill(id, attachkey);
EAP_PassWord.data = new byte[bodylen + 35];
System.arraycopy(namehead, 0, EAP_PassWord.data, 0, namehead.length);
System.arraycopy(md5, 0, EAP_PassWord.data, namehead.length,
md5.length);
System.arraycopy(username, 0, EAP_PassWord.data, namehead.length
+ md5.length, username.length);
System.arraycopy(localIp, 0, EAP_PassWord.data, namehead.length
+ md5.length + username.length, localIp.length);
System.arraycopy(passtail, 0, EAP_PassWord.data, namehead.length
+ md5.length + username.length + localIp.length,
passtail.length);
EAP_PassWord.datalink = etherpacket;
capSender.sendPacket(EAP_PassWord);
} else {
throw new EAPNameExcetion(" Error to send EAP_PASSWORD");
}
}
}
转载于:https://my.oschina.net/u/212925/blog/106842