I’m still hunting for interesting (read: “malicious”) Python samples. By reading my previous diaries, you know that I like to find how attackers implement obfuscation and evasion techniques. Like yesterday, I found a Python sample that creates a thread to run a malicious shellcode. But before processing the shellcode, it performs suspicious network traffic:
client = socket.socket(AF_INET, SOCK_DGRAM) client.sendto((bytes.fromhex("1b") + 47 * bytes.fromhex("01")), ("us.pool.ntp.org",123))
Contacting an NTP server is common and does not look malicious at all. The code above will query the NTP server and extract the current time:
$ python3 Python 3.5.2 (default, Jul 17 2020, 14:04:10) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from socket import AF_INET, SOCK_DGRAM >>> import socket >>> client = socket.socket(AF_INET, SOCK_DGRAM) >>> client.sendto((bytes.fromhex("1b") + 47 * bytes.fromhex("01")), ("us.pool.ntp.org",123)) 48 >>> msg, address = client.recvfrom( 1024 ) >>> msg b'x1cx02x03xe8x00x00x03%x00x00x0b`xcelx00x83xe2xfb! xf9]xabx0cx01x01x01x01x01x01x01x01xe2xfb(x99xafxf7(xa3xe2xfb(x99xafxf9x18x82'
Then, the current time is extracted and converted by unpacking the relevant bytes:
>>> import struct >>> import datetime >>> datetime.datetime.fromtimestamp(struct.unpack("!12I",msg) - 2208988800) datetime.datetime(2020, 9, 3, 10, 32, 25)
The number 2208988800 corresponds to 00:00 1 Jan 1970 GMT.
Then, the script performs a “sleep(5)” instruction and, again, contacts the NTP server. Then it compares the two received timestamps:
if ((datetime.datetime.fromtimestamp((struct.unpack("!12I",msg) - 2208988800)) - uWMTJmeXanwCx).seconds >= 5):
If the sleep() timeout is respected, the malicious activity will be performed otherwise, it will silently exit. Why? Because many sandboxes increase their clock to speed up the analyzis of malware that, at the opposite, implement a long pause, expecting to reach the sandbox timeout. This is a cool technique!
Another test implemented in the script is a check of the amount of CPU available:
import multiprocessing YiSUTyJ = multiprocessing.cpu_count() if YiSUTyJ >= 1:
The test isn’t very relevant in this case because it will always succeed. All systems have at least 1 CPU 😉
I suspect the script to be a beta version or a red-team exercise because the IP address used to grab the shellcode, is a RFC-1918 IP.
The script has a VT score of 7/58 and I uploaded it on MalwareBazaar.
Xavier Mertens (@xme)
Senior ISC Handler – Freelance Cyber Security Consultant
(c) SANS Internet Storm Center. https://isc.sans.edu Creative Commons Attribution-Noncommercial 3.0 United States License.