Code: Select all
public class Test {
public static void main(String[] args){
test();
}
private static void test() {
VirtualBoxManager vboxManager = VirtualBoxManager.createInstance(null);
try {
vboxManager.connect("hxxp://localhost:18083", null, null);
try {
ISession session = vboxManager.getSessionObject();
IMachine vm = vboxManager.getVBox().findMachine("test");
vm.launchVMProcess(session, "headless", null).waitForCompletion(-1);
IConsole console = session.getConsole();
try {
IGuestSession guestSession =
console.getGuest().createSession("<login>","<password>", null, null);
try {
guestSession.waitFor(1L, 30 * 1000L);
if (!guestSession.getStatus().equals(GuestSessionStatus.Started)) {
throw new RuntimeException("Guest session did not start after 30 sec");
}
for (;;) {
try {
guestSession.processCreate(
"C:\\Windows\\System32\\tasklist.exe",
null,
null,
null,
0L
).waitFor((long) ProcessWaitForFlag.Start.value(), 0L);
break;
} catch (VBoxException e) {
if (!e.toString().contains("The guest execution service is not ready (yet)")) {
throw e;
}
}
Thread.sleep(1000);
}
for (;;) {
String processes = execute(guestSession, "C:\\Windows\\System32\\tasklist.exe", null);
if (processes.contains("VBoxTray.exe")) {
break;
}
Thread.sleep(1000);
}
List<String> args = new ArrayList <>();
//args.add("C:\\Windows\\System32\\tasklist.exe");
//args.add(null);
args.add("/FO LIST");
System.out.println(execute(guestSession, "C:\\Windows\\System32\\tasklist.exe", args));
} finally {
guestSession.close();
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
System.out.println("Machine unlock");
session.unlockMachine();
}
} finally {
vboxManager.disconnect();
System.out.println("Disconnected");
}
} finally {
vboxManager.cleanup();
System.out.println("Closing");
}
}
private static String execute(IGuestSession session, String file, List<String> args) {
IGuestProcess process = session.processCreate(
file,
args,
null,
Collections.singletonList(ProcessCreateFlag.WaitForStdOut),
0L
);
IEventSource es = process.getEventSource();
IEventListener el = es.createListener();
es.registerListener(el, Collections.singletonList(VBoxEventType.Any), false);
StringBuilder builder = new StringBuilder();
try {
ProcessWaitResult pwr = process.waitFor((long) ProcessWaitForFlag.Start.value(), 30 * 1000L);
boolean keepLooping;
do {
IEvent ev = es.getEvent(el, 200);
if (ev != null) {
es.eventProcessed(el, ev);
}
byte[] stdOut = process.read(1L, 1024L * 1024L, 0L);
builder.append(new String(stdOut, "cp866"));
keepLooping = !process.getStatus().toString().contains("Terminated");
} while (keepLooping);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} finally {
es.unregisterListener(el);
if (!process.getStatus().toString().contains("Terminated")) {
process.terminate();
}
}
return builder.toString();
}
}
Code: Select all
Connected to the target VM, address: '127.0.0.1:53656', transport: 'socket'
Имя образа PID Имя сессии № сеанса Память
========================= ======== ================ =========== ============
System Idle Process 0 Services 0 4 КБ
System 4 Services 0 268 КБ
smss.exe 264 Services 0 916 КБ
csrss.exe 352 Services 0 3 644 КБ
wininit.exe 408 Services 0 3 464 КБ
csrss.exe 416 Console 1 13 876 КБ
winlogon.exe 460 Console 1 15 808 КБ
services.exe 500 Services 0 5 448 КБ
lsass.exe 508 Services 0 7 880 КБ
svchost.exe 580 Services 0 9 596 КБ
svchost.exe 624 Services 0 5 756 КБ
dwm.exe 716 Console 1 43 452 КБ
VBoxService.exe 724 Services 0 6 480 КБ
svchost.exe 820 Services 0 15 180 КБ
svchost.exe 872 Services 0 27 200 КБ
svchost.exe 924 Services 0 10 764 КБ
svchost.exe 972 Services 0 9 148 КБ
svchost.exe 276 Services 0 10 636 КБ
spoolsv.exe 952 Services 0 3 844 КБ
svchost.exe 1032 Services 0 16 960 КБ
dasHost.exe 1248 Services 0 3 664 КБ
MsMpEng.exe 1328 Services 0 69 984 КБ
svchost.exe 1700 Services 0 5 308 КБ
sppsvc.exe 1940 Services 0 13 928 КБ
WmiPrvSE.exe 1948 Services 0 6 292 КБ
svchost.exe 2040 Services 0 4 512 КБ
taskhostex.exe 1212 Console 1 8 028 КБ
userinit.exe 1308 Console 1 3 368 КБ
explorer.exe 1796 Console 1 74 572 КБ
SppExtComObj.Exe 2476 Services 0 5 892 КБ
ThumbnailExtractionHost.e 2540 Console 1 4 504 КБ
SearchIndexer.exe 2628 Services 0 8 700 КБ
mobsync.exe 2948 Console 1 7 924 КБ
VBoxTray.exe 1224 Console 1 6 332 КБ
tasklist.exe 828 Console 1 5 352 КБ
conhost.exe 1880 Console 1 4 968 КБ
Machine unlock
Disconnected
Closing
Disconnected from the target VM, address: '127.0.0.1:53656', transport: 'socket'
Process finished with exit code 0
1) args.add("C:\\Windows\\System32\\tasklist.exe");
args.add("/FO LIST");
2) args.add(null);
args.add("/FO LIST");
3) args.add("/FO LIST");
But result is same.
The output format of "C:\Windows\System32\tasklist.exe /FO LIST" should be the following:
Code: Select all
Имя образа: System Idle Process
PID: 0
Имя сессии: Services
№ сеанса: 0
Память: 24 КБ
Имя образа: System
PID: 4
Имя сессии: Services
№ сеанса: 0
Память: 1 692 КБ
VirtualBox version 5.2.16
VirtualBoxSDK version 5.2.16
VirtualBox Extension Pack version 5.2.16
VirtualBox Guest Edditions version 5.2.16 inside guest system
Connection type Webservice
Java 8
Guest system Windows 8.1 x64
Host system Windows 7 x64