Quantcast
Channel: 钻戒 and 仁豆米
Viewing all articles
Browse latest Browse all 290

Freelancer任务之六Compile an ipk file on Lede (OpenWRT)

$
0
0

这是一次失败的任务,即使再来一次,依然会失败,因为无法验证,真够shit的,扣了我10$的手续费。

记录一下,以供后效。

任务如下:

Job would be to compile an ipk file of SHC that would work with LEDE OS (openwrt) and the processor used in our system - will provide details

SHC can be downloaded here: http://www.datsi.fi.upm.es/~frosal/

I think you must have a 64 bit system to use the SDK to compile the file  

翻译一下:在OpenWRT平台下编译一个SHC,并且能工作。

完全步骤如下:

How to compile SHC on LEDE:

Tested build environment:

OS: Ubuntu 14.04.5 LTS  
CPU: ARMv7 Processor rev 5 (v7l)


Before you begin, check your system is updated, i.e.

sudo apt-get update  
sudo apt-get upgrade  
sudo apt-get autoremove


Step-by-step manual:  
Note: perform all steps as regular (non-root) user. User must be in sudo group.

1. Update your sources

sudo apt-get update

2. Install nesessary packages:

sudo apt-get install g++ libncurses5-dev zlib1g-dev bison flex unzip autoconf gawk make gettext gcc binutils patch bzip2 libz-dev asciidoc subversion sphinxsearch libtool sphinx-common libssl-dev libssl0.9.8

3. Get latest LEDE source from git repository  
   We know our CPU is armv7, it belongs to arm64, so go to http://downloads.lede-project.org/releases , just download sdk.

wget http://downloads.lede-project.org/releases/17.01.4/targets/arm64/generic/lede-sdk-17.01.4-arm64_gcc-5.4.0_musl-1.1.16.Linux-x86_64.tar.xz


4. No Need to Update and install all LEDE packages  
   We just want to compile shc, not other packages , so don't update.


5. Run 'make menuconfig' (Just Save and Exit)


6. Get SHC sources to LEDE package tree  
 (we are and shc-3.8.9b.tgz is in source directory)

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9b.tgz  
mkdir -p package/shc/src  
tar xvf shc-3.8.9b.tgz -C package/shc/src --strip-components 1

7. Make ipk Makefile

vi package/shc/Makefile  
##############################################
# OpenWrt Makefile for shc program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to 
# specify a basic description of the package, 
# where to build our program, where to find 
# the source files, and where to install the 
# compiled program on the router. 
# 
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and 
# there should be no trailing whitespace in
# lines that are not commented.
# 
##############################################

include $(TOPDIR)/rules.mk

# Name and release number of this package

PKG_NAME:=shc  
PKG_VERSION:=3.8.9b  
PKG_MAINTAINER:=Francisco, Rosales, <frosal@fi.upm.es>


# This specifies the directory where we're going to build the program.  
# The root build directory, $(BUILD_DIR), is by default the build_mipsel 
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)


include $(INCLUDE_DIR)/package.mk



# Specify package information for this program. 
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION 
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/$(PKG_NAME)  
    SECTION:=utils
    CATEGORY:=Utilities
    TITLE:= shc ---- This tool generates a stripped binary executable version of the script specified at command line.
    URL:=http://www.datsi.fi.upm.es/~frosal
endef


# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/$(PKG_NAME)/description  
    shc ---- This tool generates a stripped binary executable version
        of the script specified at command line."
endef

# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare  
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef


# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one

# Specify where and how to install the program. Since we only have one file, 
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running 
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install 
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the 
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/$(PKG_NAME)/install  
    $(INSTALL_DIR) $(1)/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/shc $(1)/bin/
endef


# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,$(PKG_NAME)))


8. Compile shc ipk without errors.

make package/shc/compile V=99

9. Building process complete witout errors.  
Now we have :  
binary packages directory: source/bin/packages/aarch64_armv8-a/  
[SHC_BIN] = ./bin/packages/aarch64_armv8-a/base/shc_3.8.9b_aarch64_armv8-a.ipk

10. Copy and install SHC .ipk to LEDE device.

scp shc_3.8.9b_aarch64_armv8-a.ipk root@<LEDE device IP address or name>:/tmp/  
ssh root@<LEDE device IP address or name> #IP usually 192.168.1.1  
opkg install shc_3.8.9b_aarch64_armv8-a.ipk

11. Create test script and compile it to execute. (in LEDE shell)  
ssh root@<LEDE device IP address or name> #IP usually 192.168.1.1

vi /tmp/1.sh  
#!/bin/sh
echo "hahahaha"

shc -v -f /tmp/1.sh  
/tmp/1.sh.x

这里面有几个注意点,一个是网上有很多教程,上去就是

./scripts/feeds update -a
./scripts/feeds install -a

这个千万不要,这两条命令是把所有的库和软件都给下载下来的,如果再编译,基本半天过去了,浪费时间生命。我们的目标只是要编译个shc,其他都不管。

第二点就是那个Makefile,其实编译shc就一句话,但是我们要放到LEDE里就必须有这个Makefile.

这样就顺利编译出来个shc3.8.9baarch64_armv8-a.ipk,但是雇主反映拷上去不能用,我去!

这才又仔细研究shc,这个软件其实是个混淆的软件,把shell脚本变成*.c,然后再用gcc编译成2进制文件。

但是依OpenWRT的性格,一般根本装不下gcc的,所以必须外挂U盘装GCC上去,装法如下:

http://www.th7.cn/Program/cp/201708/1224085.shtml

验证方法也比较古怪,首先写好shell,然后跑到openwrt上用shc进行混淆,然后把混淆过的.c文件拷出来,再用gcc编译成bin,然后再拷回openwrt运行验证。

再附上单独用gcc编译的方法:

export STAGING_DIR=~/LEDE-IPQ40XX$/staging_dir  
cd ~/LEDE-IPQ40XX$  
./staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-7.3.0_musl_eabi/bin/arm-openwrt-linux-muslgnueabi-gcc  script.sh.x.c -o script.sh.x

shit啊,其实我编译出来的shc已经是可以运行并混淆出*.c的,但是不知道为什么arm-gcc编译出的bin文件在openwrt上无法运行,更悲剧的是我没有他那个平台,无法自己装个gcc看看到底是怎么回事

悲惨世界啊,被扣了10$手续费。

最后问了雇主,他又找了一个家伙解决了问题,给出的方法如下:

9. Compile shell script

${HOME}/shc.sh -r ${HOME}/openwrt-sdk -n hello-world -s ${HOME}/hello-world.sh.x.c;
ls -lh ${HOME}/openwrt-sdk/bin/packages/arm_cortex-a7_neon-vfpv4/base/script_*;  
ls -lh ${HOME}/openwrt-sdk/build_dir/target-arm_cortex-a7+neon-vfpv4_musl_eabi/hello-world-0.0.1/hello-world;

10.  Install the script package on OpenWrt.

scp ${HOME}/openwrt/bin/packages/arm_cortex-a7_neon-vfpv4/base/script_* root@192.168.1.1:/tmp/;  
ssh root@192.168.1.1;  
opkg install /tmp/script_*;  
exit;  

看明白了吧,我也无法验证,靠有心人去试试了。记录一下。


Viewing all articles
Browse latest Browse all 290

Trending Articles