📄 security.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 org.jawin.donated.win32;
import java.io.IOException;
import org.jawin.COMException;
import org.jawin.FuncPtr;
import org.jawin.ReturnFlags;
import org.jawin.io.LittleEndianOutputStream;
import org.jawin.io.NakedByteStream;
import org.jawin.marshal.StructConverter;
/**
* Except where otherwise noted, the functions in this module correspond
* to similarly-named functions in the Windows API
*/
public class Security {
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 = fp.invoke("IIA:T3:lln4",
12, nbs,
null, ReturnFlags.CHECK_FALSE);
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 = fp.invoke("IIkkA::L16n4",
20, nbs,
null, ReturnFlags.CHECK_NONE); // to get the buffer size
int size = StructConverter.bytesIntoInt(sizeRes, 0);
leos.writeInt(size);
byte[] res = fp.invoke("IIM" + size + "IA:T1:L8n" + size,
20, nbs,
null, ReturnFlags.CHECK_FALSE);
return res;
}
public static byte[] GetTokenUserSid(int TokenHandle)
throws IOException, COMException
{
byte[] info = GetTokenInformation(TokenHandle, SecurityConstants.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 fp.invoke_S(sid, 6, ReturnFlags.CHECK_FALSE);
}
public static int GetSidSubAuthorityCount(byte[] sid) throws COMException, IOException {
FuncPtr fp = new FuncPtr("ADVAPI32.DLL", "GetSidSubAuthorityCount");
byte[] res = fp.invoke_S(sid, 1, ReturnFlags.CHECK_FALSE);
return res[0];
}
public static int GetSidSubAuthority(byte[] sid, int subAuthority) throws COMException, IOException {
FuncPtr fp = new FuncPtr("ADVAPI32.DLL", "GetSidSubAuthority");
byte[] res = fp.invoke_S(sid, subAuthority, 4, ReturnFlags.CHECK_FALSE);
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++) {
int unsigned = ((int)sia[n]) & 0xFF;
authority += (Integer.toHexString(unsigned) + ".").toUpperCase();
}
}
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 + -