V1.4.2 Added geolocation query commands

master
Ng Yat Yan 1 month ago
parent 45bbb03bd9
commit 8c30a60134

@ -1,2 +1,2 @@
prompt=$
prompt={user}$
chpasswd=Password updated

@ -32,13 +32,25 @@
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<RollingFile name="LogToLogin"
filePattern="logs/login.%d{yyyy-MM-dd}.log"
immediateFlush="true">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %p [%t] %c : %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="not_found" level="info" additivity="false">
<AppenderRef ref="LogToConsole" />
</Logger>
<Logger name="ip_info" level="info" additivity="false">
<AppenderRef ref="LogToIpInfo" />
<AppenderRef ref="LogToConsole" />
</Logger>
<Logger name="login" level="info" additivity="false">
<AppenderRef ref="LogToConsole" />
</Logger>
<Root level="info">
<AppenderRef ref="LogToConsole" />

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.sshd</groupId>
<artifactId>echo-sshd-server</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
<name>ECHO SSH SERVER</name>
<description>Learning Apache Mina SSHD library</description>
<parent>

@ -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);
}
});

@ -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();

@ -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<Map<String, Object>>() {
@Override
public Map<String, Object> 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<Map<String, Object>> getAllRemoteIpInfo() {
return jdbcTemplate.query(
"SELECT id, remote_ip_address, remote_ip_info from public.remote_ip_lookup order by id",
new RowMapper<Map<String, Object>>() {
@Override
public Map<String, Object> 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 (?, ?, ?)",

@ -36,15 +36,33 @@ public class ReplyService {
@Autowired
Map<String, String> 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;

Loading…
Cancel
Save