⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 security.java

📁 用于Java 组件和通过Windows COM 对象或Windows DLL 来公开的组件之间的互操作
💻 JAVA
字号:
//******************************************************************
// Released under the DevelopMentor OpenSource Software License.
// Please consult the LICENSE file in the project root directory,
// or at http://www.develop.com for details before using this
// software.
//******************************************************************

package com.develop.jawin.win32;

import com.develop.jawin.*;
import com.develop.jawin.marshal.*;
import com.develop.io.*;
import com.develop.util.*;
import java.io.*;
import java.util.*;

/**
 * Except where otherwise noted, the functions in this module correspond
 * to similarly-named functions in the Windows API
 */

  public class Security implements SecurityConstants, MarshalConstants {
    

    public static int OpenProcessToken(int ProcessHandle, int DesiredAccess) 
      throws COMException, IOException
    {
      FuncPtr fp = new FuncPtr("ADVAPI32.DLL", "OpenProcessToken");
      NakedByteStream nbs = new NakedByteStream();
      LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
      //top level args
      leos.writeInt(ProcessHandle);
      leos.writeInt(DesiredAccess);
      //nested args
      byte[] res = GenericStub.win32Invoke(fp.getPeer(), 
					   "IIA:T3:lln4", 
					   12, 12,
					   nbs.getInternalBuffer(), 
					   null);
      return StructConverter.bytesIntoInt(res, 0);
    }
        
    public static byte[] GetTokenInformation(int TokenHandle, 
					     int TokenEnumerationClass)
      throws IOException, COMException                                            
    {
      FuncPtr fp = new FuncPtr("ADVAPI32.DLL", "GetTokenInformation");
      NakedByteStream nbs = new NakedByteStream();
      LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
      //top level args
      leos.writeInt(TokenHandle);
      leos.writeInt(TokenEnumerationClass);
      byte[] sizeRes = GenericStub.win32Invoke(fp.getPeer(),
					       "IIkkA::L16n4",
					       20, 20,
					       nbs.getInternalBuffer(),
					       null);
      int size = StructConverter.bytesIntoInt(sizeRes, 0);
      leos.writeInt(size);
      byte[] res = GenericStub.win32Invoke(fp.getPeer(),
					   "IIM" + size + "IA:T1:L8n" + size,
					   20, 20,
					   nbs.getInternalBuffer(),
					   null);
      return res;
    }
    
    public static byte[] GetTokenUserSid(int TokenHandle) 
      throws IOException, COMException
    {
      byte[] info = GetTokenInformation(TokenHandle, TokenUser);
      //chop off pointer and attribs
      byte[] res = new byte[info.length - 8];
      System.arraycopy(info, 8, res, 0, res.length);
      return res; 
    }
    
    public static byte[] GetSidIdentifierAuthority(byte[] sid) 
      throws COMException, IOException
    {
      FuncPtr fp = new FuncPtr("ADVAPI32.DLL", "GetSidIdentifierAuthority");
      return SharedStubs.invokeP_S(sid, 6, fp.getPeer(), CHECK_NULL);
    }
    
    public static int GetSidSubAuthorityCount(byte[] sid)
      throws COMException, IOException
    {
      FuncPtr fp = new FuncPtr("ADVAPI32.DLL", "GetSidSubAuthorityCount");
      byte[] res = SharedStubs.invokeP_S(sid, 1, fp.getPeer(), CHECK_NULL);
      return res[0];
    }

    public static int GetSidSubAuthority(byte[] sid, int subAuthority)
      throws COMException, IOException
    {
      FuncPtr fp = new FuncPtr("ADVAPI32.DLL", "GetSidSubAuthority");
      byte[] res = SharedStubs.invokePI_S(sid, subAuthority, 4, fp.getPeer(), CHECK_NULL);
      return StructConverter.bytesIntoInt(res, 0);
    }
    
    /**
     * Helper function ported from 
     * <a href="http://www.develop.com/books/pws">
     * Keith Brown's Programming Windows Security</a>
     */
    public static String SidToString(byte[] sid) 
      throws COMException, IOException
    {
      byte[] sia = GetSidIdentifierAuthority(sid);
      StringBuffer sb = new StringBuffer();
      String authority = "(hex)";
      if (0 == (sia[0] & sia[1]))  
        {
	  authority = Integer.toString(StructConverter.BEBytesIntoInt(sia, 2));
        } else 
	  {
            for (int n=0; n<6; n++) {
	      authority += (Integer.toHexString(sia[6]) + ".");
            }
	  }
      sb.append("S-").append(sid[0]).append("-").append(authority);
      int subAuthorities = GetSidSubAuthorityCount(sid);
      for (int n=0; n<subAuthorities; n++) 
        {
	  sb.append("-").append(GetSidSubAuthority(sid, n));
        }
      return sb.toString();
    }
  }


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -