scripted files download under Windows - windows

I'm about to be forced to write a script to download some number of files under Windows XP. The machines the script will be run at are all behind a proxy, and the proxy settings are entered into the IE configuration.
What came to my mind was either to somehow call IE from the command line, and using its configuration download files I'd need. Is it even possible using some shell-techniques?
Other option would be to use wget under Win, but I'd need to pass the proxy-settings to it. How to recover those settings from IE configuration?

In principle, I would go for the wget approach rather than using IE in some way.
The path to the configuration script is stored in the registry in HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections\DefaultConnectionSettings. It is a binary value, the script address starts at position 0x18 and seems ASCII encoded.
What I don't know is if wget can evaluate the script by itself, or if you need to parse it explicitly in your script that would then pass the proxy address to wget.

I agree with Treb you should prefer using wget and the path to the proxy settings can be found in "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer"

Use JScript:
function ie_NavigateComplete2(pDisp, url)
{
// output for testing
WScript.Echo('navigation to', url, 'complete');
// clear timer
t = 0;
}
// create ActiveX object
var ie = WScript.CreateObject('InternetExplorer.Application', 'ie_');
ie.Height = 200;
ie.Width = 200;
ie.Visible = true;
ie.Navigate('http://www.example.com/worddoc.doc');
var t = (+new Date()) + 30000;
// sleep 1/2 second for 30 seconds, or until NavigateComplete2 fires
while ((+new Date()) < t)
{
WScript.Sleep(500);
}
// close the Internet Explorer window
ie.Quit();
Then you invoke it with start download.js or cscript download.js. You can do something similar with VBScript, but I'm more comfortable in JScript.
Note that this ONLY works if the target of the ie.Navigate() is a file that prompts for Open/Save/Cancel. If it is a file type such as PDF that opens inside the browser, then IE will simply open the resource, then close the window, probably not what you want. Obviously you could adjust the script to suit your needs, such as not closing the IE window when the download is complete, or making the window larger, etc.
See the InternetExplorer Object documentation for more information about the Events, Methods and Properties available.
Using this method, you don't have to worry about reading the proxy settings for Internet Explorer, they will be used because you are using Internet Explorer to do the download.

Related

How could I redirect HTTP requests to localhost within my own computer through a script or program?

Similar to how one could use a Fiddler script to redirect outgoing requests to a different URL (example below), I would like to redirect outgoing requests to certain URLs to localhost or another URL.
var list = [ "https://onetoredirect.com", "https://twotoredirect.com" ]
static function OnBeforeRequest(oS: Session) {
if(oS.uriContains("http://URLIWantToFullyBlock.com/")){
oS.oRequest.FailSession(404, "Blocked", "");
}
for(var i = 0; i < 2;i++) {
if(oS.uriContains(list[i])) {
oS.fullUrl = oS.fullUrl.Replace("http://", "https://");
oS.host = "localhost"; // This can also be replaced with another IP address.
break;
}
}
}
Problem is that I need to do this for a program that I do not have access to, so I cannot just edit the program to send to these new URLs. The two vague ideas I had were
A script/program that runs system-wide and redirects the requests
A script/program that watches my specific process (I have the ability to launch the process programmatically if need be) for these requests and redirects them
Either is viable, obviously I would prefer doing whichever is easier or more versatile lol.
I want to write this as part of a launcher for a game, where you can either use my launcher which would launch the game with the redirection on, or launch the game normally and have the redirection off (to play normally), essentially removing any need for user modification. It is also okay for the solution to be Windows only since the game is Windows only at the moment!
I ended up setting up a proxy with mitmproxy with a custom script, then setting the Windows proxy settings to go through localhost automatically!

How to convert IHTMLImgElement to image

I am automating Internet Explorer using SHDocVW.dll and MSHTML with C#, and I wish to save an image from the page to the disk (JPEG format).
I can't use the WebClient class to download the image; if I do it, I end up downloading the site's login page. I can't print the screen either, because the browser has to remain invisible during this process, running in the background.
I have tried to do the following:
IHTMLImgElement imgElement = ...;
IHTMLControlRange imgRange = ...;
imgRange.add(imgElement as IHTMLControlElement);
imgRange.execCommand( "copy", false, null );
This does nothing. I am not able to extract anything from the clipboard. Every solution I found didn't work for me.
Your webclient approach is probably missing cookies... see How do I log into a site with WebClient? for an example that handles cookies.
your code looks fine except the user has to change the security setting to enable clipboard access. If the image is cached on disk you can dig the WinInet cache after parsing the page for the image location.

Registering a protocol handler in Windows 8

I'm trying to register my application that will handle opening of links, e,g, http://stackoverflow.com. I need to do this explicitly for Windows 8, I have itworking in earlier versions of Windows. According to MSDN this has changed in Win8.
I've been through the Default Programs page on MSDN (msdn.microsoft.com/en-us/library/cc144154.aspx) page on MSDN. It provides a great walkthrough on handling file types but is light on details for protocols. Registering an Application to a URL Protocol only goes over the steps involved in setting up a new protocol, but not how to correctly add a new handler to an existing protocol.
I've also tried the registry settings outlined in other SO posts.
One more thing, the application is not a Metro/Windows Store App, so adding an entry in the manifest won't work for me.
You were on the right track with the Default Programs web page - in fact, it's my reference for this post.
The following adapts their example:
First, you need a ProgID in HKLM\SOFTWARE\Classes that dictates how to handle any input given to it (yours may already exist):
HKLM\SOFTWARE\Classes
MyApp.ProtocolHandler //this is the ProgID, subkeys are its properties
(Default) = My Protocol //name of any type passed to this
DefaultIcon
(Default) = %ProgramFiles%\MyApp\MyApp.exe, 0 //for example
shell
open
command
(Default) = %ProgramFiles%\MyApp\MyApp.exe %1 //for example
Then fill the registry with DefaultProgram info inside a Capabilities key:
HKLM\SOFTWARE\MyApp
Capabilities
ApplicationDescription
URLAssociations
myprotocol = MyApp.ProtocolHandler //Associated with your ProgID
Finally, register your application's capabilities with DefaultPrograms:
HKLM\SOFTWARE
RegisteredApplications
MyApplication = HKLM\SOFTWARE\MyApp\Capabilities
Now all "myprotocol:" links should trigger %ProgramFiles%\MyApp\MyApp.exe %1.
Side note since this is a top answer found when googling this kind of an issue:
Make sure the path in the shell command open is a proper path to your application.
I spent an entire day debugging issue that seemed only to affect Chrome and Edge on Windows 10. They never triggered the protocol handler while Firefox did.
What was the issue? The path to the .bat file used mixed
\ and / slashes.
Using only proper \ slashes in the path made Edge & Chrome suddenly able to pick up the request.
LaunchUriAsync(Uri)
Starts the default app associated with the URI scheme name for the specified URI.
You can allow the user to specify, in this case.
http://msdn.microsoft.com/library/windows/apps/Hh701476
// Create the URI to launch from a string.
var uri = new Uri(uriToLaunch);
// Calulcate the position for the Open With dialog.
// An alternative to using the point is to set the rect of the UI element that triggered the launch.
Point openWithPosition = GetOpenWithPosition(LaunchUriOpenWithButton);
// Next, configure the Open With dialog.
// Here is where you choose the program.
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
options.UI.InvocationPoint = openWithPosition;
options.UI.PreferredPlacement = Windows.UI.Popups.Placement.Below;
// Launch the URI.
bool success = await Windows.System.Launcher.LaunchUriAsync(uri, options);
if (success)
{
// URI launched: uri.AbsoluteUri
}
else
{
// URI launch failed. uri.AbsoluteUri
}

Using Pgp.exe from a MVC application

I've been tasked with converting a legacy application to mvc. The app used pgp.exe to pgp sign user input and send it as an email. The application works locally and on a test server but won't run on a live server. I've had to jump though hoops such as running a specified user in the application pool so that we can set the keys in the users profile BUT it worked.
For some reason on the live server which is windows 2003 IIS 6 and identical to the testing server it fails. The problem is pgp.exe just wont seem to sign and create files the message I get from the console out put is. "Signature Error"?? When I put the command into a shell window logged in as the app pool user it runs no problem (after a fight with some permissions) but when running through the mvc application/IIS server it fails. The code used to call the process is below.
var startInfo = new ProcessStartInfo();
startInfo.FileName = _pgpexeLocation;
//startInfo.FileName = "pgp.exe";
startInfo.Arguments = string.Format("-sta \"{0}\" -u keyuser-z keypass +COMPATIBLE +FORCE", _tempFilePath);
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.LoadUserProfile = true;
using (Process exeProcess = Process.Start(startInfo))
{
// TODO: set limit to wait for and deal with exit
exeProcess.WaitForExit();
//var stringItem = exeProcess.StandardOutput.ReadToEnd();
//Logger.Info(stringItem);
}
I'm clutching at straws here hoping somebody has done something similar before and can help. I'm guessing it's key location or file location not being picked up somewhere but not sure what else to try?
Turns out that even though the app pool was using a specific user and I'd set the keys up in that users appdata folder when I checked the underlying process call it was actually trying to pick the keys up from the Default User profile. Not sure if this was an IIS config or something similar but moving the keys and pgp folder to this appdata instead worked?

WebBrowser control, isolation and IE8 InPrivate mode

I have a need to run some automation tasks in a web browser control but I seem to be facing a few limitations/unknowns that I'm not 100% sure how to resolve. The application I'm running is not for public release, so I can enforce a prerequisite that IE8 is installed.
GeckoFX (http://geckofx.org) would be great except it does not offer me an acceptable way to manipulate the DOM as I would using the WebBrowser's InvokeMember method on HtmlElement objects.
WebKit.net would be even better but it's too early in its development to offer the functionality I need to do this either.
This leaves me with the WebBrowser control. The problem with WebBrowser though is that it just runs IE, which is a big fat shared environment with all processes. In other words, all instances share cookies, sessions, proxy settings, etc.
Here's what I want:
At the end of an automation session, cookies/sessions/cache objects are not retained. Rather than clearing the global Temporary Internet Files folder, is there a way for me to access the InPrivate mode exposed by IE8?
If there is a way to access InPrivate browsing, is it possible for me to run two InPrivate-mode sessions side-by-side isolated from each other?
Ideally I'd like to be able to run multiple isolated automation tasks in separate threads, each with its own private browser control, each with its own isolated session/environment that is not retained when the task completes.
Any help or input into this would be appreciated!
No, you cannot run the WebBrowser control in InPrivate mode; it's simply not a supported scenario.
Yes, you can run two instances of IE in InPrivate mode and isolate them from each other.
Use the command line: iexplore.exe -private -nomerge
Here's some code that will give you access to an InPrivate IE
Friend Function Open(Optional ByVal Url As String = "about:blank", Optional ByVal WindowState As ProcessWindowStyle = ProcessWindowStyle.Hidden) As WebBrowser
On Error Resume Next
Dim Start As New ProcessStartInfo
Dim Windows = New ShellWindowsClass
Dim Count = Windows.Count
Start.FileName = "iexplore.exe"
Start.Arguments = "-private -nomerge " & Url
If WindowState = ProcessWindowStyle.Hidden Then
Start.WindowStyle = ProcessWindowStyle.Minimized
Else
Start.WindowStyle = WindowState
End If
Process.Start(Start)
Wait.Reset()
Do
If Windows.Count > Count Then Exit Do
Loop While Wait.Waiting
Browser = Windows(Count)
Browser.Visible = (WindowState <> ProcessWindowStyle.Hidden)
Return Browser
End Function

Resources