gpt4 book ai didi

ruby - 如何在 Ruby 中获得 super 用户权限?

转载 作者:数据小太阳 更新时间:2023-10-29 08:14:05 26 4
gpt4 key购买 nike

所以,我一直在研究一个 Ruby 脚本,它可以在我上学期间阻止 reddit(有用的东西)。这是代码:

require 'fileutils'

puts "-----------------------------------"
puts "Welcome to the hosts file modifier!"
puts "-----------------------------------"
puts "Option A: Use modified hosts"
puts "Option B: Use original hosts"
puts "Option C: Do nothing"
puts "Please enter your choice: "
input = gets.chomp.downcase

t = Time.now
# Time.now is used is conjunction with function 'original', in option 'b'

def modified
# This function copies the modified (redditblocking) hosts file from Documents to /etc
puts "Moving original hosts file out of /etc"
FileUtils.mv('/etc/hosts', '/Users/(usernameobscured)/Documents/OriginalHosts/hosts')
puts "Done. Now copying modified hosts to /etc"
FileUtils.cp('/Users/(usernameobscured)/Documents/ModifiedHosts/hosts', '/etc/hosts')
puts "Done"
end

def original
# This function deletes the modified hosts file from /etc (since we have a copy in Documents)
# and then moves the original hosts file back to /etc
puts "Deleting modified hosts file from /etc"
FileUtils.rm_rf('etc/hosts')
puts "Done. Now copying original hosts to /etc"
FileUtils.mv('/Users/(usernameobscured)/Documents/OriginalHosts/hosts', '/etc/hosts')
puts "Done"
end

def nothing
# This does... nothing. Literally.
puts "Doing nothing"
end

if input == 'a'
modified
end

if input == 'b'
# Here's when using Time.now becomes helpful: if the hour of the day is less than 5PM,
# then the original hosts file can't be moved back (don't wanna be on reddit during school hours!)
if t.hour > 17
original
elsif t.hour < 17
puts "Too early to use original hosts file. Come back at 5PM"
end
end

if input == 'c'
# Nothing...
nothing
end

如您所见,它将修改后的主机文件从我的文档文件夹移动到/etc。不过,根据 OS X/Unix 安全措施,我遇到的问题是我必须通过 sudo 运行脚本或以 root 身份登录。这是一个小麻烦,但是,我相信它可以在代码中修复。我怎样才能通过我的 ruby​​ 脚本获得 super 用户权限,或者暂时写入对/etc 的访问权限,这样我就可以在没有 sudo/root 的情况下简单地运行脚本?

最佳答案

根据 Unix 安全模型,如果没有某种外部干预(setuid 设置为可执行文件,以 root 用户身份运行),则无法获得 root 访问权限。否则我们就会有一个巨大的安全漏洞。

我不清楚您使用 sudorvmsudo 或反对设置脚本 setuid 的确切问题是什么(可以配置 sudo 不需要为狭义定义的命令集提供密码)。

我只是建议将主机文件的各种版本设置为可由您所在的组写入。

关于ruby - 如何在 Ruby 中获得 super 用户权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17904197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com