datetime:2024/11/19 15:12
author:nzb

ROS1源码加密混淆

  • 安装pyarmor
pip install pyarmor
  • 创建ros1工作空间和包
mkdir -p ~/ros1_ws/src
cd ~/ros1_ws/src
catkin_create_pkg ros1_pyarmor_demo rospy
cd ros1_pyarmor_demo 
mkdir -p scripts/source_code/test
touch scripts/source_code/demo1.py
touch scripts/source_code/test/demo2.py
touch setup.py
  • 文件结构
ros1_pyarmor_demo/
├── CMakeLists.txt
├── package.xml
├── scripts
│   └── source_code
│       ├── demo1.py
│       └── test
│           └── demo2.py
├── setup.py
└── src

4 directories, 5 files
  • setup.py
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup, get_package_python_destination

d = generate_distutils_setup(
    packages=['pytransform'],
    package_dir={'': 'scripts/dist'},
)

setup(**d)
  • demo1.py
#!/usr/bin/env python

import rospy


def print_hello(event):
    rospy.loginfo("Hello, world1!")


if __name__ == "__main__":
    rospy.init_node("demo_test1", anonymous=False)
    rospy.Timer(rospy.Duration(1), print_hello)
    rospy.spin()
  • demo2.py
#!/usr/bin/env python
"""
pyarmor-7 obfuscate source_code/*.py --output dist  # 多个文件
pyarmor-7 obfuscate -r source_code/*.py source_code/test/*.py --output dist # 多个文件夹
"""

import rospy


def print_hello(event):
    rospy.loginfo("Hello, world!2")


if __name__ == "__main__":
    rospy.init_node("demo_test2", anonymous=False)
    rospy.Timer(rospy.Duration(1), print_hello)
    rospy.spin()
  • CMakeLists.txt
cmake_minimum_required(VERSION 3.0.2)
project(pyarmor_demo)

find_package(catkin REQUIRED COMPONENTS
  rospy
)

catkin_python_setup()  # 注意开启这个

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES pyarmor_demo
#  CATKIN_DEPENDS rospy
#  DEPENDS system_lib
)

include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

# pyarmor 加密混淆
EXECUTE_PROCESS(COMMAND pyarmor-7 obfuscate -r
  ${CMAKE_CURRENT_SOURCE_DIR}/scripts/source_code/demo1.py 
  ${CMAKE_CURRENT_SOURCE_DIR}/scripts/source_code/test/demo2.py 
  --output ${CMAKE_CURRENT_SOURCE_DIR}/scripts/dist)

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/scripts/dist/pytransform/_pytransform.so
        DESTINATION  ${CMAKE_CURRENT_SOURCE_DIR}/../../devel/lib/python3/dist-packages/pytransform)

# pyarmor

catkin_install_python(PROGRAMS
  scripts/dist/demo1.py          # 加密混淆后的文件
  scripts/dist/test/demo2.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

手动编译

  • pyarmor-7 obfuscate source_code/*.py --output dist # 多个文件
  • pyarmor-7 obfuscate -r source_code/*.py source_code/test/*.py --output dist # 多个文件夹

自动编译CMakeLists.txt中添加,如上文件内容

迁移

  • 打包
    • cd ~/ros1_ws # 项目路径假设在~/ros1_ws
    • catkin_make
    • tar -zcvf ros1_code.tar.gz --exclude=**/source_code/* devel/ src/ # 排除source_code文件夹
  • 迁移
    • 新机器上创建跟编译环境一样的项目路径,否则找不到包
    • mkdir -p ~/ros1_ws/ && cd ros1_ws
    • 压缩包解压到~/ros1_ws
    • source /opt/ros/noetic/setup.bash
    • source devel/setup.bash
    • rosrun ros1_pyarmor_demo demo.py

ros2 闭源

results matching ""

    No results matching ""