3

I'm trying to create a raw socket using Ruby.

The problem is, there isn't anything called "raw socket" there and, on the other hand, the Socket class itself is not fully documented.

Does anybody have some code samples for that kind of socket in Ruby, or maybe some kind of a documentation for that?

By the way, I already know how to work with TCPSocket and TCPServer classes, and what I need is particularly a raw socket.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Raafat
  • 173
  • 3
  • 9

3 Answers3

3

Google brings up the following result: http://www.ruby-forum.com/topic/90408

Short version:

require 'socket'

rsock = Socket.open(Socket::PF_INET, Socket::SOCK_RAW, Socket::IPPROTO_RAW)

rsock.send(string, flags)

rsock.recv(1024)

More documentation on the various Socket classes: http://www.rubycentral.com/pickaxe/lib_network.html

(The whole raw sockets thing is rather nasty on unices since it usually requires root access. I did not test this code. You may need to construct the whole packet yourself if you're not using IPSocket)

hhaamu
  • 6,851
  • 3
  • 19
  • 13
  • Thanx dude ..... but i already seen this "http://www.ruby-forum.com/topic/90408" ,it's useless crap, and it's full of mistakes. the Pickaxe 3rd edition was the most useful thing, thanx again. – Raafat Aug 29 '09 at 02:20
  • It does work, sort of. The problem is the sample code you wind up with on google has an ./ip include. the thing they don't tell you is that it's a common template bitstruct class. also, it just sits there and continues to block if it gets less than 1024 of data. just saying that calling it useless crap is probably excessive. – RobotHumans Feb 06 '11 at 03:15
  • Where do you get the ip class used in the ruby-forum example? – Joost Oct 22 '11 at 14:41
2

Have a look at PacketFu. It is very well maintained and used by the Metasploit Project.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
threatagent
  • 190
  • 1
  • 7
  • First thing in readme (https://github.com/todb/packetfu/blob/master/README.rdoc) is about libpcap, do you realy want to write non diagnostic/test apps to have pcap dependencies? – nhed Aug 30 '12 at 13:10
2

Have a look at the racket gem (https://rubygems.org/gems/racket). It seems to be a bit outdated since the last version was released in 2009 but its also used in the metasploit framework.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240