使用Perl从SID获取帐户名



在我的Windows 10机器上,我使用Win32::LookupAccountSID()按帐户获取SID。在cmd.exe中,我运行:

whoami /user

我从输出中获取SID,并将其用于下面的$sid变量,但打印的帐户是空的,知道脚本出了什么问题吗?

use strict;
use warnings;
use Win32;
my $account;
my $domain;
my $sidtype;
my $sid = 'S-1-5-21-1994326832-1066739575-5522801-113721';
Win32::LookupAccountSID(undef,$sid,$account,$domain,$sidtype);
print $account;

在调用Win32::LookupAccountSID()之前,需要将SID转换为二进制格式。以下是使用Win32::Security::SID模块转换为二进制格式的示例:

use strict;
use warnings;
use Win32;
use Win32::Security::SID;
use Data::Dumper qw(Dumper);
{
my $system = undef;
my $account;
my $domain;
my $sidtype;
my $stringsid = 'S-1-5-21-1768581528-3487803020-3219343602-1001';
my $sid = Win32::Security::SID::ConvertStringSidToSid($stringsid);
Win32::LookupAccountSID($system, $sid, $account, $domain, $sidtype);
print Dumper({account => $account, domain => $domain, sidtype => $sidtype});
}

输出

$VAR1 = {
'domain' => 'DESKTOP-43CR0B8',
'sidtype' => 1,
'account' => 'hakon'
};

最新更新