Python实现

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. # @time:2022/01/12
  4. # @author: xsshim
  5. import socket
  6. import re
  7. class WebLogic:
  8. def __init__(self, url, port):
  9. self.ip = url
  10. self.port = port
  11. self.sock = self.get_sock(self.ip, self.port)
  12. self.t3_info = self.t3_hand()
  13. self.t3_version = self.regx_ver(self.t3_info.decode())
  14. self.t3_close()
  15. @staticmethod
  16. def get_sock(ip, port):
  17. try:
  18. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19. server_address = (ip, port)
  20. sock.connect(server_address)
  21. sock.settimeout(10)
  22. print("[INFO]: connecting to %s port %s" % server_address)
  23. return sock
  24. except Exception as e:
  25. print("[ERROR]: " + str(e))
  26. def t3_hand(self):
  27. try:
  28. handshake = b't3 10.3.6\nAS:255\nHL:19\nMS:10000000\n\n'
  29. print("[INFO]: sending handshake packet ...")
  30. self.sock.sendall(handshake)
  31. print("[INFO]: received handshake data")
  32. data0 = self.sock.recv(1024)
  33. print(data0)
  34. if len(data0) < 5:
  35. data1 = self.sock.recv(1024)
  36. print(data1)
  37. return data1
  38. return data0
  39. except:
  40. return b''
  41. @staticmethod
  42. def regx_ver(info):
  43. try:
  44. version = re.findall(r":(.*).false", info)[0]
  45. return version
  46. except:
  47. return '无法获取版本信息'
  48. def t3_close(self):
  49. self.sock.close()
  50. if __name__ == '__main__':
  51. print("________________________________________________________________________")
  52. wb = WebLogic("192.168.23.119", 7003)
  53. print("该 Weblogic 的版本为 " + wb.t3_version)
  54. print("________________________________________________________________________")

Java实现

  1. import cn.hutool.core.util.ReUtil;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.Socket;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class WeblogicVersionByT3 {
  9. public static byte[] hexStrToBinaryStr(String hexString) {
  10. hexString = hexString.replaceAll(" ", "");
  11. int len = hexString.length();
  12. int index = 0;
  13. byte[] bytes = new byte[len / 2];
  14. while (index < len) {
  15. String sub = hexString.substring(index, index + 2);
  16. bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
  17. index += 2;
  18. }
  19. return bytes;
  20. }
  21. public static String getVersion(String content) {
  22. content = content.replace("HELO:", "").replace(".false", "").replace(".true", "");
  23. String getVersionRegex = "[\\d\\.]+";
  24. List<String> result = ReUtil.findAll(getVersionRegex, content, 0, new ArrayList<String>());
  25. return result != null && result.size() > 0 ? result.get(0) : "";
  26. }
  27. public static String byteToHex(byte num) {
  28. char[] hexDigits = new char[2];
  29. hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
  30. hexDigits[1] = Character.forDigit((num & 0xF), 16);
  31. return new String(hexDigits);
  32. }
  33. public static String encodeHexString(byte[] byteArray) {
  34. StringBuffer hexStringBuffer = new StringBuffer();
  35. for (int i = 0; i < byteArray.length; i++) {
  36. hexStringBuffer.append(byteToHex(byteArray[i]));
  37. }
  38. return hexStringBuffer.toString();
  39. }
  40. private static String asciiToHex(String asciiValue) {
  41. byte[] bytes = asciiValue.getBytes();
  42. // return Hex.encodeHexString(bytes);
  43. return encodeHexString(bytes);
  44. }
  45. public static byte hexToByte(String hexString) {
  46. int firstDigit = toDigit(hexString.charAt(0));
  47. int secondDigit = toDigit(hexString.charAt(1));
  48. return (byte) ((firstDigit << 4) + secondDigit);
  49. }
  50. private static int toDigit(char hexChar) {
  51. int digit = Character.digit(hexChar, 16);
  52. if (digit == -1) {
  53. throw new IllegalArgumentException("Invalid Hexadecimal Character: " + hexChar);
  54. }
  55. return digit;
  56. }
  57. public static byte[] decodeHexString(String hexString) {
  58. if (hexString.length() % 2 == 1) {
  59. throw new IllegalArgumentException("Invalid hexadecimal String supplied.");
  60. }
  61. byte[] bytes = new byte[hexString.length() / 2];
  62. for (int i = 0; i < hexString.length(); i += 2) {
  63. bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
  64. }
  65. return bytes;
  66. }
  67. private static String hexToAscii(String hexString) {
  68. byte[] bytes = null;
  69. // bytes = Hex.decodeHex(hexString);
  70. bytes = decodeHexString(hexString);
  71. String s = new String(bytes);
  72. return s;
  73. }
  74. public static void main(String[] args) throws Exception {
  75. String msg = "t3 12.2.1\n" + "AS:255\n" + "HL:19\n" + "MS:10000000\n" + "PU:t3://us-l-breens:7001\n\n";
  76. System.out.println(
  77. "74332031322e322e310a41533a3235350a484c3a31390a4d533a31303030303030300a50553a74333a2f2f75732d6c2d627265656e733a373030310a0a");
  78. System.out.println(asciiToHex(msg));
  79. System.out.println(hexToAscii(asciiToHex(msg)));
  80. String version = "";
  81. try {
  82. Socket socket = new Socket("127.0.0.1", 7001);
  83. OutputStream out = socket.getOutputStream();
  84. InputStream is = socket.getInputStream();
  85. out.write(hexStrToBinaryStr(asciiToHex(msg)));
  86. out.flush();
  87. Thread.sleep(1);
  88. byte[] bytes = new byte[4096];
  89. int length = is.read(bytes);
  90. byte[] rspByte = Arrays.copyOfRange(bytes, 0, length);
  91. socket.close();
  92. System.out.println(new String(rspByte));
  93. version = getVersion(new String(rspByte));
  94. } catch (Exception e) {
  95. version = "";
  96. }
  97. System.out.println("weblogic的版本是" + version);
  98. }
  99. }

需要加入maven库

  1. <dependencies>
  2. <dependency>
  3. <groupId>cn.hutool</groupId>
  4. <artifactId>hutool-all</artifactId>
  5. <version>4.5.16</version>
  6. </dependency>
  7. </dependencies>