【问题标题】:get the account name from SID using Perl使用 Perl 从 SID 获取帐户名称
【发布时间】:2020-11-12 09:25:51
【问题描述】:

在我的 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;

【问题讨论】:

  • 我认为你输入到函数中的 SID 应该是不同的。如果我使用Win32::LookupAccountName() 来获取 SID,它看起来完全不同。在我的机器上,我得到 SID=` ÿijiîªπ╧≥Hπ┐Θ Φo ¡√╪o Ç≈% ΦÖ Φo x7 σ╗╒o P≈! ΦÖ ΦÖ Φo ╪>! └9# Φ=" °% ╪>! τz┘o └9# ΦÖ Φo αÿ ╖≡o √a B■╠oh╛y└u Φ=" └9# 9 \\╫≥o Xd4 áÜ4 Φo r═ o Φo @n ╕²a αA `
  • 如果我使用上述 SID(而不是从 whoami 返回的 SID 作为 LookupAccountSID() 的输入,它可以正常工作
  • 您知道是否可以将 S-1-15-21 格式(whomai 格式)转换为您提到的格式吗?
  • 你没有look,是吗?
  • 我看,但如果它单独使用 Win32 完成它会很棒,因为它是核心的一部分。

标签: windows perl


【解决方案1】:

在调用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'
        };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 2014-08-10
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多