Is this use of System.Security.Principal.WindowsIdentity reasonably secure?

前端 未结 1 1376
温柔的废话
温柔的废话 2020-12-15 07:30

Is System.Security.Principal.WindowsIdentity reasonably secure from being hacked such that an instance I get from Thread.CurrentPrincipal\'s Identity o

相关标签:
1条回答
  • 2020-12-15 08:15

    You can't trust the one from Thread.CurrentPrincipal, no. There's nothing to stop code running in full trust from spoofing it.

    I was able to spoof it in my environment like this:

    var admin = new WindowsIdentity(@"Administrator");
    var princ = new WindowsPrincipal(admin);
    System.Threading.Thread.CurrentPrincipal = princ;
    

    ...before invoking your code. On my machine, the created WindowsIdentity object has IsAuthenticated as true and IsAnonymous false, and so, of course, your code extracts my domain administrator's SID.

    That doesn't work in all environments, but this should, provided that the running code has enough permissions to use reflection:

    var ident = WindowsIdentity.GetCurrent();
    Thread.CurrentPrincipal = new WindowsPrincipal(ident);
    var userSid = ident.User;
    
    var fakeSid = new SecurityIdentifier("S-1-3-0");
    
    typeof (WindowsIdentity).GetField("m_user",
      BindingFlags.Instance | BindingFlags.NonPublic).SetValue(ident, fakeSid);
    

    (Again, done before calling your code.)


    Basically, there's nothing to stop two pieces of code running under Full Trust within the same process from lying to each other.

    0 讨论(0)
提交回复
热议问题