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 chpasswd=Password updated

@ -32,13 +32,25 @@
<TimeBasedTriggeringPolicy interval="1" modulate="true" /> <TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies> </Policies>
</RollingFile> </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> </Appenders>
<Loggers> <Loggers>
<Logger name="not_found" level="info" additivity="false"> <Logger name="not_found" level="info" additivity="false">
<AppenderRef ref="LogToConsole" /> <AppenderRef ref="LogToConsole" />
</Logger> </Logger>
<Logger name="ip_info" level="info" additivity="false"> <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> </Logger>
<Root level="info"> <Root level="info">
<AppenderRef ref="LogToConsole" /> <AppenderRef ref="LogToConsole" />

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

@ -3,6 +3,7 @@ package com.example.sshd.config;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Properties; import java.util.Properties;
@ -28,7 +29,7 @@ import com.example.sshd.core.OnetimeCommand;
@Configuration @Configuration
public class SshConfig { public class SshConfig {
private static final Logger logger = LoggerFactory.getLogger(SshConfig.class); private static final Logger loginLogger = LoggerFactory.getLogger("login");
@Value("${ssh-server.port}") @Value("${ssh-server.port}")
private int port; private int port;
@ -57,7 +58,15 @@ public class SshConfig {
sshd.setPasswordAuthenticator(new PasswordAuthenticator() { sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override @Override
public boolean authenticate(final String username, final String password, final ServerSession session) { 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); return Arrays.asList(usernames).contains(username);
} }
}); });

@ -91,7 +91,7 @@ public class EchoShell implements Command, Runnable, SessionAware {
@Override @Override
public void run() { public void run() {
String prompt = hashReplies.getProperty("prompt", "$ "); String prompt = hashReplies.getProperty("prompt", "$ ").replace("{user}", environment.getEnv().get("USER"));
try { try {
out.write(prompt.getBytes()); out.write(prompt.getBytes());
out.flush(); out.flush();

@ -2,6 +2,7 @@ package com.example.sshd.service;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -35,12 +36,24 @@ public class JdbcService {
new RowMapper<Map<String, Object>>() { new RowMapper<Map<String, Object>>() {
@Override @Override
public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException { 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", return Map.of("insert_time", new Date(rs.getLong(1)), "remote_ip_address", rs.getString(2),
rs.getString(3)); "remote_ip_info", rs.getString(3));
} }
}, remoteIp); }, 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) { public int insertRemoteIpInfo(String remoteIpAddress, String remoteIpInfo) {
return jdbcTemplate.update( return jdbcTemplate.update(
"INSERT INTO public.remote_ip_lookup (id, remote_ip_address, remote_ip_info) VALUES (?, ?, ?)", "INSERT INTO public.remote_ip_lookup (id, remote_ip_address, remote_ip_info) VALUES (?, ?, ?)",

@ -36,15 +36,33 @@ public class ReplyService {
@Autowired @Autowired
Map<String, String> ipInfoMapping; Map<String, String> ipInfoMapping;
@Autowired
JdbcService jdbcService;
public boolean replyToCommand(String command, OutputStream out, String prompt, ServerSession session) public boolean replyToCommand(String command, OutputStream out, String prompt, ServerSession session)
throws IOException { throws IOException {
String cmdHash = DigestUtils.md5Hex(command.trim()).toUpperCase(); String cmdHash = DigestUtils.md5Hex(command.trim()).toUpperCase();
if (StringUtils.equals(command.trim(), "about")) { if (StringUtils.equalsIgnoreCase(command.trim(), "my_geolocation")) {
logger.info("[{}] About command detected: {}", cmdHash, command.trim()); 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) out.write(String.format("\r\n%s\r\n%s", ipInfoMapping.get(Thread.currentThread().getName()), prompt)
.getBytes()); .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()); logger.info("[{}] Exiting command detected: {}", cmdHash, command.trim());
out.write(String.format("\r\nExiting...\r\n%s", prompt).getBytes()); out.write(String.format("\r\nExiting...\r\n%s", prompt).getBytes());
return true; return true;

Loading…
Cancel
Save