From 7714bcba1a5e7bb8c0fc2b1f00dd58ecc5b4347f Mon Sep 17 00:00:00 2001 From: Ng Yat Yan Date: Tue, 15 Oct 2024 13:35:14 +0800 Subject: [PATCH] V1.2.1 Allow base64 output in hash-replies.properties --- misc/start-jar.sh | 13 +++++++++++ misc/stop.sh | 9 ++++++++ pom.xml | 2 +- .../java/com/example/sshd/util/ReplyUtil.java | 23 ++++++++++++------- 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 misc/start-jar.sh create mode 100644 misc/stop.sh diff --git a/misc/start-jar.sh b/misc/start-jar.sh new file mode 100644 index 0000000..2ff7b54 --- /dev/null +++ b/misc/start-jar.sh @@ -0,0 +1,13 @@ +#!/bin/bash +PID=`ps -ef | grep echo-sshd-server.jar | grep -v grep | awk '{print $2}'` +if [ -z "$PID" ] +then + sudo nohup /opt/jdk-17/bin/java -jar echo-sshd-server.jar conf > logs/exec.log 2>&1 & + sleep 1 + PID=`ps -ef | grep echo-sshd-server.jar | grep -v grep | awk '{print $2}'` + echo "started echo-sshd-server.jar pid: $PID" +else + echo "already exist echo-sshd-server.jar pid: $PID" +fi + + diff --git a/misc/stop.sh b/misc/stop.sh new file mode 100644 index 0000000..9a23235 --- /dev/null +++ b/misc/stop.sh @@ -0,0 +1,9 @@ +#!/bin/bash +PID=`ps -ef | grep echo-sshd-server | grep -v grep | awk '{print $2}'` +if [ -z "$PID" ] +then + echo "echo-sshd-server is already dead! $PID" +else + sudo kill -9 $PID + echo "killed echo-sshd-server pid: $PID" +fi diff --git a/pom.xml b/pom.xml index da71e31..a135198 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.example.sshd echo-sshd-server - 1.2.0 + 1.2.1 ECHO SSH SERVER Learning Apache Mina SSHD library diff --git a/src/main/java/com/example/sshd/util/ReplyUtil.java b/src/main/java/com/example/sshd/util/ReplyUtil.java index 1250ceb..0515fd0 100644 --- a/src/main/java/com/example/sshd/util/ReplyUtil.java +++ b/src/main/java/com/example/sshd/util/ReplyUtil.java @@ -11,6 +11,7 @@ import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.sshd.server.session.ServerSession; +import org.bouncycastle.util.encoders.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -33,18 +34,19 @@ public class ReplyUtil { public boolean replyToCommand(String command, OutputStream out, String prompt, ServerSession session) throws IOException { - + String remoteIpAddress = ""; String cmdHash = DigestUtils.md5Hex(command.trim()).toUpperCase(); + if (session.getIoSession().getRemoteAddress() instanceof InetSocketAddress) { + InetSocketAddress remoteAddress = (InetSocketAddress) session.getIoSession().getRemoteAddress(); + remoteIpAddress = remoteAddress.getAddress().getHostAddress(); + } else { + remoteIpAddress = session.getIoSession().getRemoteAddress().toString(); + } + Thread.currentThread().setName(remoteIpAddress); if (StringUtils.equals(command.trim(), "about")) { logger.info("[{}] About command detected: {}", cmdHash, command.trim()); - if (session.getIoSession().getRemoteAddress() instanceof InetSocketAddress) { - InetSocketAddress remoteAddress = (InetSocketAddress) session.getIoSession().getRemoteAddress(); - String remoteIpAddress = remoteAddress.getAddress().getHostAddress(); - out.write(String.format("\r\n%s\r\n%s", ipInfoMapping.get(remoteIpAddress), prompt).getBytes()); - } else { - out.write(String.format("\r\n%s\r\n%s", session.getIoSession().getRemoteAddress(), prompt).getBytes()); - } + out.write(String.format("\r\n%s\r\n%s", ipInfoMapping.get(remoteIpAddress), prompt).getBytes()); } else if (StringUtils.equals(command.trim(), "exit")) { logger.info("[{}] Exiting command detected: {}", cmdHash, command.trim()); out.write(String.format("\r\nExiting...\r\n%s", prompt).getBytes()); @@ -59,6 +61,11 @@ public class ReplyUtil { String reply = hashReplies.getProperty(cmdHash).replace("\\r", "\r").replace("\\n", "\n").replace("\\t", "\t"); out.write(String.format("\r\n%s\r\n%s", reply, prompt).getBytes()); + } else if (hashReplies.containsKey(String.format("base64(%s)",cmdHash))) { + logger.info("[{}] Known base64-hash detected: {}", cmdHash, command.trim()); + String reply = hashReplies.getProperty(String.format("base64(%s)",cmdHash)); + reply = new String(Base64.decode(reply)); + out.write(String.format("\r\n%s\r\n%s", reply, prompt).getBytes()); } else { Optional> o = regexMapping.entrySet().stream() .filter(e -> command.trim().matches(((String) e.getKey())))