Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Put dynamically generated image with no ID into picturebox from webbrowser?

I am working on something and it involves captchas. The captcha has no ID and is obviously dynamically generated.
I have been able to get the src from the tag with this:
Code
foreach (HtmlElement El in GeneratorBrowser.Document.GetElementsByTagName("img"))
                {
                    if (El.GetAttribute("alt") == "Visual Challenge")
                    {
                        GeneratorCaptcha.ImageLocation = El.GetAttribute("src");
                    }
                }

That sadly doesn't work since when I go to the src link in my browser it responds with a 404 page.

I have also tried this
Code
IHTMLDocument2 doc = (IHTMLDocument2)GeneratorBrowser.Document.DomDocument;
                IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
                foreach (IHTMLImgElement img in doc.images)
                {
                    if (img.alt == "Visual Challenge")
                    {
                        imgRange.add((IHTMLControlElement)img);
                        imgRange.execCommand("Copy", false, null);
                        using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
                        {
                            GeneratorCaptcha.Image = bmp;
                        }
                    }
                }
which returns this
[Image: ca9780accc285e746f52e7b2c799aa00.png]

Anyone know how I could do this?
What's the website? With that, it won't be too hard to figure it out.
(Oct 28, 2016 07:50 PM)SL x TnT Wrote:  What's the website? With that, it won't be too hard to figure it out.

I'd rather not put the website out there.
But I know for a fact directly trying to go to the image link won't work at all.

Here's a snippet for the captcha

Code
<div class="col-xs-9">
<img id="Randomly Generated Crap" aria-label="Visual Challenge" alt="Visual Challenge" src="Backend Image Source" style="display: inline; width: 100%; height: auto; max-width: 200px;">
</div>
(Oct 28, 2016 07:56 PM)Lor‌d Wrote:  I'd rather not put the website out there.
But I know for a fact directly trying to go to the image link won't work at all.

Either

a) That captcha is valid for 1 request.
b) You're missing something.

Without the website to test on, it'll purely be guesswork.
(Oct 28, 2016 08:34 PM)SL x TnT Wrote:  Either

a) That captcha is valid for 1 request.
b) You're missing something.

Without the website to test on, it'll purely be guesswork.

Maybe I am missing something.
https://signup.live.com
normally when I am working with images and the red x of death hits the box it means one of my images isn't actually an image. maybe the bytes weren't all there or out of order or maybe the stream is closing before reading to the end.

also might i suggest, if your breaking a captcha there may be an easier way around it orrrr switch to a client based language or some kind of browser plugin (xul, etc)
(Oct 28, 2016 09:03 PM)Lor‌d Wrote:  Maybe I am missing something.
https://signup.live.com

Clicking "new" captcha calls a URL. That returns some JS that's executed.
In that JS you have

Code
G: {
...
    imageurl: 'https://eu1.client.hip.live.com/GetHIPData?hid=EU1.f68669dbad184b2389a65cac26aa04e8&fid=d920612893ba46f0ae2e363fa46999d1&id=15022&type=visual&hdid=0&rnd=c06a9b6383c64cf38c70aa373c74d27e',
...

That url is good for 1 request. Won't be useful for a browser though.




Use

Code
Clipboard.GetImage();

or get rid of the using statement.

Issue 1: Captcha is only valid for a single request.
Issue 2: You're disposing of your image. My VS threw an exception. Dispose of it later.




I took a closer look.

Line 32 of their source:
Code
var $Config= ...

That contains the URL for the JS that calls the image.
Get that source, pull the image url, grab the image that way.

Everything in the POST data can be found in the source except the "CipherValue." Figure that out, and you can ditch the webbrowser.
(Oct 28, 2016 10:04 PM)SL x TnT Wrote:  Clicking "new" captcha calls a URL. That returns some JS that's executed.
In that JS you have

Code
G: {
...
    imageurl: 'https://eu1.client.hip.live.com/GetHIPData?hid=EU1.f68669dbad184b2389a65cac26aa04e8&fid=d920612893ba46f0ae2e363fa46999d1&id=15022&type=visual&hdid=0&rnd=c06a9b6383c64cf38c70aa373c74d27e',
...

That url is good for 1 request. Won't be useful for a browser though.




Use

Code
Clipboard.GetImage();

or get rid of the using statement.

Issue 1: Captcha is only valid for a single request.
Issue 2: You're disposing of your image. My VS threw an exception. Dispose of it later.




I took a closer look.

Line 32 of their source:
Code
var $Config= ...

That contains the URL for the JS that calls the image.
Get that source, pull the image url, grab the image that way.

Everything in the POST data can be found in the source except the "CipherValue." Figure that out, and you can ditch the webbrowser.

Holy shit I love you.
I can't believe I was overlooking such a small thing.
Changed my code to
Code
IHTMLDocument2 doc = (IHTMLDocument2)GeneratorBrowser.Document.DomDocument;
                IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
                foreach (IHTMLImgElement img in doc.images)
                {
                    if (img.alt == "Visual Challenge")
                    {
                        imgRange.add((IHTMLControlElement)img);
                        imgRange.execCommand("Copy", false, null);
                        GeneratorCaptcha.Image = Clipboard.GetImage();
                    }
                }
You shouldn't use the Clipboard method really, as what if the user is working on something and has the program running in the background and he copies something important and then your program copies a captcha image.
(Oct 29, 2016 09:31 AM)Ninja Wrote:  You shouldn't use the Clipboard method really, as what if the user is working on something and has the program running in the background and he copies something important and then your program copies a captcha image.

Do you have another way?
I already added a "clipboard backup" type thing.
It'll restore it to whatever it was before hand.
(Oct 29, 2016 02:47 PM)Lor‌d Wrote:  Do you have another way?
I already added a "clipboard backup" type thing.
It'll restore it to whatever it was before hand.

Im sure there is another way. Try Googling it.
(Oct 29, 2016 06:46 PM)Ninja Wrote:  Im sure there is another way. Try Googling it.

I have been googling. Haven't been able to find anything that works aside from this.
Been trying for quite a while now.
(Oct 29, 2016 06:46 PM)Ninja Wrote:  Im sure there is another way. Try Googling it.

It's not really a question that can be solved on Google. The correct answer is to not use a webbrowser. Second correct answer is to get the image url and request it (not possible in this case). Third is screenshot. Fourth is copy.

- Somehow block the image request. Request it yourself.
- Take the cookies from the browser, request a new captcha, grab image. Modify other values to point to new captcha.
- - I'm not even sure this will even work.
- Take a screenshot of the captcha. <- Might be the best possible solution.

They all have their issues.
(Oct 29, 2016 07:06 PM)SL x TnT Wrote:  It's not really a question that can be solved on Google. The correct answer is to not use a webbrowser. Second correct answer is to get the image url and request it (not possible in this case). Third is screenshot. Fourth is copy.

- Somehow block the image request. Request it yourself.
- Take the cookies from the browser, request a new captcha, grab image. Modify other values to point to new captcha.
- - I'm not even sure this will even work.
- Take a screenshot of the captcha. <- Might be the best possible solution.

They all have their issues.

Personally I think with the route I plan on taking what I am doing right now is fine.
I have it setting the clipboard back so there's no problem with someone losing their old clipboard contents.
(Oct 29, 2016 07:16 PM)Lor‌d Wrote:  Personally I think with the route I plan on taking what I am doing right now is fine.
I have it setting the clipboard back so there's no problem with someone losing their old clipboard contents.

It does come out to a race condition. There's a possibility that someone might copy something new or paste during the time between the copy and restore.

That's why a screenshot might be the best solution in this case. It'll just take a little bit of work.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by Lithium MindJun 20, 2022 09:03 AM 0 159 Jun 20, 2022 09:03 AM
Last Post: Lithium Mind
[C#] by Tr0jandogJan 18, 2021 08:44 AM 7 364 Jan 18, 2021 08:44 AM
Last Post: SIM-4717218
[C#] by C++Dec 29, 2019 06:16 PM 16 831 Dec 29, 2019 06:16 PM
Last Post: DNA