Skip to content

Commit

Permalink
进一步优化了TCP/UDP请求包的解析,增加了对SSH协议的支持
Browse files Browse the repository at this point in the history
Signed-off-by: Sadam·Sadik <[email protected]>
  • Loading branch information
Haoke98 committed Nov 11, 2024
1 parent 2cef711 commit 5ccb3c8
Showing 1 changed file with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(protocol).append(" - ");

if ("PostgreSQL".equals(protocol)) {
if ("SSH".equals(protocol)) {
sb.append("Type: ").append(attributes.get("messageType"));
if (attributes.containsKey("version")) {
sb.append(", Version: ").append(attributes.get("version"));
}
if (attributes.containsKey("software")) {
sb.append(", Client: ").append(attributes.get("software"));
}
} else if ("PostgreSQL".equals(protocol)) {
sb.append("Type: ").append(attributes.get("messageType"));
if ("StartupMessage".equals(attributes.get("messageType"))) {
sb.append(", Version: ").append(attributes.get("protocolVersion"));
Expand Down Expand Up @@ -86,7 +94,9 @@ public static PacketInfo parsePacket(ByteBuf buf) {
}

// 检测协议
if (isHttpRequest(buf)) {
if (isSSHPacket(buf)) {
parseSSHPacket(buf, info);
} else if (isHttpRequest(buf)) {
parseHttpRequest(buf, info);
} else if (isSslRequest(buf)) {
parseSslRequest(buf, info);
Expand Down Expand Up @@ -220,6 +230,34 @@ private static void parsePostgreSQLPacket(ByteBuf buf, PacketInfo info) {
}
}

private static boolean isSSHPacket(ByteBuf buf) {
if (buf.readableBytes() < 4) return false;

// SSH协议以"SSH-"开头
byte[] header = new byte[4];
buf.getBytes(buf.readerIndex(), header);
return new String(header).equals("SSH-");
}

private static void parseSSHPacket(ByteBuf buf, PacketInfo info) {
info.setProtocol("SSH");

// 读取SSH版本信息
String content = buf.toString(StandardCharsets.UTF_8);
String[] parts = content.split("-", 3);
if (parts.length >= 3) {
// 格式通常是: SSH-2.0-OpenSSH_8.1
String version = parts[1];
String software = parts[2].split("\\r?\\n")[0];

info.getAttributes().put("version", version);
info.getAttributes().put("software", software);
info.getAttributes().put("messageType", "Version Exchange");
}

info.getAttributes().put("size", buf.readableBytes());
}

// 其他辅助方法...
private static boolean isHttpRequest(ByteBuf buf) {
if (buf.readableBytes() < 4) return false;
Expand Down

0 comments on commit 5ccb3c8

Please sign in to comment.