I'm wondering if it's possible to use Javascript in a web browser (most likely IE) to retrieve a list of currently running processes?
I'm not trying to start any processes or close them or anything like that. Just a list that I can check through then say for example do something else if a certain process is running.
No, you cannot get any information about OS processes from browser-based javascript running at normal privileges.
The browser javascript environment is very carefully protected and isolated from your system for privacy and security reasons. If one could do what you were just asking for then any web page on the internet could see exactly what programs you were running and could send that info back to their own servers.
If you are willing to loosen up your security settings, some versions of IE contain some ability to access OS information (see here for an example), but you should realize that if you do loosen up your security settings, then unknown web pages may be able to access this info or take actions in your OS also. Other browsers don't even contain this capability for regular web pages. With only one browser supporting this and only when security restrictions are relaxed, this is not a general purpose browser capability in any way.
Here is JSP page-
<html>
<head>
<title>Find running processes</title>
<script type="text/jscript">
function getProcessList()
{
var procs = GetObject("WinMgmts:").InstancesOf("Win32_Process");
var mainRes = "";
procEnum = new Enumerator(procs);
for ( ; !procEnum.atEnd(); procEnum.moveNext())
{
var proc = procEnum.item();
mainRes += proc.Name + ": " + proc.ProcessID + "\n";
}
return mainRes;
}
function getSysRunningApps()
{
var oOutput = document.getElementById("processDisplay");
oOutput.value = "";
oOutput.value = getProcessList();
}
</script>
</head>
<body bgcolor="#FFFFFF">
<input type="button" value="Show Processes" onclick="getSysRunningApps();"><br>
<p id="processDisplay" cols="30" rows="40"></p>
</body>
</html>
Absolutely not, that's well beyond what the Javascript sandbox should be able to do.
Yes, you can! The following approach targets only MSIE and may raise security warnings.
When executed under MSIE, the following code lists all Windows processes in the browser window and shows a javascript alert if McAfee is running:
<html>
<body>
<div id="list"></div>
</body>
<script>
// create a shell object and exec handle
var shell = new ActiveXObject('WScript.Shell');
var handle = shell.Exec("tasklist.exe");
// loop through the output of tasklist.exe
while (!handle.StdOut.AtEndOfStream) {
// grab a line of text
var p = handle.StdOut.ReadLine();
document.getElementById("list").innerHTML+=p+"<br>"; // for debugging
// split on space
p = p.split(' ');
if (p[0]=='mcshield.exe') {
alert("McAfee detected");
}
} // end :: while
// clean up
handle = null;
shell=null;
</script>
</html>
Credit: inspired by https://stackoverflow.com/a/6834585/698168
This code was tested under the following browsers:
If you got a JavaScript error Automation server can't create object
when creating the ActiveXObject
, you may need to set MSIE's Security option Initialize and script ActiveX controls not marked as safe for scripting
to either Prompt
or Enable
.
Under Firefox, you should use something based on XPCOM's nsIProcess.
Note that tasklist.exe
is not available under all Windows version: AFAIK it's available since Windows XP Pro.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With