Hướng dẫn tạo file binary từ shell script trên linux

Hướng dẫn tạo file binary từ shell script

 

Làm việc với môi trường linux các bạn sẽ thấy có nhiều file binary đã được mã hoá dạng nhị phân lưu trữ ở​​ /bin, /sbin , /usr/bin, /usr/sbin….​​ Mình sẽ hướng dẫn thử tạo 1 file binary như nào.

Step 1 –​​ Yêu cầu

First of all, You need to install required packages for SHC compiler.

For Ubuntu, Debian and LinuxMint

sudo apt-get install libc6-dev​​ 

For CentOS, RHEL & Fedora

sudo yum install glibc-devel

Step 2 – Download and Install SHC

Download the latest source code of SHC compiler from its official webpage or using below commands and extract on your system.

cd /usr/src

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz

sudo tar xzf shc-3.8.9.tgz

Now compile the SHC source code on your system and install it using following command.

cd shc-3.8.9

make

make install

Step 3 – Create Shell Script

Let’s create a shell script as per your requirement or skip this step if you already created. For this article we have created below sample script which add the integer values given on command line parameter and show the sum of them.

vim script.sh

 

#!/bin/bash

 

total=0

 

for i in $@; do

 ​​ ​​​​ if [ ! -z "${i##[0-9]*}" ]; then

 echo "Please enter numeric only"

 exit 1

 ​​ ​​​​ fi

 ​​ ​​​​ total=$(($total + $i))

done

 

if [ $total -eq 0 ]; then

 ​​ ​​ ​​​​ echo "Plesae execute script like: $0 10 20 30"

 ​​ ​​ ​​​​ exit 0

fi

 

echo $total

Step 4 – Create Binary of Script

At this stage we have installed SHC compiler and have a shell script named script.sh. Use the following command to create binary file of your script.

shc -T -f script.sh

The above command will create two files in current directory. One will be script.sh.x.c which is in C language format of your script. Second one will be script.sh.x which will be in binary format.

Step 5 – Test Binary Script:

If you try to open binary format of script, you will see that it is not in human readable format.

Now move this script under /usr/bin directory to use from anywhere in system. Also remove .sh.x from file name. So it will be available with simple name. Also set the execute permissions to everyone

mv script.sh.x /usr/bin/script

chmod +x /usr/bin/script

Now type command ‘script’ from anywhere in system. You will see the same results as your shell script does.

script 10 20 30​​ 

60

 

Lưu ý:

Trên Ubuntu gõ lệnh

​​ apt-get install shc​​ 

là có thể gõ lệnh shc –T –f file.sh

để tạo file binary thành công.

Khi tạo 1 file binary bắt buộc có lệnh dưới​​ ở trong file (lệnh này không phải là comment!)

#!/bin/bash

 

Lệnh này là chỉ định ra /bin/bash nghĩa là dùng bash để chạy lệnh

Câu lệnh​​ 

shc -T -f script.sh

​​ sẽ tạo ra 1 file tên là script dạng binary

Ở​​ bất kì nơi nào cũng có thể gõ được lệnh script và chạy thực thi câu lệnh trong nội dung của script

 

Link tham khảo

#https://tecadmin.net/create-binary-file-from-shell-script/

chúc các bạn thành công!

BÀI VIẾT CÙNG CHUYÊN MỤC

Critical - CVE 2026 98432 - Nguy cơ tấn công từ xa vào Linux - Ubuntu, Centos, Debian
Critical - CVE 2026 98432 - Nguy cơ tấn công từ xa vào ...

Critical - CVE 2026 98432 - Nguy cơ tấn công từ xa vào Linux - Ubuntu, Centos, ...

CXMT và DRAM 4F² cho thấy Trung Quốc tiến nhanh nhưng chưa thể kết luận Mỹ thua cuộc
CXMT và DRAM 4F² cho thấy Trung Quốc tiến nhanh nhưng ...

CXMT và DRAM 4F² cho thấy Trung Quốc tiến nhanh nhưng chưa thể kết luận Mỹ thua ...

Cảnh báo NAS Synology, XPEnology bị mã hóa dữ liệu: Vì sao bị tấn công và cách xử lý?
Cảnh báo NAS Synology, XPEnology bị mã hóa dữ liệu: Vì ...

Cảnh báo NAS Synology, XPEnology bị mã hóa dữ liệu: Vì sao bị tấn công và cách ...

Cảnh báo Telegram trên iPhone: Nguy cơ bị hacker tấn công qua sticker và cách phòng tránh
Cảnh báo Telegram trên iPhone: Nguy cơ bị hacker tấn ...

Cảnh báo Telegram trên iPhone: Nguy cơ bị hacker tấn công qua sticker và cách ...

PHP2Shell làm Nhật Bản “khốn đốn”: Vì sao nhiều dịch vụ tạm dừng, còn thế giới chịu ảnh hưởng ra sao?
PHP2Shell làm Nhật Bản “khốn đốn”: Vì sao nhiều dịch ...

PHP2Shell làm Nhật Bản “khốn đốn”: Vì sao nhiều dịch vụ tạm dừng, còn thế giới ...

PHP2Shell: Hai lỗ hổng nghiêm trọng đe dọa hàng triệu website – Cách kiểm tra và khắc phục
PHP2Shell: Hai lỗ hổng nghiêm trọng đe dọa hàng triệu ...

PHP2Shell: Hai lỗ hổng nghiêm trọng đe dọa hàng triệu website – Cách kiểm tra ...