DNS Calculation
Adversaries may perform calculations on addresses returned in DNS results to determine which port and IP address to use for command and control, rather than relying on a predetermined port number or the actual returned IP address. An IP and/or port number calculation can be used to bypass egress filtering on a C2 channel. The most documented implementation (attributed to APT12/Numbered Panda) multiplies the first two octets of a DNS-resolved IP address and adds the third octet to derive a dynamic C2 port number. This allows the malware to communicate on a port that changes based on the DNS response, making static firewall rules and port-based filtering ineffective.
// T1568.003 DNS Calculation — Detect C2 connections where destination port matches
// a mathematical derivation from the destination IP's octets.
// Primary formula (APT12): port = (octet1 * octet2) + octet3
// Secondary variant: port = octet1 * (octet2 + octet3)
DeviceNetworkEvents
| where Timestamp > ago(24h)
| where ActionType in ("ConnectionSuccess", "ConnectionAttempt")
| where isnotempty(RemoteIP)
| where RemoteIPType == "Public"
// Focus on non-trivial port range where calculations typically land
| where RemotePort between (4096 .. 49151)
// Exclude common legitimate high ports to reduce noise
| where RemotePort !in (8080, 8443, 8000, 8888, 9090, 9200, 9300, 9418, 27017, 27018, 28017)
// Parse IP octets from destination address
| extend IP_Parts = split(RemoteIP, ".")
| extend Oct1 = toint(IP_Parts[0])
| extend Oct2 = toint(IP_Parts[1])
| extend Oct3 = toint(IP_Parts[2])
// Apply known calculation formulas
| extend CalcPort_APT12 = (Oct1 * Oct2) + Oct3
| extend CalcPort_Variant = Oct1 * (Oct2 + Oct3)
// Score matches
| extend MatchScore = toint(RemotePort == CalcPort_APT12) + toint(RemotePort == CalcPort_Variant)
| where MatchScore > 0
| extend MatchedFormula = case(
RemotePort == CalcPort_APT12 and RemotePort == CalcPort_Variant, "Both formulas",
RemotePort == CalcPort_APT12, "APT12: (oct1*oct2)+oct3",
"Variant: oct1*(oct2+oct3)"
)
// Deprioritize noisy system processes
| where InitiatingProcessFileName !in~ ("svchost.exe", "lsass.exe", "services.exe", "wininit.exe")
| project
Timestamp,
DeviceName,
AccountName,
InitiatingProcessFileName,
InitiatingProcessFolderPath,
InitiatingProcessCommandLine,
InitiatingProcessId,
RemoteUrl,
RemoteIP,
RemotePort,
Oct1, Oct2, Oct3,
CalcPort_APT12,
CalcPort_Variant,
MatchedFormula,
MatchScore
| sort by Timestamp desc Data Sources
Required Tables
False Positives
- Applications connecting to high-numbered ports that coincidentally match an octet calculation — mathematically possible for any connection to a port in the 4096-49151 range, though probability is low for a specific formula match
- Custom enterprise middleware or internal tools that use IP-derived port schemes for service discovery or load balancing configuration
- VPN concentrators, STUN/TURN servers, or media relay infrastructure using dynamic port allocation that may produce coincidental calculation matches
- Peer-to-peer applications (Skype for Business, legacy Teams, BitTorrent clients) that negotiate high ports dynamically in a range overlapping with calculated values
- Development and testing environments where engineers have implemented custom port-derivation schemes for internal APIs or microservices
References (7)
- https://attack.mitre.org/techniques/T1568/003/
- http://www.crowdstrike.com/blog/whois-numbered-panda/
- https://www.fireeye.com/blog/threat-research/2014/09/darwins-favorite-apt-group-2.html
- https://blog.rapid7.com/2013/08/26/upcoming-g20-summit-fuels-espionage-operations/
- https://learn.microsoft.com/en-us/defender-xdr/advanced-hunting-devicenetworkevents-table
- https://learn.microsoft.com/en-us/sysinternals/downloads/sysmon
- https://github.com/redcanaryco/atomic-red-team/blob/master/atomics/T1568.003/T1568.003.md
Unlock Pro Content
Get the full detection package for T1568.003 including response playbook, investigation guide, and atomic red team tests.