Security Experts:

PyPI Served Malicious Version of Popular 'Ctx' Python Package

A popular Python package was compromised recently and replaced with a malicious version apparently designed to help the attacker obtain AWS credentials.

The incident was investigated by Sonatype, SANS Institute and an independent researcher. There are actually two libraries that appear to have been targeted in the attack, but only one of them may have had a more significant impact.

The Python package named Ctx, which has, on average, 22,000 downloads per week, was compromised on the Python Package Index (PyPI) on May 14. The last Ctx update prior to this attack was uploaded to PyPI in December 2014, but new versions were published on and after May 14.

An investigation revealed that the original maintainer’s domain name expired and the attacker registered the domain on May 14. With access to the domain, they could have created an email address to which the password reset link would be sent.

The Ctx versions uploaded by the attacker — 0.1.2 (this was also the last version of the original), 0.2.2 and 0.2.6 — included functionality to steal data and upload it to a remote location controlled by the attacker.

Targeted data in one version included AWS access key ID, computer name, and AWS secret access key when a dictionary is created. Another malicious version of Ctx targeted all environment variables.

The second compromised library was the PHPass portable PHP password hashing framework. The original PHPass was deleted in September 2021, along with its original developer’s account. It appears that the developer’s username became available and it was claimed by the attacker, giving them access to the project’s GitHub account.

Both impacted libraries have been taken down. While the malicious Ctx version may have impacted many users, PHPass appears to have had only a handful of installations in recent weeks.

Sonatype reported seeing evidence that suggests the two incidents are related.

Developers who downloaded one of the two packages in recent days should ensure that they are not using one of the malicious versions. SANS Institute provides indicators of compromise (IoC).

Last week, Sonatype reported seeing PyPI serving a malicious Python package that appeared to mimic the popular PyKafka Apache Kafka client. The malicious package, named “pymafka”, was downloaded roughly 300 times. It delivered Cobalt Strike to Windows, macOS and Linux devices. 

UPDATE: Researcher Yunus Aydin, aka sockpuppets, said he was behind this attack. He claimed in a blog post published on May 25 that his activities were not malicious and that he only wanted to show the full potential impact.

Related: Checkmarx Finds Threat Actor 'Fully Automating' NPM Supply Chain Attacks

Related: Google Finds 35,863 Java Packages Using Defective Log4j

Related: 1,300 Malicious Packages Found in Popular npm JavaScript Package Manager

Related: New OpenSSF Project Hunts for Malicious Packages in Open Source Repositories

view counter
Eduard Kovacs (@EduardKovacs) is a contributing editor at SecurityWeek. He worked as a high school IT teacher for two years before starting a career in journalism as Softpedia’s security news reporter. Eduard holds a bachelor’s degree in industrial informatics and a master’s degree in computer techniques applied in electrical engineering.