From bf8a16f7ae40dc0b87461eb57d6f34270882a0bc Mon Sep 17 00:00:00 2001 From: radhikasax22-sys Date: Tue, 28 Jul 2026 01:11:10 +0530 Subject: [PATCH] fix: update shell prompt after cd command in AI shell mode When using AI shell mode, the prompt path now correctly reflects directory changes after cd command. Also handles cd .., cd -, cd ~, cd (no args), and path validation. Changes: - Intercept cd commands before sending to AI engine - Update session.cwd with proper path resolution (_resolve_path) - Validate target paths (_validate_path) against allowed directories - Handle cd -, cd .., cd ~, and bare cd (goes to /root) - Log cd activity separately for audit trail Closes #6 --- honeypot/ai_shell.py | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/honeypot/ai_shell.py b/honeypot/ai_shell.py index d6bd9c45..540dac6e 100644 --- a/honeypot/ai_shell.py +++ b/honeypot/ai_shell.py @@ -59,6 +59,33 @@ def _interact(self): self.channel.send('\033[2J\033[H') self._send_prompt() continue + if cmd_lower == 'cd' or cmd_lower.startswith('cd '): + session = self.engine.get_or_create_session(self.session_id) + parts = line.strip().split(maxsplit=1) + if len(parts) == 1 or parts[1] in ('~', ''): + target = '/root' + elif parts[1] == '-': + target = getattr(session, '_prev_cwd', '/root') + else: + target = parts[1] + session._prev_cwd = session.cwd + if target.startswith('/'): + new_cwd = target + else: + new_cwd = (session.cwd.rstrip('/') + '/' + target) + new_cwd = AIShellHandler._resolve_path(new_cwd) + if not AIShellHandler._validate_path(new_cwd): + self.channel.send(f'bash: cd: {target}: No such directory\r\n') + else: + session.cwd = new_cwd + self._send_prompt() + HONEYPOT_LOGGER.log_session_activity( + self.client_ip, + f"AI cd: {line[:200]}", + username=self.username + ) + continue + mitre_techniques = MITRE.analyze_command(line) mitre_str = MITRE.format_techniques(mitre_techniques) db.insert_command( @@ -100,6 +127,25 @@ def _send_prompt(self): def _cleanup(self): self.engine.cleanup_session(self.session_id) + @staticmethod + def _resolve_path(path: str) -> str: + parts = path.split('/') + resolved = [] + for p in parts: + if p in ('', '.'): + continue + if p == '..': + if resolved: + resolved.pop() + else: + resolved.append(p) + return '/' + '/'.join(resolved) if resolved else '/' + + @staticmethod + def _validate_path(path: str) -> bool: + allowed = ('/', '/root', '/home', '/tmp', '/var', '/etc', '/usr', '/bin', '/opt', '/mnt') + return any(path == a or path.startswith(a + '/') for a in allowed) + class AuthAcceptHandler: