• Collection

    • General
  • System

    • Old System
  • AI

    • Python Env Setting
    • 认证
    • Ollama
    • WebUI Setting
  • Raspberry

    • Raspberry Hardware Info
    • Raspberry Setting
  • Daily

    • Daily Tools
  • Resource

    • 逆向
    • Book
    • BlockChain
  • Environment

    • Linux
    • Can Debug
    • Can Detail Debug

check version

cat /etc/os-release

PRETTY_NAME="Kali GNU/Linux Rolling"
NAME="Kali GNU/Linux"
VERSION_ID="2024.3"
VERSION="2024.3"
VERSION_CODENAME=kali-rolling
ID=kali
ID_LIKE=debian
HOME_URL="https://www.kali.org/"
SUPPORT_URL="https://forums.kali.org/"
BUG_REPORT_URL="https://bugs.kali.org/"
ANSI_COLOR="1;31"

uname -r
6.8.11-arm64

uname -m
aarch64

lsb_release -a

No LSB modules are available.
Distributor ID: Kali
Description:    Kali GNU/Linux Rolling
Release:        2024.3
Codename:       kali-rolling
CommandPurpose
cat /etc/os-releaseShow Kali Linux version
uname -rShow kernel version
lsb_release -aShow distribution details
uname -mShow system architecture (32-bit or 64-bit)
`dpkg-query -Wgrep kali`

Prepare

sudo apt update
sudo apt install can-utils python3 python3-pip

# not work
# pip install pycryptodome

# work
sudo apt install python3-pycryptodome

Set up Virtual CAN interface

sudo modprobe
vcan sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
  • modprobe loads kernel modules.
  • vcan is the virtual CAN module, which allows you to create software-based CAN interfaces for testing.
  • This does not create a CAN interface; it just enables the vcan functionality.

Replay Attack (Without Security)

Simulate a Mock ECU Sending Messages

cangen vcan0 -I 123 -L 8 -D 1122334455667788 -v -g 100

# Generate Random Data
cangen vcan0 -I 123 -L 8 -v -g 100

# Generate Incremental Data
cangen vcan0 -I 123 -L 8 -D i -v -g 100

# Send Messages Faster (Reduce Delay)
cangen vcan0 -I 123 -L 8 -D 1122334455667788 -v -g 10

# Limit the Number of Messages
# Sends only 10 messages, then stops.
cangen vcan0 -I 123 -L 8 -D 1122334455667788 -v -g 100 -n 10

OptionMeaning
cangen vcan0Sends CAN frames on vcan0
-I 123Uses CAN ID 0x123 (hex)
-L 8Sets data length to 8 bytes
-D 1122334455667788Uses fixed data payload (11 22 33 44 55 66 77 88 in hex)
-vEnables verbose output (prints sent frames)
-g 100100 ms delay between sending messages

Expected Output

vcan0  123   [8]  11 22 33 44 55 66 77 88
vcan0  123   [8]  11 22 33 44 55 66 77 88
vcan0  123   [8]  11 22 33 44 55 66 77 88

explain

  • vcan0 → Interface
  • 123 → CAN ID (0x123 in hex)
  • [8] → Data length (DLC)
  • 11 22 33 44 55 66 77 88 → Payload

Monitor Message

candump vcan0

Summary

CommandPurpose
cangen vcan0 -I 123 -L 8 -D 1122334455667788 -v -g 100Send fixed CAN frames every 100ms
cangen vcan0 -I 123 -L 8 -v -g 100Send random CAN frames every 100ms
cangen vcan0 -I 123 -L 8 -D i -v -g 100Send incremental CAN frames
cangen vcan0 -I 123 -L 8 -v -g 10Send frames every 10ms
candump vcan0Capture CAN messages

Capture and Log Messages

candump vcan0 > captured_can_log.txt

cat captured_can_log.txt
  vcan0  123   [8]  11 22 33 44 55 66 77 88
  vcan0  123   [8]  11 22 33 44 55 66 77 88
  vcan0  123   [8]  11 22 33 44 55 66 77 88
  vcan0  123   [8]  11 22 33 44 55 66 77 88
  vcan0  123   [8]  11 22 33 44 55 66 77 88
  ...
candump -l vcan0 > captured_can_log.txt

cat candump-2025-03-02_104339.log
(1740930219.186591) vcan0 123#1122334455667788
(1740930219.287877) vcan0 123#1122334455667788
(1740930219.388586) vcan0 123#1122334455667788
(1740930219.489370) vcan0 123#1122334455667788
(1740930219.593078) vcan0 123#1122334455667788
(1740930219.695327) vcan0 123#1122334455667788
...

Python Environment

$ python3 receive_secoc_can.py 
Traceback (most recent call last):
  File "/home/parallels/Documents/Test/lab3/receive_secoc_can.py", line 5, in <module>
    from Crypto.Cipher import AES
ModuleNotFoundError: No module named 'Crypto'
                                                                                                                                           
$ pip install pycryptodome

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.
    
    See /usr/share/doc/python3.13/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

Check

dpkg -L python3-pycryptodome | grep Crypto

Env

sudo apt update
sudo apt install python3-venv

sudo apt install python3.13-venv

python3 -m venv --help

# create env
python3 -m venv myenv

source myenv/bin/activate
source ~/myenv/bin/activate

pip install pycryptodome
pip install python-can


python3 -c "from Cryptodome.Util.Padding import pad, unpad; print('PyCryptodome installed!')"

pip list | grep pycryptodome

python3 -m pip show pycryptodome
Name: pycryptodome
Version: 3.21.0
Summary: Cryptographic library for Python
Home-page: https://www.pycryptodome.org
Author: Helder Eijs
Author-email: helderijs@gmail.com
License: BSD, Public Domain
Location: /home/parallels/myenv/lib/python3.13/site-packages
Requires: 
Required-by: 

check pad function

pwd
/home/parallels/myenv/lib/python3.13/site-packages/Crypto/Util

ls
asn1.py      _cpu_features.py   _file_system.pyi  Padding.py     __pycache__   RFC1751.pyi
asn1.pyi     _cpu_features.pyi  __init__.py       Padding.pyi    _raw_api.py   _strxor.abi3.so
Counter.py   _cpuid_c.abi3.so   number.py         py3compat.py   _raw_api.pyi  strxor.py
Counter.pyi  _file_system.py    number.pyi        py3compat.pyi  RFC1751.py    strxor.pyi


$ grep 'def pad' Padding.py
def pad(data_to_pad, block_size, style='pkcs7'):


Add VCan

sudo ip link set vcan0 down
sudo ip link set vcan0 up


sudo ip link delete vcan0 type vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0


ip -details -statistics link show vcan0

ip link show type can

# check Can
python3 -c "import can; can.Bus(channel='vcan0', interface='socketcan').shutdown()"

Debug

Issue in send_secop_can.py The error "OSError: [Errno 22] Invalid argument" occurs because CAN frames are limited to a maximum of 8 bytes of data, but your script is trying to send 16 bytes.

Last Updated:
Prev
Linux
Next
Can Detail Debug