加入新的 sudo 侮辱

加入新的 sudo 侮辱

我剛剛又讀過那篇文章: sudo 的侮辱訊息儲存在哪裡?

但我只有一個美麗的關於這些侮辱的問題:

我們可以透過新增其他文件來新增文件(而不是修改內部的標頭plugins/sudoers)嗎?

目前 sudo 侮辱文件是:

  • ins_2001.h
  • ins_classic.h
  • ins_csops.h
  • ins_goons.h
  • insults.h

答案1

不,您不能簡單地將侮辱性內容「添加」到目錄中並期望它們起作用。新增侮辱的唯一方法是重新編譯,sudo以便在編譯時包含這些侮辱。

您的問題的連結(sudo 的侮辱訊息儲存在哪裡?) 解釋了這些文件是什麼:

檔案侮辱.h 包含有關將上述哪些侮辱包含在編譯核心中的編譯器指令。事實上,您可以創建自己的侮辱文件,將名稱添加到侮辱.h 並重新編譯...

但是您需要添加侮辱性內容,然後重新編譯以包含它們。然而,這樣做意味著sudo如果有安全性更新等,您必須重新編譯,並且如果您不使用包含的安全性修補程式重新編譯,這可能會導致安全性問題。

答案2

您只能透過取代相同或更長長度的現有侮辱來添加新的侮辱。

從您連結的第二個答案中可以看到這些短語:

ins_2001.h(2001太空漫遊侮辱):

/*
 * HAL insults (paraphrased) from 2001.
 */

"Just what do you think you're doing Dave?",
"It can only be attributed to human error.",

找到文件

$ grepall "Just what do you think you're doing Dave?"
Binary file /usr/lib/sudo/sudoers.so matches

好的,我們現在知道檔案名稱了。這是最容易的部分。

備份文件

首先進行備份(因為我們總是記得這樣做,對嗎?):

$ sudo cp -a /usr/lib/sudo/sudoers{.so,.so.bak}

$ ll /usr/lib/sudo/sudoers{.so,.so.bak}
-rw-r--r-- 1 root root 316768 Oct 11 06:01 /usr/lib/sudo/sudoers.so
-rw-r--r-- 1 root root 316768 Oct 11 06:01 /usr/lib/sudo/sudoers.so.bak

製作腳本

然後將我們的腳本命名為sudoinsults

#!/bin/bash

# NAME: sudoinsult
# PATH: $HOME/askubuntu/
# DESC: For: https://askubuntu.com/questions/1188779/adding-new-sudo-insults
# DATE: November 14, 2019.

# NOTE: Change sudo insults to personal favorites

# Build array of insults from disk
IFS=$'\n' Arr=( $(cat sudoinsult.txt) )

# Initialize variables
File="/usr/lib/sudo/sudoers.so"
upper=0
Spaces="                                                                     "
Spaces="$Spaces""                                                            "

[[ ${#Arr[@]} -gt 0 ]] && upper=$(( ${#Arr[@]} - 1 ))
[[ $upper -gt 0 ]] && for (( i=0; i<upper; i=i+2 )) ; do
    Search="${Arr[i]}"      # Move array indices to named variables
    Replace="${Arr[i+1]}"   #  for a simpler life.

    printf "Replacing: '%s'\n     With: '%s'\n" "$Search" "$Replace"
    if [[ "${#Search}" -lt "${#Replace}" ]] ; then
        echo "Replacement can't be longer than original"
        continue
    elif [[ "${#Search}" -lt 8 ]] ; then
        echo "Original insult cannot be less than 8 characters"
        continue
    elif [[ "${#Search}" -gt "${#Spaces}" ]] ; then
        echo "Original insult cannot be longer than ${#Spaces} characters"
        continue
    elif [[ "${#Replace}" -lt 1 ]] ; then
        echo "Replacement insult cannot be less than 1 character"
        continue
    elif ! grep "$Search" "$File" >/dev/null ; then
        echo "Search insult not found in $File"
        continue
    fi

    # Pad replacement with spaces as needed.
    ReplaceS="$Replace${Spaces:0:$((${#Search} - ${#Replace}))}"
    [[ "${#ReplaceS}" -ne "${#Search}" ]] && \
        { echo Internal error ReplaceS different length than Search; exit; }

# Looks wrong: https://unix.stackexchange.com/a/354493/200094
#y="${y:0:40}${forty:0:$((40 - ${#y}))}"
#echo "'${y}'"
    sed -i "s/$Search/$ReplaceS/" "$File"
    (( InsultCount++ ))

done

if [[ $upper -gt 0 ]] ; then
    echo "$InsultCount Insults replaced."
else
    echo "Insult file (sudoinsult.txt) does not exist or only has one line." >2
fi

建立資料文件

希望不需要解釋如何建立文字檔案(提示gedit:)

$ cat sudoinsult.txt

Just what do you think you're doing Dave?
Just what do you think you're doing Rick?
It can only be attributed to human error.
It can only be attributed to the beer.

運行腳本並檢查結果

$ sudo ./sudoinsult

Replacing: 'Just what do you think you're doing Dave?'
     With: 'Just what do you think you're doing Rick?'
Replacing: 'It can only be attributed to human error.'
     With: 'It can only be attributed to the beer.'
2 Insults replaced.

$ ll /usr/lib/sudo/sudoers{.so,.so.bak}

-rw-r--r-- 1 root root 316768 Nov 14 17:43 /usr/lib/sudo/sudoers.so
-rw-r--r-- 1 root root 316768 Oct 11 06:01 /usr/lib/sudo/sudoers.so.bak

$ grep "Just what do you think you're doing Rick?" /usr/lib/sudo/sudoers.so

Binary file /usr/lib/sudo/sudoers.so matches

$ grep "It can only be attributed to the beer." /usr/lib/sudo/sudoers.so

Binary file /usr/lib/sudo/sudoers.so matches

相關內容