[WordPress Security] Malware Scanning: An Essential Layer of Website Security 

From: Wordfence <list_at_wordfence.com>
Date: Mon, 2 Oct 2023 09:09:11 -0700

Detecting an infection so you can respond quickly is just as important as protecting against attacks

Wordfence-Logo.png (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2Cg3l5QzW5BW0B06lZ3pHN67HYyCHzFJGW8Fb2Wc598szXW4kqSS_5_HfK5VM_byK5q1bS0W90FGcJ6VYXjYW52kS_g9736ZYW1JkZPB310J-8W3rm5qY6tWtGhW2DJF804dCPB0V2bCGt4ynsW8W8nfB491WvSC4VLcwNw6wh8w1W41MX3j8zlWBWW4TSPSf7kds-3W3S2tzB4132KvW6pfx4q3Dtmn3W5p9R_H6cqGBmW54HTpP3s79lRd65Rg404 )

Know your malware featured image (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2F23l5QzW95jsWP6lZ3q4W95Yxjw85xTcgW64Cvzd3wDPRgN5Zmxf57QRsfW8rS7v-8tS8J3W82kg3s4fFr1nW3JKjmh2-YpRSW4ZqwHg3thgjdW3fhszJ6HJsXBW4vPtYr6NLpxnW5Dcfgk3HBRncW5rnyXF6vqpjYW4YXtcW1bCrHnW1vRXKs25X9JRW8P_JGm7Jcs96VvZFxW5tq9YYW3_X7B15BvPFQW1bL2nc3m9GSLW3W0Gt08L6w6XN4vGSbyHf1Y4W3ZWpfl2v3fZLW4vc5N-3_dMGRW3KNHnV1VZ5hfW6MBXGT677m9lW7FjMcZ2M7RtjVDQPbT98MD88W7kp2893Mv9wRW58b0LJ8gTnw0W15lt7V4DNphhW7dcJvH44lPBRW312Zmf85LXdlf314WNb04 )

With the launch of Wordfence CLI (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2CT3l5QzW6N1vHY6lZ3lrW3GHd3M286qYTW2Q7sPC1SQsZGW6J1Px597gGX9W6ZGjls5rWfp7N210Zz_Gl5rVVQXT3w85SPGPW8Nt02C3zm2VWW6ll7nT4DW410W7mB6423bbMWVW2q5S5F6KYKsCW5ZR_pq3gTk3-W54_HYp155Bw_W2335t51gZltfVHqgkq33n2wzW1dzlXp3VThnmW43Z-dz4Bgv0WVfpsN42pcgYPW1ljb8g31RhGKW8RL7vC5T7V1jW7Bz27h6nBq42N1_ym9Nn0tVQW5n7R_57dV_Jqf8YvGNz04 ) , our high performance security scanner that can detect the vast majority of PHP malware targeting WordPress, Wordfence continues to emphasize the importance of malware detection and remediation. Malware targeting WordPress uses a variety of obfuscation techniques to avoid detection, and today’s post dives into some of the most common built-in PHP functionality malware often makes use of in order to do this.

What is Obfuscation?

Obfuscation is the process of concealing the purpose or functionality of code or data so that it evades detection and is more difficult for a human or security software to analyze, but still fulfills its intended purpose.

Obfuscation makes use of various types of encoding techniques, but is not exactly the same thing as encoding. There are countless legitimate uses for encoding data, including saving space through compression, transmitting data over a network, and packaging code so that it can be easily interpreted by programs in an expected format. Meanwhile obfuscation is intentionally designed to prevent understanding and detection by humans and security software.

Obfuscation is also different from encryption in that it can typically be reversed without a “key”, though there are some encoding techniques, such as XOR encoding, which do use keys and are used in both encryption and obfuscation.

READ THIS POST ON THE BLOG
(https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2F23l5QzW95jsWP6lZ3q4W95Yxjw85xTcgW64Cvzd3wDPRgN5Zmxf57QRsfW8rS7v-8tS8J3W82kg3s4fFr1nW3JKjmh2-YpRSW4ZqwHg3thgjdW3fhszJ6HJsXBW4vPtYr6NLpxnW5Dcfgk3HBRncW5rnyXF6vqpjYW4YXtcW1bCrHnW1vRXKs25X9JRW8P_JGm7Jcs96VvZFxW5tq9YYW3_X7B15BvPFQW1bL2nc3m9GSLW3W0Gt08L6w6XN4vGSbyHf1Y4W3ZWpfl2v3fZLW4vc5N-3_dMGRW3KNHnV1VZ5hfW6MBXGT677m9lW7FjMcZ2M7RtjVDQPbT98MD88W7kp2893Mv9wRW58b0LJ8gTnw0W15lt7V4DNphhW7dcJvH44lPBRW312Zmf85LXdlf314WNb04 )

Encoding Techniques

Since obfuscation often relies heavily on encoding techniques, It’s important to understand what these techniques look like, their typical legitimate use cases, and signs that they’re being used to hide something potentially malicious. In today’s article, we will cover some of the most commonly used encoding techniques, and teach you how to spot legitimate uses as well as potentially suspicious patterns.

Base64 Encoding

What is Base64 encoding?

Base64 encoding is widely used to send and store data. If you’ve ever played with Linux and tried to look at an executable file using the cat command, you might have noticed that your terminal starts acting very strangely. This is because binary data includes an enormous number of potential byte sequences, and software that’s not designed to interpret a particular file format can incorrectly interpret some of these sequences as commands.

Base64 encoding allows any data, including binary data, to be stored and transmitted as text which makes it very convenient for programs to talk to one another without being misunderstood, especially over a network.

It uses 26 lower-case letters, 26 upper-case letters, the digits 0-9, and the ‘+’ and ‘/’ symbols for a total of 64 characters, plus ‘=’ for padding.

Note that, unlike the Base 8(Octal) and Base 16(Hexadecimal) encodings we’ll cover later, base64 is not a direct representation of the underlying bytes. Instead, it converts their octal representations to Base 10(Decimal) and then uses a lookup table to assign a character value.

How is Base64 Encoding Used Legitimately?

You’ve likely seen base64 encoded data in the past, and it’s very easy to spot – for instance, SGVsbG8sIFdvcmxkIQ== decodes to “Hello, World!” and you can run the code snippet:

[View this code snippet on the blog.] (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2F23l5QzW95jsWP6lZ3q4W95Yxjw85xTcgW64Cvzd3wDPRgN5Zmxf57QRsfW8rS7v-8tS8J3W82kg3s4fFr1nW3JKjmh2-YpRSW4ZqwHg3thgjdW3fhszJ6HJsXBW4vPtYr6NLpxnW5Dcfgk3HBRncW5rnyXF6vqpjYW4YXtcW1bCrHnW1vRXKs25X9JRW8P_JGm7Jcs96VvZFxW5tq9YYW3_X7B15BvPFQW1bL2nc3m9GSLW3W0Gt08L6w6XN4vGSbyHf1Y4W3ZWpfl2v3fZLW4vc5N-3_dMGRW3KNHnV1VZ5hfW6MBXGT677m9lW7FjMcZ2M7RtjVDQPbT98MD88W7kp2893Mv9wRW58b0LJ8gTnw0W15lt7V4DNphhW7dcJvH44lPBRW312Zmf85LXdlf314WNb04 )

to see this in action.

PHP uses the base64_encode and base64_decode functions to encode and decode Base64-encoded data. Many applications store information in this format as data files or database entries, so the presence of the base64_encode and base64_decode functions in a PHP file are often no cause for concern on their own.

How is Base64 Encoding Used by Malware?

It is significantly less common for base64-encoded data to be hardcoded into a PHP file, especially one that executes it as code.

For example, the following code snippet

[View this code snippet on the blog.] (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2F23l5QzW95jsWP6lZ3q4W95Yxjw85xTcgW64Cvzd3wDPRgN5Zmxf57QRsfW8rS7v-8tS8J3W82kg3s4fFr1nW3JKjmh2-YpRSW4ZqwHg3thgjdW3fhszJ6HJsXBW4vPtYr6NLpxnW5Dcfgk3HBRncW5rnyXF6vqpjYW4YXtcW1bCrHnW1vRXKs25X9JRW8P_JGm7Jcs96VvZFxW5tq9YYW3_X7B15BvPFQW1bL2nc3m9GSLW3W0Gt08L6w6XN4vGSbyHf1Y4W3ZWpfl2v3fZLW4vc5N-3_dMGRW3KNHnV1VZ5hfW6MBXGT677m9lW7FjMcZ2M7RtjVDQPbT98MD88W7kp2893Mv9wRW58b0LJ8gTnw0W15lt7V4DNphhW7dcJvH44lPBRW312Zmf85LXdlf314WNb04 )

is a minimalist webshell. The eval function tells PHP to execute whatever is decoded by the base64_decode function as PHP, so once the string of data c3lzdGVtKCRfR0VUWydjbWQnXSk7 is decoded it will execute system($_GET['cmd']);.

This uses the system function to run the contents of the cmd query string parameter as a terminal command. This means that if this webshell was installed on a site as webshell.php, an attacker could go to https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2CT4YGXlW6N1X8z6lZ3nmW14j0FZ3zh2yCW1rMDSY2ZVQXgW60KS2r6X1n4PW6Vn82Q4008D5W5sB2Zj3d3vCWW7fjrd17074GzN9cxMdxyP47pW5QH1ZG3BbzxfW8mnX579gSbqQN42H0hrb5xkvW98ss0w1_vqFyW3-gFRJ2hJZSMVyV_1t8W6mD3W8q43-m4N77vqW6vxvRg6nyP2kVys5C08CmWH8W2C1l686QXYxVW7G2phJ5tMvsdV3nD5J72GP8wW79W_5B3T_SYgW5NcScx6zLYk_N90D_8VSZHt7W7kWlbW26NSTjW84sBqy4QYgHtVJLtj52clhlSW30RsQP5QPfRGW7_qNxG53mNFGW7Zf3_F47smd3W8wHyDW36j_2BW75dD6F3fXwkMW2qNkW97G6Sr7W6XwN4v3LdgmMN416jVJkxnBYW3m4GkH6ys4WYVC7GxV9fRmCzW22CJTX5vtPFtW2JNsZj2XgC3rW5x4Yvt8flvn4f2fn7l804 to run the ls command and list all files in the directory.

Byte Escape Sequences

What are Byte Escape Sequences?

You might already be familiar with some escape sequences, such as \n to denote a new line of text, or \t to denote a tab, but they can also be used to represent binary data.

PHP uses byte escape sequences for this, and they are similar to base64 encoding in that they are a way to represent both text and binary data as text strings.

There are two commonly used byte escape sequence formats used in PHP – Hexadecimal, which uses Base 16, and Octal, which uses Base 8.

Hex encoded byte sequences are represented by \x followed by two characters, which can be any digit from 0 through 9 and the letters ‘a’ through ‘f’.

For example, the text “Hello, World!” can be represented as the following escaped sequence:

\x48\x65\x6c\x6c\x6f\x2c\x20\x57\x6f\x72\x6c\x64\x21.

Octal byte sequences are represented by ‘\’ followed by a one to three digit number from 0 through 377.

For example, the text “Hello, World!” can be represented as the following escaped sequence:

\110\145\154\154\157\54\40\127\157\162\154\144\41.

If you’ve ever worked with Linux filesystem permissions, they are also stored in octal format, for example ‘777’ which denotes that all users have permission to read, write, and execute.

PHP also uses unicode escape sequences, which begin with \u and can be used to encode unicode characters used for international languages as well as as emojis. While unicode escape sequences can be used to bypass security systems, they are less commonly used in malware, and are beyond the scope of this article.

How are Byte Escape Sequences Used Legitimately?

Byte escape sequences are used to store binary information, and many PHP applications use them to store encryption keys and to perform operations that can be sped up by handling binary data directly. As such they are most often found in code libraries for handling encryption and text manipulation and conversion.

How are Byte Escape Sequences Used by Malware?

PHP has an unusual property – any byte escape sequence surrounded by double quotes(“”) is automatically parsed. Moreover, PHP can interpret any valid combination of text, hex escape sequences, octal escape sequences, and unicode escape sequences in a single string. In other words, “He\x6c\x6c\x6f\54\40\127\157rld!” will be processed by PHP as “Hello, World!”. You can actually test this using the following code snippet:

[View this code snippet on the blog.] (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2F23l5QzW95jsWP6lZ3q4W95Yxjw85xTcgW64Cvzd3wDPRgN5Zmxf57QRsfW8rS7v-8tS8J3W82kg3s4fFr1nW3JKjmh2-YpRSW4ZqwHg3thgjdW3fhszJ6HJsXBW4vPtYr6NLpxnW5Dcfgk3HBRncW5rnyXF6vqpjYW4YXtcW1bCrHnW1vRXKs25X9JRW8P_JGm7Jcs96VvZFxW5tq9YYW3_X7B15BvPFQW1bL2nc3m9GSLW3W0Gt08L6w6XN4vGSbyHf1Y4W3ZWpfl2v3fZLW4vc5N-3_dMGRW3KNHnV1VZ5hfW6MBXGT677m9lW7FjMcZ2M7RtjVDQPbT98MD88W7kp2893Mv9wRW58b0LJ8gTnw0W15lt7V4DNphhW7dcJvH44lPBRW312Zmf85LXdlf314WNb04 )

The fact that PHP can easily interpret such sequences but humans usually cannot read them make byte escape sequences ideal for obfuscation. It is very unusual for legitimate software to use mixed encodings in this manner, and so it is a very strong indicator of malicious activity.

Character Encoding

What is Character Encoding?

Character encoding is similar to hex encoding but more limited in that it can only be used to represent text and a very limited subset of control characters. PHP uses the chr function to decode a number between 0 and 255 into a single character, and the ord function to encode a single character back into a numeric value. This is slightly complicated by the fact that the chr function accepts decimal, hexadecimal, and octal formatted numbers, but decimal format is most commonly used.

The following code provides an example of character encoding utilizing chr, and would output “Hello, World!” when executed:

[View this code snippet on the blog.] (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2F23l5QzW95jsWP6lZ3q4W95Yxjw85xTcgW64Cvzd3wDPRgN5Zmxf57QRsfW8rS7v-8tS8J3W82kg3s4fFr1nW3JKjmh2-YpRSW4ZqwHg3thgjdW3fhszJ6HJsXBW4vPtYr6NLpxnW5Dcfgk3HBRncW5rnyXF6vqpjYW4YXtcW1bCrHnW1vRXKs25X9JRW8P_JGm7Jcs96VvZFxW5tq9YYW3_X7B15BvPFQW1bL2nc3m9GSLW3W0Gt08L6w6XN4vGSbyHf1Y4W3ZWpfl2v3fZLW4vc5N-3_dMGRW3KNHnV1VZ5hfW6MBXGT677m9lW7FjMcZ2M7RtjVDQPbT98MD88W7kp2893Mv9wRW58b0LJ8gTnw0W15lt7V4DNphhW7dcJvH44lPBRW312Zmf85LXdlf314WNb04 )

Legitimate use of character encoding is somewhat rare in PHP, though it is occasionally used for text manipulation and inserting control characters such as null bytes in place of hex or octal encoding. It is far more commonly used in languages such as JavaScript where the code is often publicly visible.

One common use case is “reverse obfuscation” where character encoding in JavaScript is used to render an email address on a site in a way that a human can read it once the code is executed, but that older automated tools that can only view the uninterpreted code have difficulty scraping.

How is Character Encoding Used by Malware?

Character encoding is used by malware in almost exactly the same way as byte escape sequences, that is, to make the code more difficult for a human to read and security tools to interpret. It is frequently used by malware to hide malicious URLs that the malware then sends sensitive information or redirects visitors to.

Substitution Ciphers(rot13, etc.)

What are Substitution Ciphers?

One of the simplest ways to obfuscate content is to simply substitute letters for other letters. This method is known as a Caesar cipher, and most programming languages have a built-in method to do this, the most popular of which is simply to replace each letter with the one halfway across the alphabet from it, or 13 steps away. As such, “Hello, World!” becomes “Uryyb, Jbeyq!” The rot13 substitutions can be seen in the following table:

A => N

B => O

C => P

D => Q

E => R

F => S

G => T

H => U

I => V

J => W

K => X

L => Y

M => Z

N => A

O => B

P => C

Q => D

R => E

S => F

T => G

U => H

V => I

W => J

X => K

Y => L

Z => M

How are Substitution Ciphers Used Legitimately?

It is uncommon for substitution ciphers to be used in well-architected code, but some legitimate software does use it as a workaround when it has issues running in an environment where naive or poorly configured security software might hinder its intended execution, or when a value needs to be stored that won’t be interfered with by code that is looking for that value. In other words, it is almost always used to evade detection of some kind even by legitimate software.

How are Substitution Ciphers Used by Malware?

Malware frequently uses the str_rot13 function to obfuscate malicious URLs that it sends sensitive data, redirects visitors to, or receives commands from, that might be on a blocklist. It is a relatively strong signal of suspicious behavior, though it is not strong enough on its own to mark a file as malicious.

Compression (gzencoding, zlib encoding, and more)

What is Compression?

Compression refers to the process of compacting data, making it take up less space for storage and less bandwidth for transport. Compression algorithms are fairly complex, though many of them work in part by finding repeated patterns and storing references to them rather than the entire data.

As a very basic example, the text “aaaaabbbccca” could potentially be compressed to “a5b3c3a”. Real compression algorithms are significantly more sophisticated, and there are many other steps involved depending on the type of data being compressed.

There are a number of commonly used compression algorithms, including ones specifically designed to compress images, movies, and audio files. Media compression algorithms are often “lossy” and do not perfectly reconstruct the original data so much as produce output that looks or sounds similar enough to a human that it’s hard to notice.

In today’s article we are going to focus specifically on the functions most commonly used by PHP to compress and decompress arbitrary data, which use “lossless” compression and can perfectly reconstruct the original data from the archived format.

How is Compression used legitimately?

Most people are familiar with zip files, and many websites use compression to load large amounts of content more quickly while saving money on outbound data transfer. The most common compression algorithms used in PHP are Zlib and Gzip, both of which are handled by the Zlib module, though BZip2 is also fairly common.

Note that Gzip is not exactly the same thing as the zip files you may be familiar with as it can only compress single files, while modern zip archives can be configured to use many different algorithms including the one used by Gzip. There is a workaround to the single file problem, however – if you’ve ever seen a file with a .tar.gz extension, It is very common to combine multiple files into a “tarball” and then compress the combined file using gzip.

Gzip uses an algorithm called “DEFLATE” which tends to be very fast and is often used by web servers to compress outbound data over the network. This process is effectively transparent – if configured correctly, a web server will send out a compressed page and your browser will automatically and transparently decompress and load it. Zlib and Bzip2 are slower but attain higher compression ratios so they’re often used to store archive files.

How is Compression Used by Malware?

Compressed files have a unique advantage for malicious actors – it is difficult to spot particular data in them, especially at high compression ratios. However, they also can’t easily be executed directly in the context of PHP. Compression isn’t limited to just files – any data, including text strings can be compressed. This means that an attacker can use compression to hide their code in a file and uncompress and execute it at runtime using, for instance, the gzinflate and gzuncompress functions.

There is one hurdle, however, which is that compressed files contain binary data, that is, data that can’t be directly represented as a text string. One solution to this is to load the compressed data from a separate, appropriately formatted file. Since attackers can often only upload a single file to take control of a site, this can be impractical.

While it is possible to mix string and raw binary data in a single file, reading these separately often requires knowing exactly where in the file everything is, which may be difficult if the file was uploaded or written by exploiting a vulnerability.

Earlier in the article, we discussed ways to safely store binary data in a text string, such as base64 encoding and byte escape sequences. These become significantly more useful to attackers when combined with compression algorithms, and we’ll examine this use case shortly.

XOR Encoding

What is XOR Encoding?

XOR (eXclusive OR) is a simple way to mix two sets of data together at the binary level, meaning it operates on the 1s and 0s that make up data. Think of it as a lightweight disguise for data. It takes two bits (a 1 or a 0) and compares them. If the bits are the same, it outputs 0; if they’re different, it outputs 1.

Here’s an example:

0 XOR 0 = 0

0 XOR 1 = 1

1 XOR 0 = 1

1 XOR 1 = 0

In PHP, you would use the ^ symbol to do an XOR operation between two characters. What actually happens is that the computer looks at the binary form of these characters and does the XOR bit by bit.

For example, the letter ‘A’ in binary is 01000001, and ‘B’ is 01000010. When you XOR them:

01000001

01000010

——–

00000011

You get a jumbled mix of the two. What makes XOR particularly useful is that if you take this result and do the exact same XOR operation on it again with ‘B’, you’ll get back ‘A’.

How is XOR Encoding Used Legitimately?

In practical terms, XOR is used for basic encryption or data masking. It’s fast and doesn’t require a lot of computing power. For example, if you have a secret key that both the sender and receiver know, you could XOR your message with this key to obscure the text before sending it over the internet. The downside to this is that it is usually trivial to find the “key” using statistical analysis, so while XOR encoding is used as part of a much more complex process by many strong encryption schemes, it is not secure encryption on its own.

How is XOR Encoding Used by Malware?

XOR encoding is particularly useful for attackers who want to restrict access to malware, such as webshells, used to control a website. For instance, by making the XOR “key” a value that isn’t present in the malware itself but is passed in by an input parameter, it acts as a password protection mechanism that makes the malware unable to run unless an attacker who knows the key sends a specially crafted request. Likewise, needing the key to deobfuscate the malware makes it much more difficult for security analysts and scanners to identify malicious behavior.

The following malicious file actually includes the “key” in the malware itself, but requires commands to be encoded with that key before they can be processed. It accepts various $_COOKIE values and XORs them against the value of $odqwv, then executes the decoded commands.

[View this code snippet on the blog.] (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2F23l5QzW95jsWP6lZ3q4W95Yxjw85xTcgW64Cvzd3wDPRgN5Zmxf57QRsfW8rS7v-8tS8J3W82kg3s4fFr1nW3JKjmh2-YpRSW4ZqwHg3thgjdW3fhszJ6HJsXBW4vPtYr6NLpxnW5Dcfgk3HBRncW5rnyXF6vqpjYW4YXtcW1bCrHnW1vRXKs25X9JRW8P_JGm7Jcs96VvZFxW5tq9YYW3_X7B15BvPFQW1bL2nc3m9GSLW3W0Gt08L6w6XN4vGSbyHf1Y4W3ZWpfl2v3fZLW4vc5N-3_dMGRW3KNHnV1VZ5hfW6MBXGT677m9lW7FjMcZ2M7RtjVDQPbT98MD88W7kp2893Mv9wRW58b0LJ8gTnw0W15lt7V4DNphhW7dcJvH44lPBRW312Zmf85LXdlf314WNb04 )

This means that any attacker that knows the value of $odqwv can thus send commands to the file that have already been XORd against that value, which will then be reversed and executed.

In this example, $odqwv is the XORd value of \x16\x13\x1b\x13_at_V*\x1e\x0\x2\xb\x16\xc and trhvvbuzeadricgobq which turns out to be “base64_decode.” You can find this value by creating a simple one liner:

[View this code snippet on the blog.] (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2F23l5QzW95jsWP6lZ3q4W95Yxjw85xTcgW64Cvzd3wDPRgN5Zmxf57QRsfW8rS7v-8tS8J3W82kg3s4fFr1nW3JKjmh2-YpRSW4ZqwHg3thgjdW3fhszJ6HJsXBW4vPtYr6NLpxnW5Dcfgk3HBRncW5rnyXF6vqpjYW4YXtcW1bCrHnW1vRXKs25X9JRW8P_JGm7Jcs96VvZFxW5tq9YYW3_X7B15BvPFQW1bL2nc3m9GSLW3W0Gt08L6w6XN4vGSbyHf1Y4W3ZWpfl2v3fZLW4vc5N-3_dMGRW3KNHnV1VZ5hfW6MBXGT677m9lW7FjMcZ2M7RtjVDQPbT98MD88W7kp2893Mv9wRW58b0LJ8gTnw0W15lt7V4DNphhW7dcJvH44lPBRW312Zmf85LXdlf314WNb04 )

which prints the value. In this case $odqwv is the literal string “base64_decode” but this is simply used as a key and does not refer to the built-in function itself.

The value in $_COOKIE[“dj”] is then XORd against the $odqwv key, which is ‘base64_decode’, and the result is called as a function, with similar steps occurring throughout the rest of the code.

Putting it All Together

Most obfuscated malware uses a combination of these techniques to hide its functionality, and combined techniques are one of the clearest indications of malicious activity. For example, take the following code:

[View this code snippet on the blog.] (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2F23l5QzW95jsWP6lZ3q4W95Yxjw85xTcgW64Cvzd3wDPRgN5Zmxf57QRsfW8rS7v-8tS8J3W82kg3s4fFr1nW3JKjmh2-YpRSW4ZqwHg3thgjdW3fhszJ6HJsXBW4vPtYr6NLpxnW5Dcfgk3HBRncW5rnyXF6vqpjYW4YXtcW1bCrHnW1vRXKs25X9JRW8P_JGm7Jcs96VvZFxW5tq9YYW3_X7B15BvPFQW1bL2nc3m9GSLW3W0Gt08L6w6XN4vGSbyHf1Y4W3ZWpfl2v3fZLW4vc5N-3_dMGRW3KNHnV1VZ5hfW6MBXGT677m9lW7FjMcZ2M7RtjVDQPbT98MD88W7kp2893Mv9wRW58b0LJ8gTnw0W15lt7V4DNphhW7dcJvH44lPBRW312Zmf85LXdlf314WNb04 )

If supplied with the correct $xor_key, it will output “Hello, World!”.

Let’s take a look at how we did this:

First, we took the code ‘echo “Hello, World!”;’ and XOR-encoded it with a key value of ‘K’, resulting in the output .(#$ki.''$gk$9'/jip.

We then ran it through the gzdeflate function, which results in a binary output that can’t be rendered here, but after base64-encoding that output it turns into 09NQVsnOZNZTV1dJz5ZRsVTXz8osAAA=.

If you placed the code in a hello.php file on your site and accessed it, you’d get a blank screen unless you sent a request to /hello.php?k=K, which would output “Hello, World!”.

While this example only outputs “Hello, World!” when it is passed the right key, it is trivial to disguise any PHP code in this manner, including destructive code that adds malicious administrators, creates additional malicious files, or alters system settings.

Conclusion

In today’s article, we discussed the most commonly used encoding techniques in PHP, their legitimate applications, and how malicious code uses them to obfuscate its purpose and intent. While obfuscation is an arms race, the Wordfence scanner and Wordfence CLI (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2CT3l5QzW6N1vHY6lZ3lrW3GHd3M286qYTW2Q7sPC1SQsZGW6J1Px597gGX9W6ZGjls5rWfp7N210Zz_Gl5rVVQXT3w85SPGPW8Nt02C3zm2VWW6ll7nT4DW410W7mB6423bbMWVW2q5S5F6KYKsCW5ZR_pq3gTk3-W54_HYp155Bw_W2335t51gZltfVHqgkq33n2wzW1dzlXp3VThnmW43Z-dz4Bgv0WVfpsN42pcgYPW1ljb8g31RhGKW8RL7vC5T7V1jW7Bz27h6nBq42N1_ym9Nn0tVQW5n7R_57dV_Jqf8YvGNz04 ) both use our incredibly effective malware detection signatures and are able to detect the vast majority of obfuscated malware targeting WordPress. A large part of why this is possible is due to our expertise and deep understanding of these encoding techniques and which combinations of encoding tend to indicate malicious behavior. Our experienced security analysts are continuously writing new signatures to improve our detection capabilities.

In a future article, we’ll cover more advanced obfuscation techniques that rely on other properties and quirks of PHP, but it’s necessary to understand basic encoding methods first because of how frequently they’re used, even when they’re not the primary method of obfuscation.

We encourage readers who want to learn more about this to experiment with the various code snippets we have presented. More advanced readers may wish to review public malware repositories in order to better learn to spot these indicators, but be sure to be careful with any actual malware samples you find and only execute them in a virtual environment, as even PHP malware can be used for local privilege escalation on vulnerable machines.

For security researchers looking to disclose vulnerabilities responsibly and obtain a CVE ID, you can submit your findings to Wordfence Intelligence (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2Cg3cscvW5BW0B06lZ3nBW1625Dp4wPc20N2rTXrF8DQZzW2PPDZ35ftvRkW79lGyc8Yw7jcW7dY9775N4vLZW8F_gTY3XDjmmVTzKrZ942BDDN2sspVsNL0tvW5fhK557rJ-mhV_r7-m846bNrW7Yrs_h3VXV3yW8stySY3k25JCW7NCX5G2CHBLNW1kk1_M3L8__qW6-46nR598vNmW3-4j5G4Cd4MWV2Hxg93xgqfjW620sw58r8GSNf4j_DSK04 ) and potentially earn a spot on our leaderboard (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2CT3cscvW6N1vHY6lZ3p9N4yR9MmZQth_W1vNMQM6gGvpMW7w-P0s6xXn8jW8Vx9f54ftZljW5C9M363xwY1xW3z0k3G3N9QNMW8NmN4b49wZ_8W7-xcxD17w9r7W4FgL1D4V_P3zW1vYh-n1LdmxgW1PgLTt6jXdW6W389gVB86GK9TW7dyW1l5ZfQqSV-mmYv7z75mdW2Ksxq-24KcR4VWybmB3lM6hnW1ZdY_d5pfx9jW63Cr7w5-H2FqW3ggtkZ4SvzSlW2ZXzgg4-WxVgW7V8cd06MNfBsW3YV-rb8Y_9Dmf4kLds804 ) .

The Full Product Lineup:

wf-stacked-free-1 (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2Cz3l5QzW69sMD-6lZ3p4W2nmPLX5krZn2W6hcHLl3RBqltW17ZJvW8jClg5W8V81Tk3VlttCW6Vn9tl3X3DnXMX6mmxdTd6sW8v7Skh5XM77JW7Wvj8p1f4MvDW43b-tC7x8Z2GW40yB2M4DRz2SW2b4-l9649r7xW8kp_zJ6P6fnrW2Fhr4L1tdzCdVfkCp67JW1TSW7Nlr6S8bkbdgW5grt9q4hFHV6W5RRr2p8Mqgq5W5gTXPz2FyvxQW3z0rJj1qbhGvW7Pcn3L4mY0H7f83qYbx04 )

wf-stacked-response-2 (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2CT3l5QzW6N1vHY6lZ3lRW6qkqTR7dx-hsW1zndBs8GWmCjW5pySlj5BSZknN5xgs9xBk-zSW2GZhwr2j4xngV-p26R2Fl8hhW7YqTj-2W9LMNVpZHPR5bcdNrW88Hdl-99hChhW3f92PK6Qw1wwW4j2YTc1k6w8XW2tx3KB98LFdBW5vlL0N5_Jfg9W2V81JX6xFyfmW9gq-Mm29v486W5jcH--2ZZMlbW8Bkxhh82MfNpW70txT24xM5YxW4ctlYJ3V-nfpW3r-Qrm6K9RBcW4T-Hsw9j47fhW6-xvRG1nl-szf1hP2D204 )

wf-stacked-premium-1 (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2CT3l5QzW6N1vHY6lZ3lpW5tsJ-52Wq8JSW7_jZqS686PLqW26T8MC5Dc91bW84s_Fk8KLP-DVbG1Kt1xQCWfW8249VS3CYY4hW9jF4xG8ybR6MW6STl893yX50tW7ypt5G90KzPYW85QL8_7rmrVnVwv7K3150XTyW5gjWSR18NqSQW2VmNHH6B_w25N1ZlP9F1-nbhW1Xm-sV5kJxTrW17mfjK5W8MCDW5t5GNC2mTw9xW6D5_vR5R9WmHN6g9BVMYs-7PVfLg5R39nb21W5PrNP55pxsRtW3Y7snd6rpyWff1wmZyz04 )

wf-stacked-commandline-1024x901 (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2CT3l5QzW6N1vHY6lZ3lrW3GHd3M286qYTW2Q7sPC1SQsZGW6J1Px597gGX9W6ZGjls5rWfp7N210Zz_Gl5rVVQXT3w85SPGPW8Nt02C3zm2VWW6ll7nT4DW410W7mB6423bbMWVW2q5S5F6KYKsCW5ZR_pq3gTk3-W54_HYp155Bw_W2335t51gZltfVHqgkq33n2wzW1dzlXp3VThnmW43Z-dz4Bgv0WVfpsN42pcgYPW1ljb8g31RhGKW8RL7vC5T7V1jW7Bz27h6nBq42N1_ym9Nn0tVQW5n7R_57dV_Jqf8YvGNz04 )

wf-stacked-care-3 (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2Cz3l5QzW69sMD-6lZ3nXW64sM298Q4lCgW1l2-K92pCHD1W412TrQ8Jm8C4N2K7WjwmB3DLW4vnpxc2MG0bBW4vsR2177MVYqVzY3c15s9KZ4W2-qbnt27SP0SW7rq8DM83t11xW77X60y455CG5W979ScG6Zr4L-N3dGbh_dwpRqW3qpyrL67H2xzW5WVn_J1rxjHLW8bJmq53kYLtHW4CD6jt6qrx6bN4Z3_nZpkDgDW590KWS7pZF8nW9hWNkL7vXL80N6fvN53rZZDQf54SSVP04 )

wf-intel-footer (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2Cz3l5QzW69sMD-6lZ3n5W17hx-62TsjBwW2_0LVv2htpvjVWfwHP2PSl--N4sFzjq5tlLMW3xhsnN1lF6cyW8cKTW_98tfkfW7172Qt2p_JDpVkgHlm6LQbJdW8yfX6R45CXXMW86FfbM7Ng9gfW85lg3l71dJlNW51kZtB5n--z5W5dT4sN1T9lrtVJG9LC3gkRL0W7rQ53g84Yz45N6dr1l3ym97qW3HpDfB9gbTcfW1SpHR239bYs-W502PF593DclNN3wJn4Gx9V_Cf5SQdMM04 )

logo-defiant (https://email.wordfence.com/e3t/Ctc/GC+113/cwG7R04/MWP5VKlyWqcW4b8pF686m2wlW5j5c_V547LRKN3fD2Cg3l5QzW5BW0B06lZ3pgN38FPyCv46TCN3sbXYRSKSRMVM2kTJ3-RZy8W5yqmK64yKRjtN5ypr4y7z0m6W4s4cbV1Z8N8HW6vRb7b4n9CX2N3YhYZR1k_hzW7V9nmB37lm54N3b8TnPM93YqN26HPsPpn8ghW7gDZZW5Q1KW0W239q2F1NfpsRN16dWM6hWHbwN5vg-NRjcLL9W6Q3F-p993-dXW7XdsVG74RhG9W5GTd5R8BZLK3f2Vc56j04 )

Defiant, Inc., 1700 Westlake Ave N STE 200, Seattle, WA 98109, United States

Unsubscribe (https://email.wordfence.com/hs/manage-preferences/unsubscribe-all?languagePreference=en&d=Vn9bTK892TtnVsxx1M3JN_XyW41Rcn-4h29fmN6J4V3XmWFdkW8x_3-681L1jFV25Cp85kxVvtN9h8GyLX1Q8_N4_JcS0Xk_7QVbw8444KkcsVW77-kbg7PFtjvw1D06S4mDv2&v=3&_hsenc=p2ANqtz-90aRL_Qcnbs_J0Ho27WP32Q4w7sU3QUSfv1CAqZj8wNW4qh0Nn6SMXZEOK1pFY_Mkn7E5XNhA-lbfqTB4UpiLA_uHp1w&_hsmi=276608175 )

Manage preferences (https://email.wordfence.com/hs/manage-preferences/unsubscribe?languagePreference=en&d=Vn9bTK892TtnVsxx1M3JN_XyW41Rcn-4h29fmN6J4V3XmWFdkW8x_3-681L1jFV25Cp85kxVvtN9h8GyLX1Q8_N4_JcS0Xk_7QVbw8444KkcsVW77-kbg7PFtjvw1D06S4mDv2&v=3&_hsenc=p2ANqtz-90aRL_Qcnbs_J0Ho27WP32Q4w7sU3QUSfv1CAqZj8wNW4qh0Nn6SMXZEOK1pFY_Mkn7E5XNhA-lbfqTB4UpiLA_uHp1w&_hsmi=276608175 )

You're receiving this email because you signed up to the Wordfence WordPress security mailing list.
Received on Mon Oct 02 2023 - 18:09:14 CEST

This archive was generated by hypermail 2.3.0 : Mon Oct 02 2023 - 18:13:02 CEST