From 3a2f859acc5317aaa0c26be5b9dcaecd493bd5f4 Mon Sep 17 00:00:00 2001 From: VHSgunzo Date: Sat, 9 Nov 2024 10:00:42 +0300 Subject: [PATCH] v0.1.1 --- Cargo.toml | 2 +- README.md | 22 +++++++-------- lib4bin | 81 ++++++++++++++++++++++++++++++++---------------------- 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 34a115b..9220b1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sharun" -version = "0.0.9" +version = "0.1.1" readme = "README.md" license = "MIT" repository = "https://github.com/VHSgunzo/sharun" diff --git a/README.md b/README.md index 4a7ef3a..d1466ef 100644 --- a/README.md +++ b/README.md @@ -48,22 +48,22 @@ cp ./target/$(uname -m)-unknown-linux-musl/release/sharun . ## Usage lib4bin: ``` -[ Usage ]: lib4bin [options] /path/executable +[ Usage ]: lib4bin [OPTIONS] /path/executable -- [STRACE CMD ARGS] [ Options ]: - -s, --strip Strip binaries and libraries (env: STRIP=1) - -v, --verbose Verbose mode (env: VERBOSE=1) + -a, --any-executable Pack any executable (env: ANY_EXECUTABLE=1) -d, --dst-dir '/path' Destination directory (env: DST_DIR=/path) - -n, --not-one-dir Separate directories for each executable (env: ONE_DIR=0) - -l, --libs-only Pack only libraries (env: LIBS_ONLY=1) - -p, --hard-links Create hard links to sharun (env: HARD_LINKS=1) - -r, --patch-rpath Patch RPATH to a relative path (env: PATCH_RPATH=1) + -e, --strace-mode Use strace for get libs (env: STRACE_MODE=1, STRACE_TIME=5) -g, --gen-lib-path Generate a lib.path file (env: GEN_LIB_PATH=1) - -a, --any-executable Pack any executable (env: ANY_EXECUTABLE=1) + -h, --help Show this message -i, --patch-interpreter Patch INTERPRETER to a relative path (env: PATCH_INTERPRETER=1) + -l, --libs-only Pack only libraries (env: LIBS_ONLY=1) + -n, --not-one-dir Separate directories for each executable (env: ONE_DIR=0) + -p, --hard-links Pack sharun and create hard links (env: HARD_LINKS=1) -q, --quiet-mode Show only errors (env: QUIET_MODE=1) - -e, --strace-mode Use strace for get libs (env: STRACE_MODE=1, STRACE_TIME=5) - -h, --help Show this message + -r, --patch-rpath Patch RPATH to a relative path (env: PATCH_RPATH=1) + -s, --strip Strip binaries and libraries (env: STRIP=1) + -v, --verbose Verbose mode (env: VERBOSE=1) -w, --with-sharun Pack sharun from PATH or env or download (env: WITH_SHARUN=1, SHARUN=/path|URL, SHARUN_URL=URL, UPX_SHARUN=1) ``` @@ -74,7 +74,7 @@ cp ./target/$(uname -m)-unknown-linux-musl/release/sharun . ./sharun lib4bin --with-sharun --dst-dir test /bin/bash # or for correct /proc/self/exe you can use --hard-links flag -./sharun lib4bin --hard-links --with-sharun --dst-dir test /bin/bash +./sharun lib4bin --hard-links --dst-dir test /bin/bash # this will create hard links from 'test/sharun' in the 'test/bin' directory # now you can move 'test' dir to other linux system and run binaries from the 'bin' dir diff --git a/lib4bin b/lib4bin index c13333d..c0ec1ff 100755 --- a/lib4bin +++ b/lib4bin @@ -33,22 +33,22 @@ PATCH_INTERPRETER=${PATCH_INTERPRETER:=0} GIT_SHARUN_RELEASES='https://github.com/VHSgunzo/sharun/releases' usage() { - echo -e "[ Usage ]: lib4bin [options] /path/executable + echo -e "[ Usage ]: lib4bin [OPTIONS] /path/executable -- [STRACE CMD ARGS] [ Options ]: - -s, --strip Strip binaries and libraries (env: STRIP=1) - -v, --verbose Verbose mode (env: VERBOSE=1) + -a, --any-executable Pack any executable (env: ANY_EXECUTABLE=1) -d, --dst-dir '/path' Destination directory (env: DST_DIR=/path) - -n, --not-one-dir Separate directories for each executable (env: ONE_DIR=0) - -l, --libs-only Pack only libraries (env: LIBS_ONLY=1) - -p, --hard-links Create hard links to sharun (env: HARD_LINKS=1) - -r, --patch-rpath Patch RPATH to a relative path (env: PATCH_RPATH=1) + -e, --strace-mode Use strace for get libs (env: STRACE_MODE=1, STRACE_TIME=5) -g, --gen-lib-path Generate a lib.path file (env: GEN_LIB_PATH=1) - -a, --any-executable Pack any executable (env: ANY_EXECUTABLE=1) + -h, --help Show this message -i, --patch-interpreter Patch INTERPRETER to a relative path (env: PATCH_INTERPRETER=1) + -l, --libs-only Pack only libraries (env: LIBS_ONLY=1) + -n, --not-one-dir Separate directories for each executable (env: ONE_DIR=0) + -p, --hard-links Pack sharun and create hard links (env: HARD_LINKS=1) -q, --quiet-mode Show only errors (env: QUIET_MODE=1) - -e, --strace-mode Use strace for get libs (env: STRACE_MODE=1, STRACE_TIME=5) - -h, --help Show this message + -r, --patch-rpath Patch RPATH to a relative path (env: PATCH_RPATH=1) + -s, --strip Strip binaries and libraries (env: STRIP=1) + -v, --verbose Verbose mode (env: VERBOSE=1) -w, --with-sharun Pack sharun from PATH or env or download (env: WITH_SHARUN=1, SHARUN=/path|URL, SHARUN_URL=URL, UPX_SHARUN=1)" exit 1 @@ -122,7 +122,7 @@ get_libs() { if [ "$STRACE_MODE" == 1 ] then local libs_file="/tmp/libs.$$" - strace -f -e trace=openat --always-show-pid -o "$libs_file" "$1" &>/dev/null & + strace -f -e trace=openat --always-show-pid -o "$libs_file" "$1" "${STRACE_CMD_ARGS[@]}" &>/dev/null & sleep $STRACE_TIME local pids="$(cut -d ' ' -f1<"$libs_file"|sort -u)" kill $pids 2>/dev/null @@ -308,6 +308,13 @@ while [[ "$#" -gt 0 ]]; do esac done +if [ "$2" == '--' ] + then + STRACE_MODE=1 + BINARY_LIST=("$1"); shift 2 + STRACE_CMD_ARGS=("$@") +fi + check_deps if [ "$VERBOSE" == 1 ] @@ -468,30 +475,36 @@ for binary in "${BINARY_LIST[@]}" lib_src_name="$(basename "$lib_src_pth")" grep -qE '/lib32|/i386-linux-gnu'<<<"$lib_src_dirname_pth" && \ lib_dir="lib32"||lib_dir="lib" - lib_dst_dir_pth="${dst_dir_pth}/${lib_dir}$(sed 's|^/usr||;s|^/lib64||;s|^/lib32||;s|^/lib||;s|^/i386-linux-gnu||;s|^/x86_64-linux-gnu||'<<<"$lib_src_dirname_pth")" + lib_dst_dir_pth="${dst_dir_pth}/${lib_dir}$(sed 's|^/usr||;s|^/opt||;s|^/lib64||;s|^/lib32||;s|^/lib||;s|^/i386-linux-gnu||;s|^/x86_64-linux-gnu||'<<<"$lib_src_dirname_pth")" lib_dst_pth="$lib_dst_dir_pth/$lib_src_name" - try_mkdir "$lib_dst_dir_pth" - if [[ "$HARD_LINKS" == 1 && ! -L "${dst_dir}/${lib_dir}" ]] + if [[ "${LIBRARIES["$lib_dst_pth"]}" != 1 ]]||\ + [[ -n "$lib_src_real_pth" && "${LIBRARIES["$lib_src_real_pth"]}" != 1 ]] then - (try_cd "$dst_dir" - try_ln shared/$lib_dir $lib_dir)||exit 1 - fi - if [ -n "$lib_src_real_pth" ] - then - lib_src_real_name="$(basename "$lib_src_real_pth")" - try_cp "$lib_src_real_pth" "$lib_dst_dir_pth/$lib_src_real_name" - if [[ "$lib_src_name" != "$lib_src_real_name" && ! -L "$lib_dst_pth" ]] + try_mkdir "$lib_dst_dir_pth" + if [[ "$HARD_LINKS" == 1 && ! -L "${dst_dir}/${lib_dir}" ]] + then + (try_cd "$dst_dir" + try_ln shared/$lib_dir $lib_dir)||exit 1 + fi + if [ -n "$lib_src_real_pth" ] then - (try_cd "$lib_dst_dir_pth" - try_ln "$lib_src_real_name" "$lib_src_name")||exit 1 - fi - else - try_cp "$lib_src_pth" "$lib_dst_dir_pth/$lib_src_name" - fi - repath_needed_libs "$lib_dst_pth" - try_strip "$lib_dst_pth" - if [[ ! "$lib_dst_pth" =~ "$INTERPRETER" ]] - then try_set_rpath "$lib_dst_pth" + lib_src_real_name="$(basename "$lib_src_real_pth")" + try_cp "$lib_src_real_pth" "$lib_dst_dir_pth/$lib_src_real_name" + if [[ "$lib_src_name" != "$lib_src_real_name" && ! -L "$lib_dst_pth" ]] + then + (try_cd "$lib_dst_dir_pth" + try_ln "$lib_src_real_name" "$lib_src_name")||exit 1 + fi + LIBRARIES["$lib_src_real_pth"]=1 + else + try_cp "$lib_src_pth" "$lib_dst_dir_pth/$lib_src_name" + fi + repath_needed_libs "$lib_dst_pth" + try_strip "$lib_dst_pth" + if [[ ! "$lib_dst_pth" =~ "$INTERPRETER" ]] + then try_set_rpath "$lib_dst_pth" + fi + LIBRARIES["$lib_dst_pth"]=1 fi LIBRARIES["$lib_src_pth"]=1 fi @@ -507,7 +520,9 @@ for binary in "${BINARY_LIST[@]}" patchelf $pvarg --set-interpreter "../$lib_dir/$INTERPRETER" "$binary_name"||exit 1 fi)||exit 1 fi - # sed -i 's|/usr|/xxx|g;s|/lib|/XXX|g;s|/etc|/EEE|g' "${dst_dir}/shared/$lib_dir/$INTERPRETER"||exit 1 + interpreter_pth="${dst_dir}/shared/$lib_dir/$INTERPRETER" + info_msg "$YELLOW[ PATCH INTERPRETER ]: $BLUE[$interpreter_pth]" + sed -i 's|/usr|/xxx|g;s|/lib|/XXX|g;s|/etc|/EEE|g' "$interpreter_pth"||exit 1 fi info_msg "[ DONE ]" binary_number=$(( $binary_number + 1 ))