Below is a penetration testing memo for Linux machines; I came across it on the internet by chance and copied it over here.

Penetration testing a Linux machine

CommandDescription
netstat -tulpnShow network ports in Linux together with the corresponding ID (PID)
watch ss -stpluObserve TCP and UDP ports in real time through sockets
lsof -iShow established connections
macchanger -m MACADDR INTRChange the MAC address on KALI Linux
ifconfig eth0 192.168.2.1/24Set the IP address in Linux
ifconfig eth0:192.168.2.3/24Add an IP address to an existing network interface in Linux
ifconfig eth0 hw ether MAC ADDRChange the address in Linux using ifconfig
ifconfig eth0 mtu 1500Change the MTU size with ifconfig; change 1500 to the MTU you want
dig -x 192.168.1.1Perform a reverse lookup on an IP address
host 192.168.1.1Perform a reverse lookup on an IP address; useful when dig is not installed
dig @192.168.2.2 domain.com -t AXFRPerform a DNS zone transfer with dig
host -l domain.com nameserverPerform a DNS zone transfer with host
nbtstat -A x.x.x.xObtain the domain name corresponding to an IP address
ip addr add 192.168.2.22/24 dev eth0Add a hidden IP address that does not show up when running ifconfig
tcpkill -9 host goole.comBlock access to goole.com from the host
echo "1">/proc/sys/net/ipv4/ip_forwardEnable IP forwarding, turning the Linux box into a router — which makes it convenient to control the traffic routed through this box
echo "8.8.8.8">/etc/resolv.confUse Goole's DNS

System information commands

Very helpful for local enumeration

CommandDescription
whoamiShow the currently logged-in user on Linux
idShow the currently logged-in user and group
lastShow the last logged-in users
mountShow mounted drives
df -hShow disk usage in human-readable output
echo "user:passwd" | chpasswdReset a password with a single command
getent passwdList the users on Linux
strings /usr/local/bin/blahShow the content of a non-text file, for example what is inside a binary
uname -arShow the running kernel version
PATH=$PATH:/my/new-pathAdd a new path, making file system (FS) operations more convenient
historyShow the user's history of previously executed bash scripts, along with the commands typed

Redhat/CentOS/RPM based distributions

CommandDescription
cat /etc/redhat-releaseShow the Redhat/CentOS version number
rpm -qaList all installed RPM packages on RPM-based Linux
rpm -q --changelog openvpnCheck whether an installed RPM has been patched against a CVE; the grep command can be used to filter the output related to CVEs

YUM commands

CommandDescription
yum updateUpdate all RPM packages with YUM; it also shows which ones are outdated
yum update httpdUpdate a single package, in this example HTTPD (Apache)
yum install packageInstall a package with YUM
yum --exclude=package kernel* updateExclude a package from being updated when using YUM
yum remove packageRemove a package with YUM
yum erase packageRemove a package with YUM
yum list packageList information about a YUM package
yum provides httpdShow what a package provides
yum info httpdShow package information, architecture, version and so on
yum local install blah.rpmUse YUM to install a local RPM, installing from the repository
yum deplist packageShow the provider information of a package
yum list installed | moreList all installed packages
yum grouplist | moreShow all YUM groups
yum groupinstall 'Development Tools'Install a YUM group

Debian/Ubuntu/.deb based distributions

CommandDescription
cat /etc/debian_versionShow the Debian version number
cat /etc/*-releaseShow the Ubuntu version number
dpkg -lList all installed packages on Debian/.deb based Linux distributions

Linux user management

CommandDescription
useradd new-userCreate a new Linux user
passwd usernameReset a Linux user's password; if you are the root user you only need to enter the password
userdel usernameDelete a Linux user

User creation commands

adduser: automatically specifies the home directory and system shell version for the created user, and asks for the user password at creation time useradd: the basic settings above must be specified with parameter options; if no parameters are used, the created user has no password, no home directory and no specified shell version

User deletion commands

userdel: delete a user; adding the option -r deletes all files related to the user, and -f forces deletion of the user even if the user is logged in deluser: remove a user from a group (that is, leave the user group) deluser username usergroup

Linux decompression commands

CommandDescription
unzip archive.zipExtract the files in a zip archive on Linux
zipgrep *.txt archive.zipSearch inside a zip archive
tar xf archive.tarExtract the files in a tar archive on Linux
tar xvzf archive.tar.gzExtract the files in a tar.gz archive on Linux
tar xjf archive.tar.bz2Extract the files in a tar.bz2 archive on Linux
tar ztvf file.tar.gz | grep blahSearch inside a tar.gz file
gzip -d archive.gzExtract the file in a gzip archive on Linux
zcat archive.gzRead a .gz file on Linux without decompressing it
zless archive.gzAchieve the same functionality for a .gz archive with the less command
zgrep 'blah' /var/log/maillog*.gzSearch the contents of .gz archives on Linux, for example searching compressed log files
vim file.txt.gzRead a .txt.gz file with vim

Linux compression commands

CommandDescription
zip -r file.zip /dir/*Create a .zip file on Linux
tar cf archive.tar filesCreate a .tar file on Linux
tar czf archive.tar.bz2 filesCreate a tar.gz file on Linux
tar cjf archive.tar.bz2 filesCreate a tar.bz2 file on Linux
gzip fileCreate a .gz file on Linux
upx -9 -o output.exe input.exeCompress an .exe file with UPX on Linux

Linux file commands

CommandDescription
df -h blahShow the size of a file/directory on Linux
diff file1 file2Compare/show the difference between two files on Linux
md5sum fileGenerate an MD5 digest on Linux
md5sum -c blah.iso.md5Check a file's MD5 digest on Linux, assuming the file and the .md5 are in the same path
file blahFind out the type of a file on Linux; it also shows whether the file is 32 or 64 bit
dos2unixConvert Windows line endings to Unix/Linux ones
base64 input-file output-fileBase64-encode the input file and output a Base64-encoded file named output-file
base64 -d input-file output-fileBase64-decode the input file and output a Base64-decoded file named output-file
touch -r ref-file new-fileCreate a new file using the timestamp data from the reference file; drop -r to simply create a file
rm -rfDelete files and directories without showing a confirmation prompt

Misc commands

CommandDescription
init 6Restart Linux from the command line
gcc -o output.c input.cCompile C code
gcc -m32 -o output.c input.cCross-compile C code; on 64-bit Linux this compiles a 32-bit binary
unset HISTORYFILETurn off bash history logging
rdesktop x.x.x.xConnect to an RDP server from Linux
kill -9 $$Kill the current session
chown user:group blahChange the owner of a file or directory
chown -R user:group blahChange the owner of a file or directory as well as the files/directories under it — recursively execute chown
chmod 600 fileChange the permission settings of a file/directory; see Linux file system permissions for details

Clearing bash history

$ ssh user@x.x.x.x | cat /dev/null > ~/.bash_history

The .bash_history file can hold many history entries; the number of saved commands can be reduced by setting the HISTFILESIZE and HISTSIZE parameters.

The meaning of the two parameters:

  • HISTSIZE: defines the number of commands output by the history command
  • HISTFILESIZE: defines the number of commands saved in the .bash_history file
$ HISTFILESIZE=<num>

$ HISTSIZE=<num>

The command history -c clears the commands output this time but does not clear the .bash_history file, so after logging in again the old commands will still appear.

Linux file permissions

ValueMeaning
777rwxrwxrwx no restriction, fully readable, writable and executable (RWX); the user can do anything
755rwxr-xr-x the owner has full access, others can only read and execute
700rwx------ the owner has full access, no one else can access it
666rw-rw-rw- everyone can read and write, but not execute
644rw-r--r-- the owner can read and write, others can only read
600rw------- the owner can read and write, no one else can access it

Penetration testing memo for the Linux file system

PathDescription
// is also called "slash" or root
/binGeneral programs shared by the system, the system administrator and users
/bootBoot files, boot loader (grub), kernel, vmlinuz
/devContains references to system devices, files with special attributes
/etcImportant system configuration files
/homeHome directories of system users
/libLibrary files, including files for all kinds of programs needed by both the system and users
/lost+foundFiles from failed file operations are saved here
/mntStandard mount point for external file systems
/mediaMount point for external file systems (or for some distributions)
/netStandard mount point for entire remote file systems — nfs
/optGenerally contains some additional or third-party software
/procA virtual file system containing information related to system resources
/rootHome directory of the root user
/sbinPrograms used by the system and the system administrator
/tmpTemporary space for the system's use, cleared on reboot
/usrPrograms, libraries, documentation and so on used by all user-related programs
/varStores all variable files and transient files created by users, such as log files, mail queues, print spoolers, web servers, databases and so on

Some interesting files/directories in Linux

Very helpful for privilege escalation / post-exploitation

PathDescription
/etc/passwdContains the local Linux users
/etc/shadowContains the hashed local account passwords
/etc/groupContains the local account groups
/etc/init.d/Contains the service initialization scripts — worth a look to see exactly what is installed
/etc/hostnameThe system's hostname
/etc/network/interfacesNetwork interfaces
/etc/resolv.confThe system's DNS service
/etc/profileThe system's environment variables
~/.ssh/SSH keys
~/.bash_historyThe user's bash history log
/var/log/Linux system log files are generally stored here
/var/adm/UNIX system log files are generally stored here
/var/log/apache2/access.log /var/log/httpd/access.logThe usual storage paths of the Apache access log files
/etc/fstabMounted file systems

Breaking out of shell restrictions

Python small tricks:

python -c 'import pty;pty.spawn("/bin/bash")'
echo os.system("/bin/bash")
/bin/sh -i

Samba commands

Connecting to a Samba share from Linux:

$ smbmount //server/share /mnt/win -o user=username,password=password1
$ smbclient -U user \\\\server\\share
$ mount -t cifs -o username=user,password=password  //x.x.x.x/share /mnt/share

Reference

Linux enthusiasts