Bash Logon สคริปต์พฤติกรรมแปลก ๆ

Oct 06 2020

ฉันกำลังสร้าง honeypot ซึ่งจะบันทึกข้อมูลผู้ใช้ทั้งหมดเมื่อเข้าสู่ระบบสำเร็จผ่าน SSH

นี่คือวิธีการทำงาน: ฉันadminตั้งค่าเชลล์เริ่มต้นของผู้ใช้ honeypot เป็น bash script:

admin:x:1001:1001::/home/admin:/home/admin/HoneyPot/spawner.sh

spawner.shสคริปต์จะไปข้างหน้าและเปิดสคริปต์และบันทึกการส่งออกโดยใช้exceptscript

#!/bin/bash
cd /home/admin/HoneyPot/template/

pwd
script -c "./script.exp" -t 2> timing.log -a output.session #start recording session, execute except script
echo "we should not get here"

นี่คือสองสามบรรทัดแรกของscript.exp:

#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Mon Oct  5 13:55:35 
# boilerplate comments and code:

set force_conservative 0  ;# set to 1 to force conservative mode even if
              ;# script wasn't run conservatively originally
if {$force_conservative} { set send_slow {1 .1} proc send {ignore arg} { sleep .1 exp_send -s -- $arg
    }
}

# start of my code

set timeout -1
spawn emulator #emulator is a simh emulator executable. not a shell script.
...
interact

เมื่อฉันเรียกใช้สคริปต์โดยใช้./template.shเป็นadminใช้bashสคริปต์จะทำงานได้ดีอย่างสมบูรณ์ อย่างไรก็ตามเมื่อฉันเข้าสู่ระบบโดยใช้suสิ่งนี้จะเกิดขึ้น:

austin@ubuntu:~$ su admin
Password: 
/home/admin/HoneyPot/template
Script started, file is output.session
/home/admin/HoneyPot/template
Script started, file is output.session
/home/admin/HoneyPot/template
Script started, file is output.session
/home/admin/HoneyPot/template
Script started, file is output.session
/home/admin/HoneyPot/template
Script started, file is output.session
/home/admin/HoneyPot/template
...

เหตุใดสคริปต์ทุบตีของฉันจึงไม่ทำงานกับเชลล์ของผู้ใช้ที่ตั้งค่าไว้ ไม่มีการเรียกซ้ำในสคริปต์ของฉันและscriptคำสั่งควรปิดกั้น!

และในกรณีที่ทุกคนกังวลเครื่องนี้ไม่มีการเชื่อมต่อเครือข่ายขาออก สามารถรับการเชื่อมต่อ SSH เท่านั้น ใช่ฉันรู้ว่าผู้ใช้สามารถแยกออกจากสิ่งนี้ได้ กำลังรันใน VM

คำตอบ

1 laktak Oct 06 2020 at 01:35

ฉันคิดว่าคุณกำลังใช้สคริปต์โดยไม่ต้องมี shebang ที่ไหนสักแห่ง

ดูคำตอบนี้ :

ถ้าฟังก์ชัน execl () ล้มเหลวเนื่องจากข้อผิดพลาดที่เทียบเท่ากับข้อผิดพลาด [ENOEXEC] ที่กำหนดไว้ในโวลุ่ม System Interfaces ของ POSIX.1-2008 เชลล์จะดำเนินการคำสั่งที่เทียบเท่ากับการเรียกเชลล์ที่เรียกด้วยชื่อพา ธ ที่เป็นผลมาจากการค้นหา เป็นตัวถูกดำเนินการตัวแรกโดยมีอาร์กิวเมนต์ที่เหลือส่งผ่านไปยังเชลล์ใหม่ยกเว้นว่าอาจตั้งค่า "$ 0" ในเชลล์ใหม่เป็นชื่อคำสั่ง หากไฟล์ปฏิบัติการไม่ใช่ไฟล์ข้อความเชลล์อาจข้ามการเรียกใช้คำสั่งนี้ ในกรณีนี้จะต้องเขียนข้อความแสดงข้อผิดพลาดและจะส่งคืนสถานะการออกเป็น 126

ในการspawner.shเพิ่มของคุณecho "$@"เพื่อรับคำแนะนำเพิ่มเติม

ตกลงดังนั้นคำตอบจึงซับซ้อนกว่านี้ แต่น่าจะช่วยได้ ในการspawner.shเพิ่มสิ่งนี้ด้านบน:

if [[ $1 == -c ]]; then exec bash "$@"
fi