Search in sources :

Example 1 with SessionOutput

use of io.bastillion.manage.model.SessionOutput in project KeyBox by skavanagh.

the class SessionAuditDB method getTerminalLogsForSession.

/**
 * returns terminal logs for user session for host system
 *
 * @param sessionId  session id
 * @param instanceId instance id for terminal session
 * @return session output for session
 */
public static List<SessionOutput> getTerminalLogsForSession(Connection con, Long sessionId, Integer instanceId) throws SQLException {
    List<SessionOutput> outputList = new LinkedList<>();
    PreparedStatement stmt = con.prepareStatement("select * from terminal_log where instance_id=? and session_id=? order by log_tm asc");
    stmt.setLong(1, instanceId);
    stmt.setLong(2, sessionId);
    ResultSet rs = stmt.executeQuery();
    StringBuilder outputBuilder = new StringBuilder();
    while (rs.next()) {
        outputBuilder.append(rs.getString("output"));
    }
    String output = outputBuilder.toString();
    output = output.replaceAll("\\u0007|\u001B\\[K|\\]0;|\\[\\d\\d;\\d\\dm|\\[\\dm", "");
    while (output.contains("\b")) {
        output = output.replaceFirst(".\b", "");
    }
    DBUtils.closeRs(rs);
    SessionOutput sessionOutput = new SessionOutput();
    sessionOutput.setSessionId(sessionId);
    sessionOutput.setInstanceId(instanceId);
    sessionOutput.getOutput().append(output);
    outputList.add(sessionOutput);
    DBUtils.closeRs(rs);
    DBUtils.closeStmt(stmt);
    return outputList;
}
Also used : SessionOutput(io.bastillion.manage.model.SessionOutput) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LinkedList(java.util.LinkedList)

Example 2 with SessionOutput

use of io.bastillion.manage.model.SessionOutput in project KeyBox by skavanagh.

the class SessionOutputUtil method getOutput.

/**
 * returns list of output lines
 *
 * @param sessionId session id object
 * @param user      user auth object
 * @return session output list
 */
public static List<SessionOutput> getOutput(Connection con, Long sessionId, User user) throws SQLException {
    List<SessionOutput> outputList = new ArrayList<>();
    UserSessionsOutput userSessionsOutput = userSessionsOutputMap.get(sessionId);
    if (userSessionsOutput != null) {
        for (Integer key : userSessionsOutput.getSessionOutputMap().keySet()) {
            // get output chars and set to output
            SessionOutput sessionOutput = userSessionsOutput.getSessionOutputMap().get(key);
            if (sessionOutput != null && sessionOutput.getOutput() != null && StringUtils.isNotEmpty(sessionOutput.getOutput())) {
                outputList.add(sessionOutput);
                // send to audit logger
                systemAuditLogger.info(gson.toJson(new AuditWrapper(user, sessionOutput)));
                if (enableInternalAudit) {
                    SessionAuditDB.insertTerminalLog(con, sessionOutput);
                }
                userSessionsOutput.getSessionOutputMap().put(key, new SessionOutput(sessionId, sessionOutput));
            }
        }
    }
    return outputList;
}
Also used : AuditWrapper(io.bastillion.manage.model.AuditWrapper) SessionOutput(io.bastillion.manage.model.SessionOutput) UserSessionsOutput(io.bastillion.manage.model.UserSessionsOutput)

Example 3 with SessionOutput

use of io.bastillion.manage.model.SessionOutput in project KeyBox by skavanagh.

the class SessionAuditDB method getTerminalLogsForSession.

/**
 * returns terminal logs for user session for host system
 *
 * @param sessionId  session id
 * @param instanceId instance id for terminal session
 * @return session output for session
 */
public static List<SessionOutput> getTerminalLogsForSession(Long sessionId, Integer instanceId) throws SQLException, GeneralSecurityException {
    // get db connection
    Connection con = DBUtils.getConn();
    List<SessionOutput> outputList = getTerminalLogsForSession(con, sessionId, instanceId);
    DBUtils.closeConn(con);
    return outputList;
}
Also used : SessionOutput(io.bastillion.manage.model.SessionOutput) Connection(java.sql.Connection)

Example 4 with SessionOutput

use of io.bastillion.manage.model.SessionOutput in project KeyBox by skavanagh.

the class SSHUtil method openSSHTermOnSystem.

/**
 * open new ssh session on host system
 *
 * @param passphrase     key passphrase for instance
 * @param password       password for instance
 * @param userId         user id
 * @param sessionId      session id
 * @param hostSystem     host system
 * @param userSessionMap user session map
 * @return status of systems
 */
public static HostSystem openSSHTermOnSystem(String passphrase, String password, Long userId, Long sessionId, HostSystem hostSystem, Map<Long, UserSchSessions> userSessionMap) throws SQLException, GeneralSecurityException {
    JSch jsch = new JSch();
    int instanceId = getNextInstanceId(sessionId, userSessionMap);
    hostSystem.setStatusCd(HostSystem.SUCCESS_STATUS);
    hostSystem.setInstanceId(instanceId);
    SchSession schSession = null;
    try {
        ApplicationKey appKey = PrivateKeyDB.getApplicationKey();
        // check to see if passphrase has been provided
        if (passphrase == null || passphrase.trim().equals("")) {
            passphrase = appKey.getPassphrase();
            // check for null inorder to use key without passphrase
            if (passphrase == null) {
                passphrase = "";
            }
        }
        // add private key
        jsch.addIdentity(appKey.getId().toString(), appKey.getPrivateKey().trim().getBytes(), appKey.getPublicKey().getBytes(), passphrase.getBytes());
        // create session
        Session session = jsch.getSession(hostSystem.getUser(), hostSystem.getHost(), hostSystem.getPort());
        // set password if it exists
        if (password != null && !password.trim().equals("")) {
            session.setPassword(password);
        }
        session.setConfig("StrictHostKeyChecking", "no");
        session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
        session.setServerAliveInterval(SERVER_ALIVE_INTERVAL);
        session.connect(SESSION_TIMEOUT);
        Channel channel = session.openChannel("shell");
        if ("true".equals(AppConfig.getProperty("agentForwarding"))) {
            ((ChannelShell) channel).setAgentForwarding(true);
        }
        ((ChannelShell) channel).setPtyType("xterm");
        InputStream outFromChannel = channel.getInputStream();
        // new session output
        SessionOutput sessionOutput = new SessionOutput(sessionId, hostSystem);
        Runnable run = new SecureShellTask(sessionOutput, outFromChannel);
        Thread thread = new Thread(run);
        thread.start();
        OutputStream inputToChannel = channel.getOutputStream();
        PrintStream commander = new PrintStream(inputToChannel, true);
        channel.connect();
        schSession = new SchSession();
        schSession.setUserId(userId);
        schSession.setSession(session);
        schSession.setChannel(channel);
        schSession.setCommander(commander);
        schSession.setInputToChannel(inputToChannel);
        schSession.setOutFromChannel(outFromChannel);
        schSession.setHostSystem(hostSystem);
        // refresh keys for session
        addPubKey(hostSystem, session, appKey.getPublicKey());
    } catch (JSchException | IOException | GeneralSecurityException ex) {
        log.info(ex.toString(), ex);
        hostSystem.setErrorMsg(ex.getMessage());
        if (ex.getMessage().toLowerCase().contains("userauth fail")) {
            hostSystem.setStatusCd(HostSystem.PUBLIC_KEY_FAIL_STATUS);
        } else if (ex.getMessage().toLowerCase().contains("auth fail") || ex.getMessage().toLowerCase().contains("auth cancel")) {
            hostSystem.setStatusCd(HostSystem.AUTH_FAIL_STATUS);
        } else if (ex.getMessage().toLowerCase().contains("unknownhostexception")) {
            hostSystem.setErrorMsg("DNS Lookup Failed");
            hostSystem.setStatusCd(HostSystem.HOST_FAIL_STATUS);
        } else {
            hostSystem.setStatusCd(HostSystem.GENERIC_FAIL_STATUS);
        }
    }
    // add session to map
    if (hostSystem.getStatusCd().equals(HostSystem.SUCCESS_STATUS)) {
        // get the server maps for user
        UserSchSessions userSchSessions = userSessionMap.get(sessionId);
        // if no user session create a new one
        if (userSchSessions == null) {
            userSchSessions = new UserSchSessions();
        }
        Map<Integer, SchSession> schSessionMap = userSchSessions.getSchSessionMap();
        // add server information
        schSessionMap.put(instanceId, schSession);
        userSchSessions.setSchSessionMap(schSessionMap);
        // add back to map
        userSessionMap.put(sessionId, userSchSessions);
    }
    SystemStatusDB.updateSystemStatus(hostSystem, userId);
    SystemDB.updateSystem(hostSystem);
    return hostSystem;
}
Also used : JSchException(com.jcraft.jsch.JSchException) ApplicationKey(io.bastillion.manage.model.ApplicationKey) PrintStream(java.io.PrintStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) GeneralSecurityException(java.security.GeneralSecurityException) ChannelShell(com.jcraft.jsch.ChannelShell) SecureShellTask(io.bastillion.manage.task.SecureShellTask) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) SessionOutput(io.bastillion.manage.model.SessionOutput) SchSession(io.bastillion.manage.model.SchSession) UserSchSessions(io.bastillion.manage.model.UserSchSessions) SchSession(io.bastillion.manage.model.SchSession) Session(com.jcraft.jsch.Session)

Example 5 with SessionOutput

use of io.bastillion.manage.model.SessionOutput in project KeyBox by skavanagh.

the class SentOutputTask method run.

public void run() {
    Gson gson = new Gson();
    while (session.isOpen()) {
        try {
            Connection con = DBUtils.getConn();
            List<SessionOutput> outputList = SessionOutputUtil.getOutput(con, sessionId, user);
            if (!outputList.isEmpty()) {
                String json = gson.toJson(outputList);
                // send json to session
                this.session.getBasicRemote().sendText(json);
            }
            Thread.sleep(25);
            DBUtils.closeConn(con);
        } catch (SQLException | GeneralSecurityException | IOException | InterruptedException ex) {
            log.error(ex.toString(), ex);
        }
    }
}
Also used : SessionOutput(io.bastillion.manage.model.SessionOutput) SQLException(java.sql.SQLException) GeneralSecurityException(java.security.GeneralSecurityException) Connection(java.sql.Connection) Gson(com.google.gson.Gson) IOException(java.io.IOException)

Aggregations

SessionOutput (io.bastillion.manage.model.SessionOutput)5 IOException (java.io.IOException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Connection (java.sql.Connection)2 Gson (com.google.gson.Gson)1 Channel (com.jcraft.jsch.Channel)1 ChannelShell (com.jcraft.jsch.ChannelShell)1 JSch (com.jcraft.jsch.JSch)1 JSchException (com.jcraft.jsch.JSchException)1 Session (com.jcraft.jsch.Session)1 ApplicationKey (io.bastillion.manage.model.ApplicationKey)1 AuditWrapper (io.bastillion.manage.model.AuditWrapper)1 SchSession (io.bastillion.manage.model.SchSession)1 UserSchSessions (io.bastillion.manage.model.UserSchSessions)1 UserSessionsOutput (io.bastillion.manage.model.UserSessionsOutput)1 SecureShellTask (io.bastillion.manage.task.SecureShellTask)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1