1 package ZClient; 2 3 import java.security.MessageDigest; 4 import java.security.NoSuchAlgorithmException; 5 import jpcap.JpcapSender; 6 import jpcap.packet.EthernetPacket; 7 import jpcap.packet.Packet; 8 /** 9 * 802.1x packet sender 10 * @author Lingle<p> 11 * Email: mea08@126.com<p> 12 */ 13 public class EAPSender { 14 15 private final EthernetPacket etherpacket = new EthernetPacket(); 16 private byte[] localIp =null; 17 private JpcapSender capSender =null; 18 private byte[] username =null; 19 private byte[] password =null; 20 private final byte[] lenovoMAC = { 0x01, (byte) 0x80, (byte) 0xc2, 0x00, 0x00, 0x03 }; 21 22 public class EAPNameExcetion extends Exception { 23 private static final long serialVersionUID = 4518311057238635826L; 24 public EAPNameExcetion(String e) { 25 super(e + ": No Name or Password!"); 26 } 27 } 28 29 public EAPSender() {} 30 31 public void setSender(JpcapSender sender, byte[] ip, byte[] mac){ 32 etherpacket.frametype = (short) 0x888e; 33 etherpacket.src_mac = mac; 34 etherpacket.dst_mac = lenovoMAC; 35 localIp = ip; 36 capSender = sender; 37 } 38 39 public void setName(String name, String pass) { 40 if (name!=null) username = name.getBytes(); 41 if (pass!=null) password = pass.getBytes(); 42 } 43 44 public boolean ckeckName() { 45 if (username==null || password==null) { 46 return false; 47 } 48 if (username.length==0 || password.length==0) { 49 return false; 50 } 51 return true; 52 } 53 54 private byte[] MD5PwFill(byte id, byte[] attachkey) { 55 byte md5pw[] = new byte[16]; 56 byte pwkey[] = new byte[17 + password.length]; 57 pwkey[0] = id; 58 System.arraycopy(password, 0, pwkey, 1, password.length); 59 System.arraycopy(attachkey, 0, pwkey, 1 + password.length, 16);// MD5-Attach-KEY 60 try { 61 md5pw = MessageDigest.getInstance("MD5").digest(pwkey); 62 } catch (NoSuchAlgorithmException e) { 63 e.printStackTrace(); 64 } 65 return md5pw; 66 } 67 68 public void sendEapStart() throws EAPNameExcetion { 69 if (ckeckName()) { 70 Packet EAP_start = new Packet(); 71 EAP_start.data = new byte[50]; 72 byte startdata[] = { 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2f,(byte) 0xfc, 0x03, 0x01 }; 73 System.arraycopy(startdata, 0, EAP_start.data, 0, startdata.length); 74 EAP_start.datalink = etherpacket; 75 capSender.sendPacket(EAP_start); 76 } else { 77 throw new EAPNameExcetion("Error to send EAP_START"); 78 } 79 } 80 81 public void sendEapOnline() { 82 Packet EAP_online = new Packet(); 83 EAP_online.data = new byte[50];// 64-14 84 byte[] onlinedata = { 0x01, (byte) 0xfc, 0x00, 0x0c };// 0xfc=252 85 System.arraycopy(onlinedata, 0, EAP_online.data, 0, onlinedata.length); 86 System.arraycopy(localIp, 0, EAP_online.data, onlinedata.length + 8, 87 localIp.length);// ip offset:8 byte 88 EAP_online.datalink = etherpacket; 89 capSender.sendPacket(EAP_online); 90 } 91 92 public void sendEapLogOff() { 93 Packet EAP_logoff = new Packet(); 94 EAP_logoff.data = new byte[50];// 64-14 95 byte logoffdata[] = { 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, (byte) 0x2f, 96 (byte) 0xfc, 0x00, 0x00 }; 97 System.arraycopy(logoffdata, 0, EAP_logoff.data, 0, logoffdata.length); 98 EAP_logoff.datalink = etherpacket; 99 capSender.sendPacket(EAP_logoff); 100 } 101 102 public void sendEapName(byte id) throws EAPNameExcetion { 103 if (ckeckName()) { 104 Packet EAP_UserName = new Packet(); 105 EAP_UserName.data = new byte[50];// 64-14 106 short bodylen = (short) (username.length + 5);// Extensible Authentication Protocol Length 107 byte[] namehead = { 0x01, 0x00, (byte) (bodylen >> 8), 108 (byte) bodylen, 0x02, id, (byte) (bodylen >> 8), 109 (byte) bodylen, 0x01 }; 110 111 System.arraycopy(namehead, 0, EAP_UserName.data, 0, namehead.length); 112 System.arraycopy(username, 0, EAP_UserName.data, namehead.length, 113 username.length); 114 115 EAP_UserName.datalink = etherpacket; 116 capSender.sendPacket(EAP_UserName); 117 } else { 118 throw new EAPNameExcetion("Error to send EAP_USERNAME"); 119 } 120 } 121 122 public void sendEapPassWord(byte id, byte[] attachkey)throws EAPNameExcetion { 123 if (ckeckName()) { 124 Packet EAP_PassWord = new Packet(); 125 byte passtail[] = { 0x00, 0x00, 0x2f, (byte) 0xfc, 0x00, 0x15, 126 0x01, 0x01, 0x00 }; 127 short bodylen = (short) (username.length + 22);// Extensible Authentication Protocol Length 128 byte[] namehead = { 0x01, 0x00, (byte) (bodylen >> 8), 129 (byte) bodylen, 0x02, id, (byte) (bodylen >> 8), 130 (byte) bodylen, 0x04, 0x10 }; 131 byte[] md5 = MD5PwFill(id, attachkey); 132 EAP_PassWord.data = new byte[bodylen + 35]; 133 System.arraycopy(namehead, 0, EAP_PassWord.data, 0, namehead.length); 134 System.arraycopy(md5, 0, EAP_PassWord.data, namehead.length, 135 md5.length); 136 System.arraycopy(username, 0, EAP_PassWord.data, namehead.length 137 + md5.length, username.length); 138 System.arraycopy(localIp, 0, EAP_PassWord.data, namehead.length 139 + md5.length + username.length, localIp.length); 140 System.arraycopy(passtail, 0, EAP_PassWord.data, namehead.length 141 + md5.length + username.length + localIp.length, 142 passtail.length); 143 144 EAP_PassWord.datalink = etherpacket; 145 capSender.sendPacket(EAP_PassWord); 146 } else { 147 throw new EAPNameExcetion(" Error to send EAP_PASSWORD"); 148 } 149 } 150 151 }
浙公网安备 33010602011771号