1

我有一个由 root 拥有并具有 setuid 的 Perl 脚本。

在此脚本中,我正在更改作为参数传递的文件的所有者。

但是在运行这个脚本时,我得到了

chown: changing ownership of `file': Operation not permitted

有人告诉我,如果启用,则默认情况下使用 suidperl 运行带有 setuid 的脚本。

但我不认为这发生在我的案例中。

任何人都可以在这件事上帮助我吗?我正在使用 Debian wheezy。我的 Perl 版本是 5.14.2。我试过了

apt-get install perl-suid

但它没有用。

apt-cache search perl

在 Perl 中没有给我与 suid 相关的候选人。

这是我的 Perl 程序

#! /usr/bin/perl -T

use Cwd 'abs_path';

sub check_path {
  my $file = $_[0];

  $file = abs_path($file);
  if ($file =~ /^\/home\/abc\/dir1\//) {
    return 1;
  }
  else {
    return 0;
    print("You can only update permissions for files inside /home/abc/dir1/ directory\n");
  }
}

if (@ARGV == 1) {
  if (&check_path($ARGV[0]) == 1) {
    $ENV{PATH} = "/bin:/usr/bin";
    my $command = "chown abc:abc " . $ARGV[0];
    if ($command =~ /^(.*)$/) {
      $command = $1;
    }

    $result = `$command`;
  }
}
elsif ((@ARGV == 2) && ($ARGV[0] eq "-R")) {
  if (&check_path($ARGV[1]) == 1) {
    $ENV{PATH} = "/bin:/usr/bin";
    my $command = "chown -R abc:abc " . $ARGV[1];
    if ($command =~ /^(.*)$/) {
      $command = $1;
    }
    $result = `$command`;
  }
}
else {
  print("Sorry wrong syntax. Syntax: perl /home/abc/sbin/update_permission.pl [-R] file_path");
}
4

1 回答 1

-1

对你来说可能为时已晚,但我遇到了同样的问题并使用了以下简单的 C 包装器(无耻地取自http://www.blyberg.net/downloads/suid-wrapper.c):

#include <unistd.h>
#include <errno.h>

main( int argc, char ** argv, char ** envp )
{
    if( setgid(getegid()) ) perror( "setgid" );
    if( setuid(geteuid()) ) perror( "setuid" );
    envp = 0; /* blocks IFS attack on non-bash shells */
    system( "/path/to/bash/script", argv, envp );
    perror( argv[0] );
    return errno;
}

将 C 代码中的路径替换为脚本的路径,编译为

gcc -o suid-wrapper suid-wrapper.c

并设置权限

chmod 6755 suid-wrapper

suidperl ist 不再是一个选项,它已在 perl 5.12 中被删除而无需替换(参见 perl5120delta,即http://search.cpan.org/~shay/perl-5.20.2/pod/perl5120delta.pod)。

于 2015-05-04T15:20:44.327 回答