How can I do a tcp syn portscan with golang

1,927 views
Skip to first unread message

Billy Shea

unread,
Nov 15, 2013, 1:55:56 AM11/15/13
to golan...@googlegroups.com
I'm trying to write a tcp syn portscanner with golang, I found a solution in C version here: http://www.binarytides.com/tcp-syn-portscan-in-c-with-linux-sockets/

I'd like to implement it in go, how can I send a tcp header like this:

//TCP Header
    tcph->source = htons ( source_port );
    tcph->dest = htons (80);
    tcph->seq = htonl(1105024978);
    tcph->ack_seq = 0;
    tcph->doff = sizeof(struct tcphdr) / 4;      //Size of tcp header
    tcph->fin=0;
    tcph->syn=1;
    tcph->rst=0;
    tcph->psh=0;
    tcph->ack=0;
    tcph->urg=0;
    tcph->window = htons ( 14600 );  // maximum allowed window size
    tcph->check = 0; //if you set a checksum to zero, your kernel's IP stack should fill in the correct checksum during transmission
    tcph->urg_ptr = 0;


Do I have to use syscall or cgo? I'm really appreciated if someone could help me out.




Alexandre Fiori

unread,
Nov 16, 2013, 6:35:36 AM11/16/13
to golan...@googlegroups.com
I believe you have to use raw sockets:

Billy Shea

unread,
Nov 16, 2013, 9:33:29 AM11/16/13
to golan...@googlegroups.com
Thank you! I'll look into that package.

Kevin D

unread,
Nov 18, 2013, 1:00:26 AM11/18/13
to golan...@googlegroups.com
I decided to try to tackle this myself, and I was able to accomplish it. Here is my example I put up. Hope it helps!

Billy Shea

unread,
Nov 18, 2013, 2:12:58 AM11/18/13
to golan...@googlegroups.com
Wow, brilliant! I happened to find this gopacket, and was trying to do it myself, and you did it! Many many thanks!
Reply all
Reply to author
Forward
0 new messages