我不是新手。我是一名专业人士,向其他专业人士寻求快速解决常见问题的方法,我知道是什么chmod -R
。
这是对我的问题立即给予-1评级的回应,对于那些可能对我的调查的严肃性不屑一顾或否认其有效性的人来说。
在我的桌面和以 root 身份运行 Kali linux 的远程服务器之间试验 sshfs(Kali 的默认设置)。设法破解系统上的所有文件和文件夹权限/所有权。我已经设法修复了一些,chmod -R 0755
在适当的地方用一个简单的,但注意到很多仍然没有修复。
想知道是否有 bash 脚本或其他脚本或程序可以帮助恢复正确的所有者和权限?
我找到了一个类似的脚本,但它主要用于修复主目录。
脚本,如下:
#!/bin/bash
read -r -p "Correct file and folder permissions? [y/N] " chse
if [[ "$chse" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
echo "Processing ..."
find -H $(pwd) -type d -exec chmod 0755 {} \;
# set dirs to 755
find -H $(pwd) -type f \( -iname '*.so.*' -o -iname '*.so' \) -exec chmod 0644 {} \;
# libs
IFS=$'\n'
for value in $(find -H $(pwd) -type f ! \( -iname '*.so.*' -o -iname '*.so' -o -iname '*.bak' \) -printf '%p\n'); do
tstbin=$(readelf -l "$value" 2>/dev/null | grep -Pio 'executable|shared')
if [ -z "$tstbin" ]; then
tstbat=$(cat "$value" | head -c2 | grep -io '#!')
if [ -n "$tstbat" ]; then
perm=$(stat -c '%a' "$value")
if [ "$perm" != "755" ]; then
chmod 755 $value
echo "Set script 755 $value"
# set batch to 755
fi
else
perm=$(stat -c '%a' "$value")
if [ "$perm" != "644" ]; then
chmod 644 $value
echo "Set regular 644 $value"
# set regular files to 644
fi
fi
# above aren't elf binary
else
perm=$(stat -c '%a' "$value")
if [ "$perm" != "755" ]; then
chmod 755 $value
echo "Set binary 755 $value"
# set elf binaries to 755
fi
fi
done
unset IFS
# process linux permissions for files and folders
else
echo "Aborted."
fi
有没有人发现任何更重要的东西可以修复文件系统的权限和所有权?
--> Update: <--
没有找到理想的解决方案,我已着手修改上述脚本并使其有助于我想要的解决方案:
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# ====================================================================
# --> Documentation <--
# ---------------------
#
# 0755 21 root root .
# 0755 21 root root ..
# 0755 2 root root bin
# 0755 4 root root boot
# 0755 15 root root dev
# 0755 53 root root etc
# 0755 4 root root home
# 0755 7 root root lib
# 0700 2 root root lost+found
# 0755 6 root root media
# 0755 2 root root mnt
# 0755 4 root root opt
# dr-xr-xr-x 87 root root proc # Not touching this.
# 0744 8 root root root
# 0755 2 root root sbin
# 0755 3 root root share
# 0755 4 root root srv
# 0755 12 root root sys
# 1777 7 root root tmp
# 0755 12 root root usr
# 0755 13 root root var
#
# ========================================================================
read -r -p "Correct file and folder permissions? [y/N] " chse
if [[ "$chse" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
echo "Processing ..."
#################
# Special Cases #
#################
SDIR=("/lost+found" "/root" "/tmp")
for sd in ${SDIR[-1]}; do
perm=$(stat -c '%a' "$sd")
user=$(stat -c '%U' "$sd")
group=$(stat -c '%G' "$sd")
if [ $sd = "/tmp" ]; then
if [ "$perm" != 1777 ]; then
chmod 1777 $sd
echo "Set directory to 177 $sd"
fi
elif [ $sd = "/lost+found" ]; then
if [ "$perm" != 0700 ]; then
chmod 0700 $sd
echo "Set directory to 0700 $sd"
fi
elif [ $sd = "/root" ]; then
if [ "$perm" != 744 ];then
chmod 744 $sd
echo "Set directory to 744 $sd"
fi
else
echo "Abort!"
fi
# Do change in ownership
if [ "$user" != root ]; then
chown root $sd
echo "Set user to root $sd"
fi
if [ "$group" != root ]; then
chgrp root $sd
echo "Set group to root $sd"
fi
done
###############
# Directories #
###############
DIR=("/bin" "/boot" "/dev" "/etc" "/home" "/lib" "/media" "/mnt" "/opt" "/sbin" "/share" "/srv" "/sys" "/usr" "/var")
for pd in ${DIR[-1]}; do
perm=$(stat -c '%a' "$pd")
user=$(stat -c '%U' "$pd")
group=$(stat -c '%G' "$pd")
if [ "$perm" != 755 ]; then
chmod 755 $pd
echo "Set directory to 755 $pd"
fi
if [ "$user" != root ]; then
chown root $pd
echo "Set user to root $pd"
fi
if [ "$group" != root ]; then
chgrp root $pd
echo "Set group to root $pd"
fi
############################
# Subdirectories and files #
############################
# chmod directories to 755
find -H $pd -type d -exec chmod 0755 {} \;
# Check library files
find -H $pd -type f \( -iname '*.so.*' -o -iname '*.so' \) -exec chmod 0644 {} \;
done
#------#
# libs #
#------#
# Assign Variables
LIBFILES=$(find -H "$(pwd)" -type f ! \( -iname '*.so.*' -o -iname '*.so' -o -iname '*.bak' \) -printf '%p\n')
# Now do the hustle
for PLF in $LIBFILES; do
tstbin=$(readelf -l "$PLF" 2>/dev/null | grep -Pio 'executable|shared')
if [ -z "$tstbin" ]; then
tstbat=$(cat "$PLF" | head -c2 | grep -io '#!')
if [ -n "$tstbat" ]; then
perm=$(stat -c '%a' "$PLF")
if [ "$perm" != "755" ]; then
chmod 755 $PLF
echo "Set script 755 $PLF"
# set batch to 755
fi
else
perm=$(stat -c '%a' "$PLF")
if [ "$perm" != "644" ]; then
chmod 644 $PLF
echo "Set regular 644 $PLF"
# set regular files to 644
fi
fi
# above aren't elf binary
else
perm=$(stat -c '%a' "$PLF")
if [ "$perm" != "755" ]; then
chmod 755 $PLF
echo "Set binary 755 $PLF"
# set elf binaries to 755
fi
fi
done
unset IFS
# process linux permissions for files and folders
else
# When shit goes pear shaped
echo "Aborted."
fi
还有其他方法可以做到这一点,也有更好的方法来编写代码。但是,它现在有效。
--->Yet another update<---
我修复了脚本中的一个粗心错误,正确地重新定位了以前由于脚本结构而无法访问的几个变量的位置。