“Like LeetCode for Linux”
SadServers 通关记录
关于 SadServers
Troubleshoot and make a sad server happy!
通关记录
“Saint John”: what is writing to this log file?
1 2 3 4 5
| lsof |grep /var/log/bad.log
kill -9 [pid]
|
1 2
| find /var/log/bad.log -mmin -0.1
|
“Saskatoon”: counting IPs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| awk '{print $1}' /home/admin/access.log
awk '{print $1}' /home/admin/access.log |sort
awk '{print $1}' /home/admin/access.log |sort |uniq -c
awk '{print $1}' /home/admin/access.log |sort |uniq -c |sort -r
awk '{print $1}' /home/admin/access.log |sort |uniq -c |sort -r |head -1 |awk '{print $1}'
awk '{print $1}' /home/admin/access.log |sort |uniq -c |sort -r |head -1 |awk '{print $1}' > /home/admin/highestip.txt
|
1 2
| sha1sum /home/admin/highestip.txt
|
“Santiago”: Find the secret combination
题目描述:
- 找到
/home/admin
目录中字符串 Alice
在 *.txt
文件中的出现次数 - 在字符串
Alice
仅出现一次的文件中在此之后一行中的数字 - 将这两个数字写入
/home/admin/solution
解答:
1 2 3 4 5 6 7 8 9 10 11
| find /home/admin -type f -name "*.txt" |xargs grep -c 'Alice'
echo -n 411 > /home/admin/solution
cat /home/admin/1342-0.txt| grep 'Alice' -A 1
echo 156 >> /home/admin/solution
|
1 2
| md5sum /home/admin/solution
|
“Manhattan”: can’t write data into database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| sudo systemctl restart postgresql
journalctl -p err
cat /var/log/syslog
df -h
du -sh /opt/pgdata/main
rm /opt/pgdata/*.bk sudo systemctl restart postgresql
|
1 2
| sudo -u postgres psql -c "insert into persons(name) values ('jane smith');" -d dt
|
“Tokyo”: can’t serve web file
1 2 3 4 5 6 7 8 9 10 11 12 13
|
iptables -L
iptables -F
ls -l /var/www/html/index.html
chmod 777 /var/www/html/index.html
|
“Cape Town”: Borked Nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| systemctl restart nginx
nginx -t
cat /var/log/nginx/error.log
systemctl daemon-reload systemctl restart nginx
|
1
| curl -Is 127.0.0.1:80| head -1
|
“Salta”: Docker container won’t start
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| docker ps -a
docker logs 124a4fb17a1c
cd /home/admin/app vim Dockerfile
docker build -t app .
docker run -d -p 8888:8888 app
netstat -tunlp| grep 8888 kill -9 620
|
“Venice”: Am I in a container
1 2 3 4 5
| cat /proc/1/environ|tr "\0" "\n"
ls -ali
|
“Oaxaca”: Close an Open File
1 2 3 4 5
| lsof /home/admin/somefile
exec 77>&-
|
1 2
| lsof /home/admin/somefile
|
“Melbourne”: WSGI with Gunicorn
问题描述:
/home/admin/WSGI.py
是一个Python WSGI web应用程序文件,其目的是提供字符串“Hello,world!”。- 该文件由 Gunicorn 服务器提供,该服务器由 nginx 服务器前置(两个服务器均由 systemd管理)。
- 因此,HTTP请求的流程是:
Web Client(curl)->Nginx->Gunicorn->wsgi.py
。 - 目标是配置服务器使得
curl -s http://localhost
时返回“Hello,world!”。
解答:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| curl localhost
cat /etc/nginx/sites-enabled/default
cat /etc/systemd/system/gunicorn.service
curl localhost
curl -i localhost
systecm restart gunicorn
|
1
| curl -s http://localhost
|
“Lisbon”: etcd SSL cert troubles
1 2 3 4 5 6 7 8 9 10 11
| etcdctl get foo
date -s "last year"
sudo /usr/sbin/iptables -t nat -L
sudo /usr/sbin/iptables -t nat -F
|
“Jakarta”: it’s always DNS
1 2 3 4
|
vim /etc/nsswitch.conf
|
“Bern”: Docker web container can’t connect to db container
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| docker exec wordpress env | grep "WORDPRESS_DB"
docker inspect mariadb
docker exec wordpress grep WORDPRESS_DB_ /var/www/html/wp-config.php
docker exec wordpress mysqladmin -h mysql -u root -ppassword -h 172.17.0.2
docker stop wordpress ; \ docker rm wordpress ; \ docker run -d \ --name wordpress \ -v html:/var/www/html \ --link mariadb:mysql \ -p 80:80 \ -e WORDPRESS_DB_HOST=mysql \ -e WORDPRESS_DB_NAME=wordpress \ -e WORDPRESS_DB_USER=root \ -e WORDPRESS_DB_PASSWORD=password \ wordpress:sad
|
1 2
| sudo docker exec wordpress mysqladmin -h mysql -u root -ppassword ping
|
“Karakorum”: WTFIT – What The Fun Is This?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| /home/admin/wtfit
chmod +x /home/admin/wtfit
perl -e 'chmod 0755, "/usr/bin/chmod"'
/lib64/ld-linux-x86-64.so.2 /usr/bin/chmod +x /usr/bin/chmod
chmod +x /home/admin/wtfit
/home/admin/wtfit
strace /home/admin/wtfit
touch /home/admin/wtfitconfig.conf
/home/admin/wtfit
strace /home/admin/wtfit
nc -l -p 7777
python3 -m http.server --bind 127.0.0.1 7777
|
“Singara”: Docker and Kubernetes web app not working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| kubectl get -A pod
docker pull webapp
docker images
docker run -d -p 5000:5000 registry:2
docker tag webapp localhost:5000/webapp docker push localhost:5000/webapp
kubectl delete deploy webapp-deployment -n web
vim /home/admin/deployment.yml
kubectl apply -f /home/admin/deployment.yml
kubectl port-forward deployments/webapp-deployment 8888 -n web
|
“Hong-Kong”: can’t write data into database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
journalctl -p err
lsblk -f
mount /dev/nvme0n1 /opt/pgdata
journalctl|tail
vim /etc/fstab
systemctl daemon-reload mount /dev/nvme0n1 /opt/pgdata
df -h
du -sh /opt/pgdata/main
rm /opt/pgdata/*.bk sudo systemctl restart postgresql
|
1 2
| sudo -u postgres psql -c "insert into persons(name) values ('jane smith');" -d dt
|
“Pokhara”: SSH and other sshenanigans
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| sudo -u client ssh client@localhost 'pwd'
ssh-keygen -f "/home/client/.ssh/known_hosts" -R "localhost"
sudo -u client ssh client@localhost 'pwd'
rm /etc/ssh/sshd_config.d/sad.conf systemctl restart ssh
sudo -u client ssh client@localhost 'pwd'
chmod 600 /home/client/.ssh/id_rsa
sudo -u client ssh client@localhost 'pwd'
lslogins client
grep client /etc/shadow
chage -E-1 client
sudo -u client ssh client@localhost 'pwd'
vim /etc/security/limits.conf
sudo -u client ssh client@localhost 'pwd'
lslogins client
cat /etc/shells
usermod --shell /bin/bash client
|
1 2
| sudo -u client ssh client@localhost 'pwd'
|
“Roseau”: Hack a Web Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| ls /var/www/html/
curl localhost
vim /etc/apache2/sites-enabled/000-default.conf
cd ~ ; john/run/john /etc/apache2/.htpasswd
curl localhost/webfile -u "carlos:chalet" --output secret
file secret
unzip secret
john/run/zip2john secret > zip.hash
john/run/john zip.hash
unzip secret
|
1 2
| sha1sum /home/admin/secret.txt |awk '{print $1}'
|
参考资料