Hello everyone.

So i have this web-form that submits a query string using the Post method.
To dig this submitted query string out, i need to look at stdin.

I know this can be done using C/C++ or assembly language using the C/C++ library;
but i want to learn to do this with raw assembly language and WindowsAPI.

I did a little research and found that to get stdin, i need msvcrt.lib library and call the procedure __imp____iob.

I even went as far as writing the following program at C and debug it:
char Buffer[512];
int InputLength = 5;
fread( Buffer, InputLength, 1, stdin );

And the disassembly shows me that stdin is actually calling the same function:

char Buffer[512];
int InputLength = 5;
0096120F  mov         dword ptr ,5  
fread( Buffer, InputLength, 1, stdin );
00961219  mov         esi,esp  
0096121B  call        dword ptr [__imp____iob_func (966188h)]  
.....


The problem is that i am unable to declare the external function __imp____iob nor call it.
I have tried:

extern __imp____iob@0:proc
call __imp____iob@0

or

extern __imp____iob_func@0:proc
call __imp____iob_func@0

But neither worked. So, i am wondering if it is actually named differently or something wrong i am doing.

Here is a sample full code but not functional:
.386
.model flat, stdcall

includelib kernel32.lib
includelib msvcrt.lib

;Writes to stdout or browser
WriteFile macro consoleHandle, stringAddr, stringSize, outBytesWritten
extern WriteFile@20:proc
push 0
push offset outBytesWritten
mov ebx, stringSize
push ebx
push offset stringAddr
push consoleHandle
call WriteFile@20
endm

.data
string byte "content-type: text/plain",0Ah,0Ah
stringSize equ ($ - string)

STD_OUTPUT_HANDLE equ -11
consoleOutputHandle dword ?
bytesWritten dword ?

queryString byte 5 dup(?)

.code
main PROC
;Trying to call stdin
extern __imp__iob@0:proc
call __imp__iob@0
mov dword ptr queryString, eax

;Get the console handle to output into stdout
extern GetStdHandle@4:proc
push STD_OUTPUT_HANDLE
call GetStdHandle@4
mov consoleOutputHandle, eax

WriteFile consoleOutputHandle, string, stringSize, bytesWritten
WriteFile consoleOutputHandle, queryString, 4, bytesWritten

;Exit program
extern ExitProcess@4:proc
push 0
call ExitProcess@4
main ENDP
END main
Posted on 2010-12-21 20:25:41 by banzemanga
I was able to find my own answer.
msvcrt.lib stands for Microsoft Visual C Runtime meaning that __imp____iob_func is part of Microsoft's C library which is not what i wanted.

To get stdin from Windows API i need to use ReadFile. I was using ReadConsole which did not access stdin.
Posted on 2010-12-22 01:33:24 by banzemanga
In case anybody is interested, here is the full working code:
includelib kernel32.lib

extern ExitProcess@4:proc
exit macro
push 0
call ExitProcess@4
endm

GetEnvironmentVariableA macro envVar, buffer, outBufferSize
extern GetEnvironmentVariableA@12:proc
push 0
push 0
push offset envVar
call GetEnvironmentVariableA@12
push eax
dec eax
mov outBufferSize, eax
push offset buffer
push offset envVar
call GetEnvironmentVariableA@12
endm

STD_INPUT_HANDLE equ -10
STD_OUTPUT_HANDLE equ -11
STD_ERROR_HANDLE equ -12
GetStdHandle macro requestHandle, returnHandle
extern GetStdHandle@4:proc
push requestHandle
call GetStdHandle@4
mov returnHandle, eax
endm

ReadFile macro consoleHandle, buffer, numberOfCharsToRead, pNumberOfCharsRead
extern ReadFile@20:proc
push 0
push offset pNumberOfCharsRead
push numberOfCharsToRead
push offset buffer
push consoleHandle
call ReadFile@20
endm

WriteFile macro consoleHandle, stringAddr, stringSize, outBytesWritten
extern WriteFile@20:proc
push 0
push offset outBytesWritten
mov ebx, stringSize
push ebx
push offset stringAddr
push consoleHandle
call WriteFile@20
endm

.386
.model flat, stdcall

include macros.inc

.data
;Variables
consoleInputHandle dword ?
consoleOutputHandle dword ?
bytesWritten dword ?
bytesRead dword ?

string byte "content-type: text/plain",0Ah,0Ah
stringSize equ ($ - string)

envVar byte "content_length",0
envVarOut byte 128 dup (?)
envVarSize dword ?

buffer byte 128 dup (?)


.code
main PROC
GetEnvironmentVariableA envVar, envVarOut, envVarSize

GetStdHandle STD_OUTPUT_HANDLE, consoleOutputHandle
GetStdHandle STD_INPUT_HANDLE, consoleInputHandle

;Converts numeric string to integer
xor eax, eax
mov ebx, offset envVarOut
xor edx, edx
next_char:
mov ecx, eax
shl eax, 1
shl ecx, 3
add eax, ecx
add eax, edx
mov edx, byte ptr
add ebx, 1
sub edx, 48
jnb next_char

ReadFile consoleInputHandle, buffer, eax, bytesRead

WriteFile consoleOutputHandle, string, stringSize, bytesWritten
WriteFile consoleOutputHandle, buffer, bytesRead, bytesWritten

exit
main ENDP
END main
Posted on 2010-12-22 03:40:07 by banzemanga