From 8c30a60134a274cf5c965b867f47f4d05a4986b8 Mon Sep 17 00:00:00 2001 From: Ng Yat Yan Date: Sun, 20 Oct 2024 12:11:36 +0800 Subject: [PATCH] V1.4.2 Added geolocation query commands --- conf/hash-replies.properties | 2 +- conf/log4j2.xml | 14 ++++++++++- pom.xml | 2 +- .../com/example/sshd/config/SshConfig.java | 13 ++++++++-- .../java/com/example/sshd/core/EchoShell.java | 2 +- .../com/example/sshd/service/JdbcService.java | 17 +++++++++++-- .../example/sshd/service/ReplyService.java | 24 ++++++++++++++++--- 7 files changed, 63 insertions(+), 11 deletions(-) diff --git a/conf/hash-replies.properties b/conf/hash-replies.properties index 45fa65f..ff69402 100644 --- a/conf/hash-replies.properties +++ b/conf/hash-replies.properties @@ -1,2 +1,2 @@ -prompt=$ +prompt={user}$ chpasswd=Password updated \ No newline at end of file diff --git a/conf/log4j2.xml b/conf/log4j2.xml index d0cc96c..530126a 100644 --- a/conf/log4j2.xml +++ b/conf/log4j2.xml @@ -32,13 +32,25 @@ + + + + + + - + + + + diff --git a/pom.xml b/pom.xml index 0a2c180..b307b2e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.example.sshd echo-sshd-server - 1.4.1 + 1.4.2 ECHO SSH SERVER Learning Apache Mina SSHD library diff --git a/src/main/java/com/example/sshd/config/SshConfig.java b/src/main/java/com/example/sshd/config/SshConfig.java index ede4d7e..e8b2259 100644 --- a/src/main/java/com/example/sshd/config/SshConfig.java +++ b/src/main/java/com/example/sshd/config/SshConfig.java @@ -3,6 +3,7 @@ package com.example.sshd.config; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.net.InetSocketAddress; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.Properties; @@ -28,7 +29,7 @@ import com.example.sshd.core.OnetimeCommand; @Configuration public class SshConfig { - private static final Logger logger = LoggerFactory.getLogger(SshConfig.class); + private static final Logger loginLogger = LoggerFactory.getLogger("login"); @Value("${ssh-server.port}") private int port; @@ -57,7 +58,15 @@ public class SshConfig { sshd.setPasswordAuthenticator(new PasswordAuthenticator() { @Override public boolean authenticate(final String username, final String password, final ServerSession session) { - logger.info("Login Attempt: username = {}, password = {}", username, password); + if (session.getIoSession().getRemoteAddress() instanceof InetSocketAddress) { + InetSocketAddress remoteAddress = (InetSocketAddress) session.getIoSession().getRemoteAddress(); + String remoteIpAddress = remoteAddress.getAddress().getHostAddress(); + loginLogger.info("[{}] Login Attempt: username = {}, password = {}", remoteIpAddress, username, + password); + } else { + loginLogger.info("[{}] Login Attempt: username = {}, password = {}", + session.getIoSession().getRemoteAddress(), username, password); + } return Arrays.asList(usernames).contains(username); } }); diff --git a/src/main/java/com/example/sshd/core/EchoShell.java b/src/main/java/com/example/sshd/core/EchoShell.java index 085771b..b961777 100644 --- a/src/main/java/com/example/sshd/core/EchoShell.java +++ b/src/main/java/com/example/sshd/core/EchoShell.java @@ -91,7 +91,7 @@ public class EchoShell implements Command, Runnable, SessionAware { @Override public void run() { - String prompt = hashReplies.getProperty("prompt", "$ "); + String prompt = hashReplies.getProperty("prompt", "$ ").replace("{user}", environment.getEnv().get("USER")); try { out.write(prompt.getBytes()); out.flush(); diff --git a/src/main/java/com/example/sshd/service/JdbcService.java b/src/main/java/com/example/sshd/service/JdbcService.java index 24a611c..94d168e 100644 --- a/src/main/java/com/example/sshd/service/JdbcService.java +++ b/src/main/java/com/example/sshd/service/JdbcService.java @@ -2,6 +2,7 @@ package com.example.sshd.service; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Date; import java.util.List; import java.util.Map; @@ -35,12 +36,24 @@ public class JdbcService { new RowMapper>() { @Override public Map mapRow(ResultSet rs, int rowNum) throws SQLException { - return Map.of("id", rs.getLong(1), "remote_ip_address", rs.getString(2), "remote_ip_info", - rs.getString(3)); + return Map.of("insert_time", new Date(rs.getLong(1)), "remote_ip_address", rs.getString(2), + "remote_ip_info", rs.getString(3)); } }, remoteIp); } + public List> getAllRemoteIpInfo() { + return jdbcTemplate.query( + "SELECT id, remote_ip_address, remote_ip_info from public.remote_ip_lookup order by id", + new RowMapper>() { + @Override + public Map mapRow(ResultSet rs, int rowNum) throws SQLException { + return Map.of("insert_time", new Date(rs.getLong(1)), "remote_ip_address", rs.getString(2), + "remote_ip_info", rs.getString(3)); + } + }); + } + public int insertRemoteIpInfo(String remoteIpAddress, String remoteIpInfo) { return jdbcTemplate.update( "INSERT INTO public.remote_ip_lookup (id, remote_ip_address, remote_ip_info) VALUES (?, ?, ?)", diff --git a/src/main/java/com/example/sshd/service/ReplyService.java b/src/main/java/com/example/sshd/service/ReplyService.java index 6102ef3..a925caf 100644 --- a/src/main/java/com/example/sshd/service/ReplyService.java +++ b/src/main/java/com/example/sshd/service/ReplyService.java @@ -36,15 +36,33 @@ public class ReplyService { @Autowired Map ipInfoMapping; + @Autowired + JdbcService jdbcService; + public boolean replyToCommand(String command, OutputStream out, String prompt, ServerSession session) throws IOException { String cmdHash = DigestUtils.md5Hex(command.trim()).toUpperCase(); - if (StringUtils.equals(command.trim(), "about")) { - logger.info("[{}] About command detected: {}", cmdHash, command.trim()); + if (StringUtils.equalsIgnoreCase(command.trim(), "my_geolocation")) { + logger.info("[{}] my_geolocation command detected: {}", cmdHash, command.trim()); out.write(String.format("\r\n%s\r\n%s", ipInfoMapping.get(Thread.currentThread().getName()), prompt) .getBytes()); - } else if (StringUtils.equals(command.trim(), "exit")) { + } else if (StringUtils.equalsIgnoreCase(command.trim(), "whoami")) { + logger.info("[{}] whoami command detected: {}", cmdHash, command.trim()); + out.write(String.format("\r\n%s\r\n%s", session.getUsername(), prompt).getBytes()); + } else if (StringUtils.equalsIgnoreCase(command.trim(), "online_geolocations")) { + logger.info("[{}] online_geolocations command detected: {}", cmdHash, command.trim()); + out.write(String.format("\r\n%s\r\n%s", ipInfoMapping.toString(), prompt).getBytes()); + } else if (StringUtils.split(command.trim(), " ").length == 2 + && StringUtils.equalsIgnoreCase(StringUtils.split(command.trim(), " ")[0], "get_geolocation")) { + logger.info("[{}] get_geolocation command detected: {}", cmdHash, command.trim()); + out.write(String.format("\r\n%s\r\n%s", + jdbcService.getRemoteIpInfo(StringUtils.split(command.trim(), " ")[1]), prompt).getBytes()); + } else if (StringUtils.equalsIgnoreCase(command.trim(), "all_geolocations")) { + logger.info("[{}] all_geolocations command detected: {}", cmdHash, command.trim()); + out.write(String.format("\r\n%s\r\n%s", jdbcService.getAllRemoteIpInfo(), prompt).getBytes()); + } else if (StringUtils.equalsIgnoreCase(command.trim(), "exit") + || StringUtils.equalsIgnoreCase(command.trim(), "quit")) { logger.info("[{}] Exiting command detected: {}", cmdHash, command.trim()); out.write(String.format("\r\nExiting...\r\n%s", prompt).getBytes()); return true;