Sandbox Evasion Using NTP

Published: 2020-09-03
Last Updated: 2020-09-03 08:54:18 UTC
by Xavier Mertens (Version: 1)
2 comment(s)

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[1]. 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")), ("",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")), ("",123))
>>> msg, address = client.recvfrom( 1024 )
>>> msg
b'\x1c\x02\x03\xe8\x00\x00\x03%\x00\x00\x0b`\xcel\x00\x83\xe2\xfb! \xf9]\xab\x0c\x01\x01\x01\x01\x01\x01\x01\x01\xe2\xfb(\x99\xaf\xf7(\xa3\xe2\xfb(\x99\xaf\xf9\x18\x82'

Then, the current time is extracted and converted by unpacking the relevant bytes:

>>> import struct
>>> import datetime
>>> datetime.datetime.fromtimestamp(struct.unpack("!12I",msg)[10] - 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)[10] - 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[3].


Xavier Mertens (@xme)
Senior ISC Handler - Freelance Cyber Security Consultant

2 comment(s)


So it makes the NTP request, then it makes it again and compares the two. Then it sleeps for 5 seconds and performs malicious activity? What exactly is the malicious activity? Could you simplify for us management folk please.
Not to be nit-picky, but the title of this post makes it sound like the malware is somehow doing evasion via the NTP protocol, which it is not. Perhaps a better title might be "Sandbox Evasion Using Accurate External Time Measurement"? :-)

Diary Archives