SHARE
TWEET

TestOshiRemote

suraj1291993 Aug 17th, 2018 (edited) 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class TestOshiRemote {
  2.     public static void main(String[] args) throws Exception {
  3.         String machineName = "machine";
  4.         String userNameWithDomain = "\\administrator";
  5.         String pass = "Password";
  6.  
  7.         String namespace = "\\\\" + machineName + "\\ROOT\\CIMV2";
  8.  
  9.         BSTR username = OleAuto.INSTANCE.SysAllocString(userNameWithDomain);
  10.         BSTR password = OleAuto.INSTANCE.SysAllocString(pass);
  11.  
  12.         BSTR WQL = OleAuto.INSTANCE.SysAllocString("WQL");
  13.  
  14.         HRESULT hres = null;
  15.         // Step 1: --------------------------------------------------
  16.         // Initialize COM. ------------------------------------------
  17.         hres = Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_MULTITHREADED);
  18.         switch (hres.intValue()) {
  19.             // Successful local initialization
  20.             case COMUtils.S_OK:
  21.                 break;
  22.             // COM was already initialized
  23.             case COMUtils.S_FALSE:
  24.             case WinError.RPC_E_CHANGED_MODE:
  25.                 break;
  26.             // Any other results is an error
  27.             default:
  28.                 throw new Wbemcli.WbemcliException("Failed to initialize COM library.", hres.intValue());
  29.         }
  30.  
  31.         // Step 2: --------------------------------------------------
  32.         // Set general COM security levels --------------------------
  33.         hres = Ole32.INSTANCE.CoInitializeSecurity(null, -1, null, null, Ole32.RPC_C_AUTHN_LEVEL_DEFAULT, Ole32.RPC_C_IMP_LEVEL_IMPERSONATE, null, Ole32.EOAC_NONE, null);
  34.         // If security already initialized we get RPC_E_TOO_LATE
  35.         // This can be safely ignored
  36.         if (COMUtils.FAILED(hres) && hres.intValue() != WinError.RPC_E_TOO_LATE) {
  37.             Ole32.INSTANCE.CoUninitialize();
  38.             throw new Wbemcli.WbemcliException("Failed to initialize security.", hres.intValue());
  39.         }
  40.  
  41.         PointerByReference pSvc = new PointerByReference();
  42.         // Step 3: ---------------------------------------------------
  43.         // Obtain the initial locator to WMI -------------------------
  44.         IWbemLocator loc = IWbemLocator.create();
  45.  
  46.         // Step 4: -----------------------------------------------------
  47.         // Connect to WMI through the IWbemLocator::ConnectServer method
  48.         // Connect to the namespace with the current user and obtain pointer
  49.         // pSvc to make IWbemServices calls.
  50.         BSTR namespaceStr = OleAuto.INSTANCE.SysAllocString(namespace);
  51.         hres = loc.ConnectServer(namespaceStr, username, password, null, 0, null, null, pSvc);
  52.         OleAuto.INSTANCE.SysFreeString(namespaceStr);
  53.         // Release the locator. If successful, pSvc contains connection
  54.         // information
  55.         loc.Release();
  56.         if (COMUtils.FAILED(hres)) {
  57.             throw new Wbemcli.WbemcliException(String.format("Could not connect to namespace %s.", namespace), hres.intValue());
  58.         }
  59.  
  60.         String user = userNameWithDomain.substring(userNameWithDomain.indexOf("\\") + 1);
  61.         String domainName = userNameWithDomain.substring(0, userNameWithDomain.indexOf("\\"));
  62.         COAUTHIDENTITY auth = COAUTHIDENTITY.newAuth(user, domainName, pass);
  63.         // http://chert.cs.ksu.edu/students/sam/java_com2.htm
  64.         // Step 5: --------------------------------------------------
  65.         // Set security levels on the proxy -------------------------
  66.  
  67.         // IntByReference intByReference = new IntByReference(-1);
  68.         // PointerByReference pointerByReference = new PointerByReference(intByReference.getPointer());
  69.  
  70.         // hres = Ole32.INSTANCE.CoSetProxyBlanket(pSvc.getValue(), Ole32.RPC_C_AUTHN_WINNT, Ole32.RPC_C_AUTHZ_NONE, null, Ole32.RPC_C_AUTHN_LEVEL_CALL, Ole32.RPC_C_IMP_LEVEL_IMPERSONATE, null, Ole32.EOAC_NONE);
  71.  
  72.         // Pointer pAuth = new Pointer(0);
  73.         // Pointer.nativeValue(pAuth, -1);
  74.  
  75.         // Pointer pAuth = Pointer.createConstant(-1);
  76.         Pointer pAuth = new Pointer(-1);
  77.         Pointer.nativeValue(pAuth, -1);
  78.         LPOLESTR COLE_DEFAULT_PRINCIPAL = new LPOLESTR(pAuth);
  79.         hres = Ole32.INSTANCE.CoSetProxyBlanket(pSvc.getValue(), Ole32.RPC_C_AUTHN_DEFAULT, Ole32.RPC_C_AUTHZ_DEFAULT, COLE_DEFAULT_PRINCIPAL, Ole32.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, Ole32.RPC_C_IMP_LEVEL_IMPERSONATE, auth, Ole32.EOAC_NONE);
  80.         if (COMUtils.FAILED(hres)) {
  81.             new IWbemServices(pSvc.getValue()).Release();
  82.             throw new Wbemcli.WbemcliException("Could not set proxy blanket.", hres.intValue());
  83.         }
  84.         IWbemServices svc = new IWbemServices(pSvc.getValue());
  85.  
  86.         String query = "SELECT BuildNumber,Caption,OSArchitecture,Version FROM Win32_OperatingSystem";
  87.  
  88.         PointerByReference pEnumerator = new PointerByReference();
  89.         // Step 6: --------------------------------------------------
  90.         // Use the IWbemServices pointer to make requests of WMI ----
  91.         // Send the query. The flags allow us to return immediately and begin
  92.         // enumerating in the forward direction as results come in.
  93.         BSTR queryStr = OleAuto.INSTANCE.SysAllocString(query);
  94.         hres = svc.ExecQuery(WQL, queryStr, Wbemcli.WBEM_FLAG_FORWARD_ONLY | Wbemcli.WBEM_FLAG_RETURN_IMMEDIATELY, null, pEnumerator);
  95.         OleAuto.INSTANCE.SysFreeString(queryStr);
  96.         if (COMUtils.FAILED(hres)) {
  97.             svc.Release();
  98.             throw new Wbemcli.WbemcliException(String.format("Query '%s' failed.", query), hres.intValue());
  99.         }
  100.         IEnumWbemClassObject obj = new IEnumWbemClassObject(pEnumerator.getValue());
  101.  
  102.         System.out.println("Done");
  103.     }
  104. }
  105.  
  106.  
  107.  
  108. public interface Ole32 extends oshi.jna.platform.windows.Ole32 {
  109.     Ole32 INSTANCE = Native.loadLibrary("Ole32", Ole32.class, W32APIOptions.DEFAULT_OPTIONS);
  110.  
  111.     int RPC_C_AUTHN_LEVEL_PKT_PRIVACY = 0x06;
  112.  
  113.     int RPC_C_AUTHN_DEFAULT = 0xFFFFFFFF;
  114.     int RPC_C_AUTHZ_DEFAULT = 0xffffffff;
  115.  
  116.     HRESULT CoSetProxyBlanket(Pointer pProxy, int dwAuthnSvc, int dwAuthzSvc, LPOLESTR pServerPrincName, int dwAuthnLevel, int dwImpLevel, COAUTHIDENTITY pAuthInfo, int dwCapabilities);
  117. }
  118.  
  119.  
  120. public class COAUTHIDENTITY extends Structure {
  121.     public Pointer User;
  122.     public int UserLength;
  123.     public Pointer Domain;
  124.     public int DomainLength;
  125.     public Pointer Password;
  126.     public int PasswordLength;
  127.     public int Flags;
  128.  
  129.     @Override
  130.     protected List<String> getFieldOrder() {
  131.         return Arrays.asList("User", "UserLength", "Domain", "DomainLength", "Password", "PasswordLength", "Flags");
  132.     }
  133.  
  134.     public static COAUTHIDENTITY newAuth(String uname, String domainName, String pass) {
  135.         COAUTHIDENTITY auth = new COAUTHIDENTITY();
  136.         auth.User = new Memory(Native.WCHAR_SIZE * (uname.length() + 1));
  137.         auth.User.setWideString(0, uname);
  138.         auth.UserLength = uname.length();
  139.  
  140.         auth.Password = new Memory(Native.WCHAR_SIZE * (pass.length() + 1));
  141.         auth.Password.setWideString(0, pass);
  142.         auth.PasswordLength = pass.length();
  143.  
  144.         auth.Domain = new Memory(Native.WCHAR_SIZE * (domainName.length() + 1));
  145.         auth.Domain.setWideString(0, domainName);
  146.         auth.DomainLength = domainName.length();
  147.  
  148.         auth.Flags = 1;
  149.  
  150.         return auth;
  151.     }
  152. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top