如何在Debian wheezy中启用suidperl?

时间:2014-02-09 18:37:55

标签: perl debian suid

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

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

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

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

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

但我不认为这种情况正在发生。

任何人都可以帮我解决这个问题吗?我正在使用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");
}

1 个答案:

答案 0 :(得分:0)

对你来说可能为时已晚,但我遇到了同样的问题并使用了以下简单的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不再是一个选项,它在perl 5.12中被删除而没有替换(参见perl5120delta,即http://search.cpan.org/~shay/perl-5.20.2/pod/perl5120delta.pod)。