Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Finding network connections in a memory image

After this lesson: I can list the network connections in a memory image and tie each one to its process.

65 min lesson13 commandsIncident response and forensics lab: investigate a compromised server in your browser

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
Socketone end of a network connection inside a program
Porta number that picks one service on an address443
Listeningwaiting for others to connect
Establishedconnected and able to send data
Indicator of compromisea 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

The 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

Command
cd ~/forensics && ls -d scene.py memgrab.py symbols && . ~/memtools/bin/activate

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Start the made-up intrusion again

Command
sudo python3 scene.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Acquire this VM's memory

Command
sudo python3 memgrab.py mem.lime

Shown 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

Command
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Save 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

Command
vol -q -r csv -f mem.lime -s symbols linux.sockstat > sockets.csv

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the summary script

Command
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.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Sum up the TCP sockets

Command
python3 sockets.py sockets.csv

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Which addresses does the VM have?

Command
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Try it: one of our networks, or outside?

Code: python
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

Command
sudo ss -tnp | grep -e 443 -e 4444

Shown 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

Command
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.txt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Keep the disguised process's id

Command
KPID=$(vol -q -f mem.lime -s symbols linux.psaux | awk '$3 == "kworker" {print $1}') && echo $KPID

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Stop it and the stand-in, remove the address

Command
sudo kill $KPID && sudo pkill -f labserver.py && sudo ip addr del 198.51.100.23/32 dev lo

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check nothing listens on 4444 any more

Command
sudo ss -tlnp | grep -c 4444

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Assignment: 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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.