Discussion:
InternetReadFile to read 1.5mb dll into unsigned char buffer
(too old to reply)
Scott Kraemer
2008-02-01 01:56:52 UTC
Permalink
I am trying to load a dll from a secured directory across https. I can use this method and retrieve small text files etc. However it is not working on large binary files.
The part thats failing is the InternetReadFile, well I shouldnt say its failing. It returns as finished, however the pointer is just 3 characters while the returned size shows correct.
If I try a large 3k text file it returns ok! I can even browse the data via MSVC++ while debugging it.

I am using a third party DLL Inection method that needs a unsigned char *, and a size parameter. I have spent over 6 hours on this and my head is spinning.
If someone can help me out I will send $25 us paypal (paypal only)

I have tried most traditional ways to do this: (credits to unknown coder)

unsigned char* GetInternetFile(LPCTSTR szURL, size_t cbMaxSize, size_t* lpcbActualSize)
{
HINTERNET hNet = NULL;
HINTERNET hUrlFile = NULL;
unsigned char* buffer = NULL;
DWORD cbBytesRead = 0;
SIZE_T cbBytesTotal = 0;
BOOL bResult = FALSE;
const DWORD cbReadSize = 0x4000;
std::wstring header = TEXT("Authorization: Basic MyTopSecretCode\r\n");
DWORD dwBuffSize = 0;
dwBuffSize = header.length();

if (!(hNet = InternetOpen(TEXT("IHG Downloader"), INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_SECURE)))
goto cleanup;
DWORD options = INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_HYPERLINK|INTERNET_FLAG_RESYNCHRONIZE|INTERNET_FLAG_RELOAD|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_SECURE;
if (!(hUrlFile = InternetOpenUrl(hNet, szURL, header.c_str(), dwBuffSize, options, 0)))
goto cleanup;
do
{
if (!(buffer = (unsigned char*) ReallocOrFree(buffer, cbBytesTotal + cbReadSize)))
goto cleanup;
if (!InternetReadFile(hUrlFile, buffer + cbBytesTotal, cbReadSize, &cbBytesRead))
goto cleanup;
cbBytesTotal += cbBytesRead;
/* Max size check and size_t overflow check */
if (cbBytesTotal > cbMaxSize || ((((size_t) -1) - cbReadSize) - 1) < cbBytesTotal)
goto cleanup;
} while (cbBytesRead > 0);
if (!(buffer = (unsigned char*) ReallocOrFree(buffer, cbBytesTotal + 1)))
goto cleanup;
buffer[cbBytesTotal] = '\0';
bResult = TRUE;
cleanup:
if (hUrlFile)
InternetCloseHandle(hUrlFile);
if (hNet)
InternetCloseHandle(hNet);
if (!bResult)
free(buffer);
if (lpcbActualSize) *lpcbActualSize = (bResult ? cbBytesTotal : 0);
return (bResult ? buffer : NULL);
}
void* ReallocOrFree(void* original_ptr, size_t new_size)
{
void* temp = realloc(original_ptr, new_size);
if (!temp) free(original_ptr);
return temp;
}

my email is kozmo _ 1970 @ hotmail DOT com
Volodymyr Shcherbyna
2008-02-01 09:48:38 UTC
Permalink
The code you provided actually is not the best solution.

I suggest manually initiate https get query, and use sequence of InternetReadFile + WriteFile to save file to local folder. I attach an example to this message (a C file named https_donwloader.c) which is able to download a file from https server. Look at defines in the header of the source file, so you can change the server name, the server path, and the local path. I tested the code on a 200 MB file. It downloads the file with no problems.

Good luck,
--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:E%uoj.20844$***@newsfe15.phx...
I am trying to load a dll from a secured directory across https. I can use this method and retrieve small text files etc. However it is not working on large binary files.
The part thats failing is the InternetReadFile, well I shouldnt say its failing. It returns as finished, however the pointer is just 3 characters while the returned size shows correct.
If I try a large 3k text file it returns ok! I can even browse the data via MSVC++ while debugging it.

I am using a third party DLL Inection method that needs a unsigned char *, and a size parameter. I have spent over 6 hours on this and my head is spinning.
If someone can help me out I will send $25 us paypal (paypal only)

I have tried most traditional ways to do this: (credits to unknown coder)

unsigned char* GetInternetFile(LPCTSTR szURL, size_t cbMaxSize, size_t* lpcbActualSize)
{
HINTERNET hNet = NULL;
HINTERNET hUrlFile = NULL;
unsigned char* buffer = NULL;
DWORD cbBytesRead = 0;
SIZE_T cbBytesTotal = 0;
BOOL bResult = FALSE;
const DWORD cbReadSize = 0x4000;
std::wstring header = TEXT("Authorization: Basic MyTopSecretCode\r\n");
DWORD dwBuffSize = 0;
dwBuffSize = header.length();

if (!(hNet = InternetOpen(TEXT("IHG Downloader"), INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_NO_UI | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_SECURE)))
goto cleanup;
DWORD options = INTERNET_FLAG_NEED_FILE|INTERNET_FLAG_HYPERLINK|INTERNET_FLAG_RESYNCHRONIZE|INTERNET_FLAG_RELOAD|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_SECURE;
if (!(hUrlFile = InternetOpenUrl(hNet, szURL, header.c_str(), dwBuffSize, options, 0)))
goto cleanup;
do
{
if (!(buffer = (unsigned char*) ReallocOrFree(buffer, cbBytesTotal + cbReadSize)))
goto cleanup;
if (!InternetReadFile(hUrlFile, buffer + cbBytesTotal, cbReadSize, &cbBytesRead))
goto cleanup;
cbBytesTotal += cbBytesRead;
/* Max size check and size_t overflow check */
if (cbBytesTotal > cbMaxSize || ((((size_t) -1) - cbReadSize) - 1) < cbBytesTotal)
goto cleanup;
} while (cbBytesRead > 0);
if (!(buffer = (unsigned char*) ReallocOrFree(buffer, cbBytesTotal + 1)))
goto cleanup;
buffer[cbBytesTotal] = '\0';
bResult = TRUE;
cleanup:
if (hUrlFile)
InternetCloseHandle(hUrlFile);
if (hNet)
InternetCloseHandle(hNet);
if (!bResult)
free(buffer);
if (lpcbActualSize) *lpcbActualSize = (bResult ? cbBytesTotal : 0);
return (bResult ? buffer : NULL);
}
void* ReallocOrFree(void* original_ptr, size_t new_size)
{
void* temp = realloc(original_ptr, new_size);
if (!temp) free(original_ptr);
return temp;
}

my email is kozmo _ 1970 @ hotmail DOT com
Scott Kraemer
2008-02-01 22:17:41 UTC
Permalink
Thank you for responding. However, I need to read it into memory. I don't want the file to touch the end users filesystem.

"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
The code you provided actually is not the best solution.

I suggest manually initiate https get query, and use sequence of InternetReadFile + WriteFile to save file to local folder. I attach an example to this message (a C file named https_donwloader.c) which is able to download a file from https server. Look at defines in the header of the source file, so you can change the server name, the server path, and the local path. I tested the code on a 200 MB file. It downloads the file with no problems.

Good luck,

--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
Volodymyr Shcherbyna
2008-02-01 23:07:51 UTC
Permalink
You have the length of file in szResponse variable. So, you can allocate a buffer using malloc or new with length you recieve in szResponse, and instead of writing chunks to file, write them into allocate memory region.
--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:hUMoj.56043$***@newsfe10.phx...
Thank you for responding. However, I need to read it into memory. I don't want the file to touch the end users filesystem.

"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
The code you provided actually is not the best solution.

I suggest manually initiate https get query, and use sequence of InternetReadFile + WriteFile to save file to local folder. I attach an example to this message (a C file named https_donwloader.c) which is able to download a file from https server. Look at defines in the header of the source file, so you can change the server name, the server path, and the local path. I tested the code on a 200 MB file. It downloads the file with no problems.

Good luck,

--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
Volodymyr Shcherbyna
2008-02-02 23:32:38 UTC
Permalink
Why do you need to load dll file into memory without touching file system? As far as I remember, it's almost impossible to execute dll's functions without LoadLibrary. And LoadLibrary takes the path to library as a parameter.
--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:hUMoj.56043$***@newsfe10.phx...
Thank you for responding. However, I need to read it into memory. I don't want the file to touch the end users filesystem.

"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
The code you provided actually is not the best solution.

I suggest manually initiate https get query, and use sequence of InternetReadFile + WriteFile to save file to local folder. I attach an example to this message (a C file named https_donwloader.c) which is able to download a file from https server. Look at defines in the header of the source file, so you can change the server name, the server path, and the local path. I tested the code on a 200 MB file. It downloads the file with no problems.

Good luck,

--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
Scott Kraemer
2008-02-03 17:05:24 UTC
Permalink
I am using a stealth Injector library made by Drunken Cheetah that will inject from filesystem, resource, or from memory.

I have tried reading into a malloc memory space, but always seems to only get 3 chars into it.

Thanks,

Scott
"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:ul%***@TK2MSFTNGP06.phx.gbl...
Why do you need to load dll file into memory without touching file system? As far as I remember, it's almost impossible to execute dll's functions without LoadLibrary. And LoadLibrary takes the path to library as a parameter.

--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:hUMoj.56043$***@newsfe10.phx...
Thank you for responding. However, I need to read it into memory. I don't want the file to touch the end users filesystem.

"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
The code you provided actually is not the best solution.

I suggest manually initiate https get query, and use sequence of InternetReadFile + WriteFile to save file to local folder. I attach an example to this message (a C file named https_donwloader.c) which is able to download a file from https server. Look at defines in the header of the source file, so you can change the server name, the server path, and the local path. I tested the code on a 200 MB file. It downloads the file with no problems.

Good luck,

--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
Volodymyr Shcherbyna
2008-02-03 17:59:56 UTC
Permalink
Give me your code. I am sure there is an error in it.
--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:pvmpj.45451$***@newsfe08.phx...
I am using a stealth Injector library made by Drunken Cheetah that will inject from filesystem, resource, or from memory.

I have tried reading into a malloc memory space, but always seems to only get 3 chars into it.

Thanks,

Scott
"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:ul%***@TK2MSFTNGP06.phx.gbl...
Why do you need to load dll file into memory without touching file system? As far as I remember, it's almost impossible to execute dll's functions without LoadLibrary. And LoadLibrary takes the path to library as a parameter.

--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:hUMoj.56043$***@newsfe10.phx...
Thank you for responding. However, I need to read it into memory. I don't want the file to touch the end users filesystem.

"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
The code you provided actually is not the best solution.

I suggest manually initiate https get query, and use sequence of InternetReadFile + WriteFile to save file to local folder. I attach an example to this message (a C file named https_donwloader.c) which is able to download a file from https server. Look at defines in the header of the source file, so you can change the server name, the server path, and the local path. I tested the code on a 200 MB file. It downloads the file with no problems.

Good luck,

--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
Paul Baker [MVP, Windows - SDK]
2008-02-04 21:23:48 UTC
Permalink
This library must either re-implement parts of the Loader, for example to fix relocatable addresses - which is a bad idea, or copy it to a temporary file - which defeats your purpose.

Paul
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:pvmpj.45451$***@newsfe08.phx...
I am using a stealth Injector library made by Drunken Cheetah that will inject from filesystem, resource, or from memory.

I have tried reading into a malloc memory space, but always seems to only get 3 chars into it.

Thanks,

Scott
"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:ul%***@TK2MSFTNGP06.phx.gbl...
Why do you need to load dll file into memory without touching file system? As far as I remember, it's almost impossible to execute dll's functions without LoadLibrary. And LoadLibrary takes the path to library as a parameter.

--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:hUMoj.56043$***@newsfe10.phx...
Thank you for responding. However, I need to read it into memory. I don't want the file to touch the end users filesystem.

"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
The code you provided actually is not the best solution.

I suggest manually initiate https get query, and use sequence of InternetReadFile + WriteFile to save file to local folder. I attach an example to this message (a C file named https_donwloader.c) which is able to download a file from https server. Look at defines in the header of the source file, so you can change the server name, the server path, and the local path. I tested the code on a 200 MB file. It downloads the file with no problems.

Good luck,

--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
Volodymyr Shcherbyna
2008-02-05 18:56:49 UTC
Permalink
Bad idea is not reimplemting the windows loader, but making own implementation which is based on undocumented implementation. Such approach has nothing to do with commercial software, because it is prone to instability; so I assume one of the two possibilities:

a. it is your own project for fun (which actually is not bad idea)
b. your project is some kind of hackery exploit, which loads the dll from internet and does not touch filesystem because some antivirus fs filter driver will catch this event and will trigger alarm (so b is definitly a bad thing).
--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Paul Baker [MVP, Windows - SDK]" <***@online.rochester.rr.com> wrote in message news:***@TK2MSFTNGP06.phx.gbl...
This library must either re-implement parts of the Loader, for example to fix relocatable addresses - which is a bad idea, or copy it to a temporary file - which defeats your purpose.

Paul
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:pvmpj.45451$***@newsfe08.phx...
I am using a stealth Injector library made by Drunken Cheetah that will inject from filesystem, resource, or from memory.

I have tried reading into a malloc memory space, but always seems to only get 3 chars into it.

Thanks,

Scott
"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:ul%***@TK2MSFTNGP06.phx.gbl...
Why do you need to load dll file into memory without touching file system? As far as I remember, it's almost impossible to execute dll's functions without LoadLibrary. And LoadLibrary takes the path to library as a parameter.

--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:hUMoj.56043$***@newsfe10.phx...
Thank you for responding. However, I need to read it into memory. I don't want the file to touch the end users filesystem.

"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
The code you provided actually is not the best solution.

I suggest manually initiate https get query, and use sequence of InternetReadFile + WriteFile to save file to local folder. I attach an example to this message (a C file named https_donwloader.c) which is able to download a file from https server. Look at defines in the header of the source file, so you can change the server name, the server path, and the local path. I tested the code on a 200 MB file. It downloads the file with no problems.

Good luck,

--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
Paul Baker [MVP, Windows - SDK]
2008-02-06 21:11:17 UTC
Permalink
Okay. Bad idea for an application that is intended for business or other important use or to be reliable. Bad application if you're trying to exploit. Fun idea if you are doing it for fun. Personally, reading the response of an HTTP GET request into memory is not my idea of fun ;)

Paul
"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
Bad idea is not reimplemting the windows loader, but making own implementation which is based on undocumented implementation. Such approach has nothing to do with commercial software, because it is prone to instability; so I assume one of the two possibilities:

a. it is your own project for fun (which actually is not bad idea)
b. your project is some kind of hackery exploit, which loads the dll from internet and does not touch filesystem because some antivirus fs filter driver will catch this event and will trigger alarm (so b is definitly a bad thing).

--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Paul Baker [MVP, Windows - SDK]" <***@online.rochester.rr.com> wrote in message news:***@TK2MSFTNGP06.phx.gbl...
This library must either re-implement parts of the Loader, for example to fix relocatable addresses - which is a bad idea, or copy it to a temporary file - which defeats your purpose.

Paul
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:pvmpj.45451$***@newsfe08.phx...
I am using a stealth Injector library made by Drunken Cheetah that will inject from filesystem, resource, or from memory.

I have tried reading into a malloc memory space, but always seems to only get 3 chars into it.

Thanks,

Scott
"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:ul%***@TK2MSFTNGP06.phx.gbl...
Why do you need to load dll file into memory without touching file system? As far as I remember, it's almost impossible to execute dll's functions without LoadLibrary. And LoadLibrary takes the path to library as a parameter.

--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:hUMoj.56043$***@newsfe10.phx...
Thank you for responding. However, I need to read it into memory. I don't want the file to touch the end users filesystem.

"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
The code you provided actually is not the best solution.

I suggest manually initiate https get query, and use sequence of InternetReadFile + WriteFile to save file to local folder. I attach an example to this message (a C file named https_donwloader.c) which is able to download a file from https server. Look at defines in the header of the source file, so you can change the server name, the server path, and the local path. I tested the code on a 200 MB file. It downloads the file with no problems.

Good luck,

--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
Volodymyr Shcherbyna
2008-02-07 11:07:28 UTC
Permalink
Post by Paul Baker [MVP, Windows - SDK]
Personally, reading the response of an HTTP GET request into memory is not my idea of fun ;)
Yes, this was my idea, since I did not expect that this suggestion will be used (probably) for bad purposes.
--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Paul Baker [MVP, Windows - SDK]" <***@online.rochester.rr.com> wrote in message news:***@TK2MSFTNGP04.phx.gbl...
Okay. Bad idea for an application that is intended for business or other important use or to be reliable. Bad application if you're trying to exploit. Fun idea if you are doing it for fun. Personally, reading the response of an HTTP GET request into memory is not my idea of fun ;)

Paul
"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
Bad idea is not reimplemting the windows loader, but making own implementation which is based on undocumented implementation. Such approach has nothing to do with commercial software, because it is prone to instability; so I assume one of the two possibilities:

a. it is your own project for fun (which actually is not bad idea)
b. your project is some kind of hackery exploit, which loads the dll from internet and does not touch filesystem because some antivirus fs filter driver will catch this event and will trigger alarm (so b is definitly a bad thing).

--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Paul Baker [MVP, Windows - SDK]" <***@online.rochester.rr.com> wrote in message news:***@TK2MSFTNGP06.phx.gbl...
This library must either re-implement parts of the Loader, for example to fix relocatable addresses - which is a bad idea, or copy it to a temporary file - which defeats your purpose.

Paul
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:pvmpj.45451$***@newsfe08.phx...
I am using a stealth Injector library made by Drunken Cheetah that will inject from filesystem, resource, or from memory.

I have tried reading into a malloc memory space, but always seems to only get 3 chars into it.

Thanks,

Scott
"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:ul%***@TK2MSFTNGP06.phx.gbl...
Why do you need to load dll file into memory without touching file system? As far as I remember, it's almost impossible to execute dll's functions without LoadLibrary. And LoadLibrary takes the path to library as a parameter.

--
V.
This posting is provided "AS IS" with no warranties, and confers no
rights.
"Scott Kraemer" <kozmo _ ***@hotmail.com> wrote in message news:hUMoj.56043$***@newsfe10.phx...
Thank you for responding. However, I need to read it into memory. I don't want the file to touch the end users filesystem.

"Volodymyr Shcherbyna" <***@online.mvps.org> wrote in message news:***@TK2MSFTNGP03.phx.gbl...
The code you provided actually is not the best solution.

I suggest manually initiate https get query, and use sequence of InternetReadFile + WriteFile to save file to local folder. I attach an example to this message (a C file named https_donwloader.c) which is able to download a file from https server. Look at defines in the header of the source file, so you can change the server name, the server path, and the local path. I tested the code on a 200 MB file. It downloads the file with no problems.

Good luck,

--
V
This posting is provided "AS IS" with no warranties, and confers no
rights.
Loading...