Modify the default web browser for all child processes - c#

On Windows, is there any way to modify the default browser in current application domain, but not system wide?
I'm developing an IDE using C# and I need that all child processes that try to launch the default web browser, see my IDE as the default web browser. But I need to do so in a way that it does not affect the rest of the system configuration.
Not sure if I'm explaining the issue clearly...
Anyone has any ideas how this could be done?

Actually there is no way to make it for a certain process. This value is stored in registry, and can be changed for a different user.
But in general are several options.
Run debuggee using a special user account, which has a different registry value for default browser.
Register your IDE as default browser in a reg key HKEY_CLASSES_ROOT\http\shell\open\command. If IDE could determine if the caller is not a debuggee, invoke original browser (don't forget to backup initial value).
If you use something like host process - intercept calls to RegQueryValue or other registry functions, and hijack the result for a debuggee. (if there is no host process, try to inject an interceptor). Alternatively you may intercept CreateProcess or ShellExecute.
I'm afraid that's not an option, but - you're able to register a dummy protocol (like myhttp://) and pretend it's http :) Your IDE will be a handler for it.

On which Windows are you? In windows 7 and 8 you can find the default program settings in the control panel. Just start the control panel and type 'default programs' in search box in the upper right hand corner.

Related

How Do I retrieve a URL Protocol Request in C#

I have a Windows WPF application running in the background at all times. I have registered a URL protocol in with the application called WTL:. The protocol works when the application is closed, but how do I respond to it when the application is open or running in the background? I have clients that are using WinXP, so the new protocol handling in Win8 isn't sufficient.
I figured OnNavigated or OnActivated might work, but the protocol doesn't work with those. Should I run something that is always listening to the protocol? Help.
Thanks!
OK - i had to read over this two times but now i think i know what you mean. I have had a similar task, not with WPF but in WinForms... (almost the same in that case).
I don't know what "WTL" does exactly, because i registered my own protocol manually at the application startup.
You can view the current settings by opening the registry editor and goto HKEY_CLASSES_ROOT
Beneath you can find your "protocol" (dont know yours, so for example i take "callto://")
Browse to the command-subkey HKEY_CLASSES_ROOT\callto\shell\open\command - The default entry points to your application. The value must look like:
"path_to_your_application.exe" "%1" (including the double-quotes!)
Important is the "%1"
This value represents the argument for your application instance.
Reading commandline args isn't really a complicated task in .net.
But it's getting a little bit tricky, if you allow only one instance!
For me the best approach for a single-instance application & getting those
"second instance-commandline-args" can be found under:
Codeproject: WPF Single Instance Application
Long story short: A request to your application can look like:
"callto://008291478624599"
In which the number will be filled in the args for your 2nd application startup request (the %1)

Increasing the security when using WebBrowser

I've looked through several similar questions on SO but haven't found something quite like what I need, so my question is this:
I want to take a screenshot (thumbnail) of a URL after the user provides one. I was going to use Awesomium because they provide a fairly simple solution for screengrabs. Unfortunately, Awesomium won't compile in an x64 application, and since I'm building this with ASP.NET for Windows Azure, I can't switch to x86.
So I'm left with a less-elegant solution, using Windows.Forms WebBrowser to load the url and take the screenshot (as shown here: http://www.codeproject.com/Articles/95439/Get-ASP-NET-C-2-0-Website-Thumbnail-Screenshot ).
Ugly, I know, but it works with most pages (there is the occasional white screenshot), but now I'm concerned with security.
If the user inputs a malicious URL and the WebBrowser loads it, what is to stop it from running harmful code and downloading a virus to the server where the app is hosted?
There are several services and websites that offer similar functionality, albeit with different approaches, but the core idea is the same: the site must open up the URL and render the page in order to grab the screenshot. So what kind of measures would one expect them to take to thwart viruses and malicious URLs?
The biggest threat to your application would be client script executing in your browser control (i.e. JavaScript and client-side VBScript). It appears it is not possible to disable JavaScript programmatically in the WebBrowser object:
VB.NET WebBrowser disable javascript
Disable javascript in
WinForms WebBrowser control?
Stripping <script> tags in the first question's first answer is not the way to go for security, as there are so many other ways script can get inserted.
Changing window.alert in the second answer won't work as it needs the page to load fully first, and it is possible for script to execute before then. Also, this would only stop the alert function and not prevent script code in any other way.
Changing the registry settings as suggested in this answer may be the way to go, but this appears to be the same as changing Internet Explorer settings to high security for the internet zone (or selecting custom and disabling Active Scripting). If you are always in control of the machine where your app is loaded from, then manually disabling scripting in Internet Explorer options could be a viable solution.
Most client-side internet threats such as drive-by downloads involve script in some way, so this approach will go a long way in protecting your app.
However, there are other exploits such as the Windows Metafile vulnerability that can harm a client machine.
Viewing a website in a web browser that automatically opens WMF files, in which case any potential malicious code may be automatically downloaded and opened. Internet Explorer, the default Web browser for all versions of Microsoft Windows since 1996, does this.
However, making sure your machines are patched with the latest Windows Updates will secure you against threats like these. This will leave zero-day attacks against Internet Explorer or the WebBrowser object, which you will not be able to do much about. I would suggest running your app on an isolated machine (or VM) which would then upload the screenshot to another server (e.g. via the web) which would help mitigate threats in this scenario.

Has my application been started by hand?

I am developing a Forms application that is either supposed to start on Windows boot (by registering a key in the registry) or by the actual user.
In case the user starts it by hand (i.e. clicking the executable file), I want the form to show up. In case Windows triggered the event, I want the application to be hidden (into the tray bar).
Is there a way to programmatically determine which of those cases occured?
I would suggest adding a command line parameter, something like -minimized and pass that when starting it on boot.
See this thread if you are looking for how to read the arguments.
You can start you application upon Windows startup with a certain parameter, while a user will start it without this parameter.
The easiest way to do that, that I can think about, is:
When you register to run in Windows boot, pass a parameter (some default parameter), in case when user will run it, parameter will be absent.
In this way you would be able, depending on presence or absence of a paremeter, to determine the startup option of your program.

Windows - Custom Lock Screen link to custom App?

Would anyone be able to point me to docs/api that allow me to put a custom
button or link on windows lock screen ?
I want to be able to launch a custom program from there.
Thanks.
Ralph
What do you mean on "lock screen" ?
You can just run:
rundll32 USER32.DLL,LockWorkStation
This will lock the current user, but doesn't log off.
You can run it in "cmd" or "Run (Start Menu)"
OR... if using C, VB, Delphi, others... you can just call function "LockWorkStation" at USER32.DLL, and remember it's case sensitive.
It is highly unlikely that Microsoft would readily allow you to do something like that because it would be a security loophole of note. The point is that if no one is currently authorised to use the machine, it shouldn't be possible to run programs. Suddenly despite your best and honest intentions - your program becomes a potential backdoor to hack into machines.
The only kind of custom "application" MS might allow from the login screen would be alternate authentication mechanisms (e.g. fingerprint reader).
These would probably be implemented at the driver level, and have to interact with a very specific API. Even then, I doubt such a driver would be allowed to run an arbitrary application.
You may want to rather consider having your application run on a schedule or as a service without user interaction.

Prevent application launch in C#

Okay I've spent the afternoon researching and haven't had much luck finding the answer to this. I am trying to prevent an application from launching via some sort of dll or background application. It is to be used in monitoring application usage and licenses at my institution. I have found leads here regarding WqlEventQuery and also FileSystemWatcher. Neither of these solutions appear to work for me because:
With WqlEventQuery I was only able to handle an event after the process was created. Using notepad as a test, notepad was visible and accessible to me before my logic closed it. I attempted to Suspend/Resume the thread (I know this is unsafe but I was testing/playing) but this just hung the window until my logic finished.
With FileSystemWatcher I was not able to get any events from launching a .exe, only creating, renaming and deleting files.
The goal here is to not let the application launch at all unless my logic allows it to launch. Is this possible? The next best solution I came up with was forcing some type of modal dialog which does not allow the user to interact with anything, once the dialog is closed the application is killed. My concern here is killing the application nicely and handling applications with high overhead when they load such as Photoshop or something. This would also interfere with a feature I was hoping to have where the user could enter a queue until a license is available. Is this my best route? Any other suggestions?
Thanks
edit: To clarify this is not a virus or anything malicious. It's not about preventing access to a blacklist or allowing access through a whitelist. The idea is to check a database on a case by case basis for certain applications and see if there is a license available for use. If there is, let the app launch, if not display a dialog letting the user know. We also will use this for monitoring and keeping track if we have enough licenses to meet demand, etc. An example of one of these apps is SPSS which have very expensive licenses but a very limited pool of people using it.
Could you use
System.Diagnostics.Process.GetProcessesByName
in a loop to look for the process?
It might work if you don't use too aggressive a polling rate.
You are indeed close, take a look at the WMI Management Events. http://msdn.microsoft.com/en-us/library/ms186151%28VS.80%29.aspx
Sample code from Microsoft: http://msdn.microsoft.com/en-us/library/ms257355(VS.80).aspx
Subscribing to the appropriate event will provide your application with the appropriate information to perform what you described.
Not sure if this is a GOOD solution but you could do something like pass a key into main so that if the key is not present or valid the application shuts down. Then when you open the application in your code, just pass the key in. Someone would then have to know the key in order to start the application.
This is assuming you have access to the application in question's source code, which upon reading your question again, I'm not so sure of.
I assume you don't have source for the application you want to prevent from loading...
Have you considered using a system policy? That would be the best-supported way to prevent a user from launching a program.
You could have a service running that force-kills any app that isn't "whitelisted", but I can't say how well that would work.
I wonder if you are taking the wrong approach. Back in the day there was a Mac app that would prevent access to the desktop and had buttons to launch a set list of applications.
IDEA
What if you had a wrapper for the approved apps then only allow your wrapper to run on the computer?
I would expect there is some way of hooking an application launch, but can't help directly on that front.
You may be able to improve your current approach by detecting the application's window opening and hiding it (move it offscreen) so that the user can't attempt to interact with it while you are trying to shut it down.
However, another approach that may be possible (depending on your circumstances) would be to write an application launcher. This simply is a replacement for the shortcut to the application that checks your licencing conditions, and then does a Process.Start to launch the real .exe at that point. This would work well for any application. (I used a system like this for starting up applications with specialised environment settings and it works beautifully)
You could combine this with your current approach as a fall-back for "clever" users who manage to circumvent your launcher.
If my understanding is right you want to create an application what will prevent the computer user to start any other process except ones for a white-list.
If this is the case, monitor the process list of processes (in a while loop) using System.Diagnostics.Process (the GetProcesses method gives the list of all running ones)
Just kill the process when it starts.
Or if your machines have Windows 7 (Windows 2008??) you can use AppLocker. http://www.microsoft.com/windows/enterprise/products/windows-7/features.aspx#applocker Just let Windows prevent the startup.
You might want to look at this product: http://www.sassafras.com/licensing.html Personally I can't stand it, but that's because it does what you describe. Might save you some coding.
You could actually edit the registry so when you click a psd, your launcher gets called instead of photoshop. Your launcher then checks for licenses and if there is one starts photoshop with the path of the file.
This is a long shot but you may find it helpful.
Perceived Types and Application Registration
http://msdn.microsoft.com/en-us/library/cc144150(VS.85).aspx

Categories