i wrote C code to check if a port is open, i am having trouble converting it to ASM for use with MASM.

here is the C code:



;-----------------

; check_port.asm
; marla@hush.com

;-----------------

.486
.model flat, stdcall
option casemap:none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\masm32.inc
include \MASM32\INCLUDE\kernel32.inc

includelib \MASM32\LIB\masm32.lib
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

;-----------------

.data?

;-----------------

.data

;-----------------

.code

BOOL checkPort(char ip[],unsigned short int port)
{
WSADATA wsaData;
struct sockaddr_in clientService;
SOCKET m_socket;
if (WSAStartup( MAKEWORD(2,2), &wsaData ) != 0)
{
return FALSE;
}
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(ip);
clientService.sin_port = htons(port);
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( m_socket == INVALID_SOCKET )
{
  printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
  return FALSE;
}
if (connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR)
{
  closesocket(m_socket);
  WSACleanup();
  return FALSE;
}
else
{
writeResultTXT(clientService.sin_port, clientService.sin_addr.s_addr, "");
}
closesocket(m_socket);
return TRUE;
WSACleanup();
}
Posted on 2006-02-16 14:54:44 by marla
marla,

This was probably one of my first win32 applications in masm, I moved over to masm from BSD/Nasm so the syntax is kinda rough, but basically it's what's called a vanilla scanner, it connects to several ports from a list and sees if their open. I know I posted this either on this forum or the masmforum but when I searched for it, I couldn't find it anywhere, so here it is.


; Conscan - CLI "test"
.386
.model flat, stdcall
option casemap :none

include windows.inc
include user32.inc
include kernel32.inc
include masm32.inc
include wsock32.inc
includelib user32.lib
includelib kernel32.lib
includelib masm32.lib
includelib wsock32.lib

print macro lpszText:VARARG
local txt
.data
  txt db lpszText,13,10,0
.code
  invoke StdOut,addr txt
ENDM

SOCKADDR_IN struct
sin_family WORD ?
sin_port WORD ?
sin_addr DWORD ?
sin_zero BYTE 8 dup (?)
SOCKADDR_IN ends

.data
szusg db "usage: conscan <hostname>",13,10,0
fmt  db "%d OPEN",13,10,0
sa    SOCKADDR_IN <>
wsa  WSADATA <>
sfd  dd 0
port  dd 0
pl    dd 21
      dd 22
      dd 23
      dd 25
      dd 80
      dd 137
      dd 350
      dd 8080
      dd 6667
      dd 31337
      dd 0
   
.data?
hostname db 2024 dup (?)
buffer  db 100 dup (?)

.code
conscan:
call main
call ExitProcess

main proc
invoke GetCL,1,addr hostname
cmp eax,1
jne Arg_Error

mov sa.sin_family, AF_INET
lea edi,pl
Port_Scan_Loop:
mov eax,
cmp eax,0
je Port_Scan_Complete
inc edi
mov port,eax
invoke WSAStartup,101h,addr wsa
invoke socket, AF_INET, SOCK_STREAM, 0
mov sfd,eax
invoke htons, port
mov sa.sin_port, ax
invoke gethostbyname, addr hostname
mov eax,
mov eax,
mov eax,
mov sa.sin_addr,eax
invoke connect,sfd,addr sa,SIZEOF sa
cmp eax, 0
jne Port_Closed
invoke wsprintf,addr buffer,addr fmt,port
invoke StdOut,addr buffer
Port_Closed:
invoke closesocket,sfd
call WSACleanup
jmp Port_Scan_Loop
Arg_Error:
invoke StdOut,addr szusg
ret
Port_Scan_Complete:
print "-- Scan Complete --"
ret
main endp
end conscan


Regards,
Bryant Keller
Posted on 2006-02-16 18:41:01 by Synfire
thanks alot bryant! this is exactly what i was looking for, this will keep me busy for a long time, and made for masm ... perfect! have the best day ever man!
Posted on 2006-02-17 10:49:38 by marla
Try putting your variables in the .data or .constant section
.code is meant for only invokes and other actions
to set a variable do this
MyVariable db, NULL, "put value here",0
Posted on 2006-06-12 19:50:24 by tjweb

Try putting your variables in the .data or .constant section
.code is meant for only invokes and other actions


Humm, where does Synfire have data in the .code section?
Posted on 2006-06-13 05:04:47 by f0dder
In the K32/K32B include I co-authored with him for assisting with self-modifying and relocatable code projects, but I digress :P
Posted on 2006-06-13 10:43:14 by Homer

Try putting your variables in the .data or .constant section
.code is meant for only invokes and other actions
to set a variable do this
MyVariable db, NULL, "put value here",0


If you are refering to the print "-- Scan Complete --" line, that string is stored in the .data section, refer to the macro which is located in the top of the example source.


In the K32/K32B include I co-authored with him for assisting with self-modifying and relocatable code projects, but I digress :P


I wonder if I should release that.. It has it's good uses, but of course something which simplifies relocatable code projects also might be used in ways that I don't support. I still have the old, outdated, version on my project page which doesn't support any of our mutual advancements (the bare bones version I first wrote without relocatable code support).
Posted on 2006-06-13 20:41:47 by Synfire
Save it for a rainy day - I rewrote a 'lighter' version of it for my own purposes (I needed SMC/RC for my exe self-decompressor thingy recently).. yeah, I think that to post this public would be a mistake and a transgression, wait until someone can come up with a reasonable excuse first, then hand it to them privately.. skeleton keys are handy too, but what kind of people want them?

Posted on 2006-06-20 07:19:52 by Homer
tjweb stop posting stuff you clearly don't know about!
It grows increasingly obvious that your a newbie to masm and while there is nothing wrong in that (we all are/were!), you should slow down a bit!

Take your time to to learn asm more fully and allow more expererienced members to answer the more complex questions.
Posting:
Try putting your variables in the .data or .constant section
.code is meant for only invokes and other actions
to set a variable do this
MyVariable db, NULL, "put value here",0


Is a little like saying 'Dont forget to type your serial number when installing windows' to a networking admin!

It makes you look like a child craving attention add to that the fact that the information you post was incorrect (the opening NULL is not req'ed and would turn your string into a null string if it was formatted correctly (it's not)) and, well, you're not painting a very good impression of yourself!
Posted on 2006-06-22 08:39:24 by asmrixstar
No need to get all excited. As much as experienced members "shouldn't be checked", it is that mentality that allows simple bugs to slip through. Try to keep a happy medium and inform people when they are off-topic in solving something, but being an "elitist" helps no one.

That being said, tjweb, you can learn many things from people like Synfire. Normally, I would ask you to take a step back and read Iczelion's Tutorials, but you need to start further back than that. I would suggest the x86 Book, for starters.

As asmrixstar touched on, calm down and focus that excitement and energy in the right direction :)
Posted on 2006-06-22 16:33:43 by SpooK

tjweb stop posting stuff you clearly don't know about!
It grows increasingly obvious that your a newbie to masm and while there is nothing wrong in that (we all are/were!), you should slow down a bit!

Take your time to to learn asm more fully and allow more expererienced members to answer the more complex questions.
Posting:
Try putting your variables in the .data or .constant section
.code is meant for only invokes and other actions
to set a variable do this
MyVariable db, NULL, "put value here",0


Is a little like saying 'Dont forget to type your serial number when installing windows' to a networking admin!

It makes you look like a child craving attention add to that the fact that the information you post was incorrect (the opening NULL is not req'ed and would turn your string into a null string if it was formatted correctly (it's not)) and, well, you're not painting a very good impression of yourself!


It's all good mate, truth is, that program was written while I was a "newb" (at least to MASM anyways). When he made the remark I actually had to check over it to make sure he wasn't right :p I do understand his confusion as well, being as I used macro where it wasn't really needed (the PRINT macro is generally used when I'm debugging).

We all make errors, be it in code or in judgment. It's mearly how we deal with those errors which determines success or failure.

Regards,
Bryant Keller
Posted on 2006-06-23 00:06:06 by Synfire
:roll: ;)
Posted on 2006-06-28 23:25:47 by asmrixstar