Who is this computer talking to?
A server is sending a few bytes every thirty seconds to an address nobody recognises. The firewall log shows the address, but not which program on the server is doing it.
The kernel knows. Every connection belongs to a process, and that link is in memory. This lesson reads it.
By the end of this lesson
Note
I can list the network connections in a memory image and tie each one to its process.
Key words
| Word | What it means | Example |
|---|---|---|
| Socket | one end of a network connection inside a program | |
| Port | a number that picks one service on an address | 443 |
| Listening | waiting for others to connect | |
| Established | connected and able to send data | |
| Indicator of compromise | a detail that shows an attack, such as an address |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsThe one rule
Important
Read only your own lab VM's connections. Another computer's memory and traffic need written permission or a lawful order, under the Computer Misuse Act 1990. The far end here is a stand-in on your own VM, on an address reserved for examples.
The scene again, and a fresh image
This lesson uses lesson 5's scene.py, memgrab.py, tools and symbol table in ~/forensics. The table still fits: the kernel is the same. If the first Run says something is missing, do lesson 5 on this VM first.
Run again, the scene stops its old processes and starts new ones, and the memory is taken again.
Find lesson 5's files and add the tools
cd ~/forensics && ls -d scene.py memgrab.py symbols && . ~/memtools/bin/activateShown here, not run. Press Start lab to type it into your own lab’s terminal.
Start the made-up intrusion again
sudo python3 scene.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Acquire this VM's memory
sudo python3 memgrab.py mem.limeShown here, not run. Press Start lab to type it into your own lab’s terminal.
Every socket belongs to a process
A program talks over the network through a socket. A server's socket is listening on a port; once someone connects, a second socket is established, with an address and port at each end.
linux.sockstat walks each process's open files and prints every socket among them, with its owner.
List every TCP socket and its process
vol -q -f mem.lime -s symbols linux.sockstat | awk '$9 == "TCP"'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsSave it, then sum it up
Volatility can print CSV with -r csv, which a script reads easily. The script prints each listener on every address and each established connection, one line each, so the odd ones stand out.
linux.ip.Addr then shows the addresses on each interface, read from memory too.
Save the sockets as CSV
vol -q -r csv -f mem.lime -s symbols linux.sockstat > sockets.csvShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the summary script
printf '%s\n' 'import csv' 'import sys' 'rows = list(csv.DictReader(open(sys.argv[1])))' 'for row in rows:' ' if not row["Proto"] == "TCP":' ' continue' ' who = row["Process Name"] + " " + row["PID"]' ' if row["State"] == "LISTEN" and row["Source Addr"] in ["0.0.0.0", "::"]:' ' print(who, "listens on every address, port", row["Source Port"])' ' elif row["State"] == "ESTABLISHED":' ' print(who, row["Source Addr"] + ":" + row["Source Port"], "connected to", row["Destination Addr"] + ":" + row["Destination Port"])' > sockets.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Sum up the TCP sockets
python3 sockets.py sockets.csvShown here, not run. Press Start lab to type it into your own lab’s terminal.
Which addresses does the VM have?
vol -q -f mem.lime -s symbols linux.ip.Addr | awk '$6 ~ /[.]/'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsTry it: one of our networks, or outside?
import ipaddress
ours = [ipaddress.ip_network("10.0.0.0/8"), ipaddress.ip_network("127.0.0.0/8")]
for text in ["10.0.2.15", "127.0.0.1", "198.51.100.23"]:
address = ipaddress.ip_address(text)
print(text, "ours" if any(address in net for net in ours) else "outside")
# Add "192.168.1.20" to the list, then press Run.
Make a free account to save your work and track your progress.
Memory against the live view
ss asks the running kernel the same question. Here the two agree, because nothing is hiding. A rootkit can make the live tools lie, so memory, read by another computer, is the second opinion.
Write the findings down as indicators of compromise, then stop the process and check it has gone.
Ask the live kernel
sudo ss -tnp | grep -e 443 -e 4444Shown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think the live tools on a machine always tell the truth. On a compromised machine they run on the attacker's kernel, which can hide a process or a socket. Take memory and read it elsewhere when the machine itself is in doubt.
Write the indicators to a file
printf "%s\n" "address 198.51.100.23 port 443" "listener port 4444" "process name kworker, args [kworker/1:3] -" "deleted program /dev/shm/.k/kworker" "string LAB-7F3A" > iocs.txt && cat iocs.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Keep the disguised process's id
KPID=$(vol -q -f mem.lime -s symbols linux.psaux | awk '$3 == "kworker" {print $1}') && echo $KPIDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Stop it and the stand-in, remove the address
sudo kill $KPID && sudo pkill -f labserver.py && sudo ip addr del 198.51.100.23/32 dev loShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check nothing listens on 4444 any more
sudo ss -tlnp | grep -c 4444Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAssignment: flag suspicious sockets
A socket is a dictionary with "process", "pid", "proto", "src", "sport", "dst", "dport" and "state", as linux.sockstat prints them (ports are ints).
listeners(socks) returns (process, port) for every TCP socket in state LISTEN on 0.0.0.0 or ::, in order.
talkers(socks) returns a dictionary from each pid with an ESTABLISHED TCP socket to a sorted list of the distinct dst:dport strings it is connected to.
flag(socks, known, ours) returns the sorted list of pids that listen on every address on a port not in known, or are connected to an address outside every network in ours (strings such as "10.0.0.0/8"; use ipaddress), leaving out connections whose destination is the socket's own address.
import ipaddress
def listeners(socks):
return []
def talkers(socks):
return {}
def flag(socks, known, ours):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go