0

Currently I redefine the functions window.open, window.unload, etc.; as a void function.

for (int C = 0; C < webBrowser1.Document.GetElementsByTagName("head").Count; C++)
            {
                HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[C];
                HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
                IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
                string alertBlocker = "window.alert = function () { }; window.print = function () { }; window.open = function () { }; window.onunload = function () { }; window.onbeforeunload = function() { };";
                element.text = alertBlocker;
                head.AppendChild(scriptEl);
            }

Now the problem is that the site eg via an external javascript loads this function and it is not there Injected:

<script src="/js/jquery.js"></script> <- there is eg the document.onunload function and is not purely injected. Now, however, the program should search in all loaded Javascripts in the document and inject the code too.

greets, Bl1zz4rD

1 Answer 1

1

To override these Javascript Functions, you need to make sure they are going into the right page.

You may be seeing a staged event in the page loading. This means that once the page is loaded, the scripts may not be loaded but are loading still. Or possibly the absolute URL is not the URL where the scripts are being loaded from.

Try this first:

public void navigateURL(string URL)
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(page_Loaded);
webBrowser1.Navigate(URL); 
}

void page_Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show(e.Url.AbsolutePath)
}

This will tell you if the URL you are trying to inject the code into is the correct one. I have seen on some pages, e.Url.AbsolutePath can change several times in between the loading of the final page. Record the URL's displayed in the MessageBox in the following URL Strings.

private string URL1 = "<First URL to load>";
private string URL2 = "<Second URL to load>";
private string URL3 = "<Third URL to load>";

void page_Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Url.ToString() == "URL3")
{
// Block POP Ups...
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
string alertBlocker = "window.alert = function () { }";
element.text = alertBlocker;
head.AppendChild(scriptEl);

this.webBrowser1.PerformLayout();

webBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(Page_Loaded);
}
}

I have put URL1 - 3 in there so as to indicate the possible URL's you need to work with.

In some situations I have also needed to implement a Timer to wait for objects to load:

void page_Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{          
// Wait for Objects to Load...
delaytimer.Interval = 500;
delaytimer.Enabled = true;
delaytimer.Tick += new EventHandler(PageObjects_Loaded);
delaytimer.Start();
}

void PageObjects_Loaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{ 
// Do your stuff Here...
}

Check all the other simple things also, make sure the page has a head tag, there may already be a code injection going on there, and your code is injected in the head tag.

Hope this helps.

[EDIT]

IHTMLScriptElement is in the mshtml library so you will need a reference to: "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Common" or "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\PIA\Common"

and add: using mshtml;

I see you have this done already, so this is for others.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.