添加新的 sudo 侮辱语句

添加新的 sudo 侮辱语句

我刚刚又读了一次那个帖子: sudo 的辱骂存储在哪里?

但我只有美丽的关于这些侮辱的问题:

我们可以通过添加其他文件(而不是修改里面的标题plugins/sudoers)来添加新的吗?

当前的 sudo 侮辱文件是:

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

答案1

不,您不能简单地将侮辱性言论“添加”到目录中并期望它们起作用。添加新侮辱性言论的唯一方法是重新编译,sudo以便在编译时包含这些侮辱性言论。

您的问题的链接(sudo 的辱骂存储在哪里?)解释了这些文件是什么:

文件 insults.h 包含编译器指令,指示将上述哪些侮辱性内容包含在已编译的内核中。事实上,您可以创建自己的侮辱性内容文件,将名称添加到 insults.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

相关内容