How can i use RAW Sockets in Ruby? - ruby

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.

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)

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.

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

Related

Is it possible to use Apache Thrift without RPC?

I searched on the internet but couldn't find anything useful. First, I was thinking to use Protocol Buffers but it doesn't provide built in feature to track multiple messages (where one message finish and second starts) or message self delimiting, but I read about this feature in Thrift white paper and it seems good to me. Now I am thinking to use Thrift instead of Protocol Buffers.
I am working on custom protocol for that I don't require RPC, could someone suggest if I can use Thrift without RPC (as its in the Protocol Buffers, one simply use the streams function) and some starting point as thrift documentation is a bit cumbersome.
Thanks!
Yes, It is possible. A similar answer is given Here. Apache thrift can be used without RPC you can simply use transport and protocol layers related libraries as they are defined in the documentation.
Apache Thrift is indeed a RPC- and serialization framework. The serialization part is used as part of the RPC mechanism, but can be used standalone. For the various languages there are samples and/or supporting helper classes available. If this is not the case for your particular language, the necessary code pretty much boils down to this (pseudo code):
var data = InitializeMyDataStructure(...);
var trans = new TStreamTransport(...);
var prot = new TJSONProtocol(trans);
data.write(prot);
Both transport(s) and protocol are pluggable, so instead JSON and a stream you are free to use your own protocol, and (for example) a file transport. Or whatever else combination makes sense for your use case and is supported for your target language.
as thrift documentation is a bit cumbersome.
You are free to ask any question, be it here or in the mailing list. Furthermore, we have a nice tutorial and the Test server/client pairs are also good examples for typical use cases.

Building a linux service in Ruby that other processes can interact with, maybe via a socket?

I'm looking into building a service to run in the background that allows clients to connect and send commands, and get data back. I'm planning on writing the service in Ruby (as a gem) but wanted to know what the best method would be to allow clients to connect to the API?
I figured a socket connection would make sense, like you'd connect with Redis or something, but I'm not sure where to start!
Any tips would be much appreciated :)
Yep, you're on the right path. A socket is just a bidirectional communication channel that allows two programs to exchange bytes. If both endpoints are on the same machine, UNIX sockets are the obvious choice; otherwise, you'll need a TCP socket to communicate over the network. The principle is the same in either case.
On top of the socket, you'll have to define your own protocol, or you could use an existing one (such as HTTP) if it applies to your situation.
A random sockets tutorial.
Since you ask for any tips, my advice to you is that building a service container is hard work. Since you don't actually need to, there being lots of awesome service containers already, you should probably use one of those.
I would recommend something behind HTTP, which gives you a whole lot of advantages around existing tooling, message framing, content negotiation, scaling your service, and deployment and upgrade models.
If you want to avoid external dependencies, using something like Webrick or Mongel that is pure Ruby is a fine way to avoid needing to wrap Apache or Nginx around your system.
This also allows you to separate out the concerns in your project: work on building the actual service layer first, handling commands and returning responses. Run that under any web server, and get it going.
Then when you have time, focus separately on how to build the service container to meet your needs: because you know that the underlying service layer works fine, you can focus on only solving the container problems.
If you really do want to build your own container, I strongly recommend you use something higher level than a socket. Tools like 0mq provide framing and other message layer features that you don't get from a socket, and make it much easier to focus on defining the interesting parts of your problem space - the commands - rather than low level details like parsing a wire format and protocol.
I'm using a Ruby/Rails app with Redis running in the background on an EC2 server (Amazon Web Services AWS). This is the ubuntu build I found to be easiest to work with:
Linux version 2.6.32-341-ec2 (buildd#crested) (gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) ) #42-Ubuntu SMP Tue Dec 6 14:56:13 UTC 2011
In my main .rb file that does most of the polling/searching I have this rubygems required, you should definitely check them out:
require 'aws'
require 'redis'
require 'timeout'
require 'json'
Let me know what you are specifically trying to do if that doesn't help you enough. Good luck!
I've built a couple of daemons with EventMachine in the past. It is efficient and powerful, supports TCP, HTTP and everything else. People even write web servers on top of it.

Confused about Thrift, what does it really do?

Can someone explain to me what thrift really does?
Say i have a Rails app, and I also have some code written in Scala.
Could thrift be used to generate an interface for my Scala code so that I could call it from Ruby?
Would the Scala code have to be written as a daemon for this to work?
I'm not really sure what Thrift's job is, other than it is used to link between various languages. Does it communicate over a socket?
Thrift is simply a binary serialization protocol. It is cross-language, so you can serialize in Scala, and then unserialize in Ruby.
Then you have to move the data, that's another story. You can use files, play directly with sockets, use a server, etc.
So how is this used for cross-platform development? Still not getting it!
Your Ruby and Scala code can reside on different machines running completely different OSes.

Ruby Torrent Library

Is there any good library for Ruby to work with BitTorrent trackers? To download or seed files. There's a rubytorrent library on rubyforge, but it was last updated in 2005 and doesn't seem like working anymore.
see lib-torrent ruby...
https://github.com/maran/libtorrent-ruby
I'm not sure if this is what you want.
Also see this post which contains some potentially useful comments as well, including a Ruby wrapper for the Transmission API using RPCs.
My experience with libtorrent derived clients has been very positive, I would like to see something new here. (I prefer the high density interface & advanced features of qbittorrent to the sparse UI of Transmission.)
A torrent client that exchanges DHT buckets to crawl the public DHT... ?
(No web searching required.)

Is there a good DNS server library in ruby?

I want to create a test DNS server in ruby, but could not find anything suitable. I found pnet-dns(http://rubyforge.org/projects/pnet-dns/). This project is incomplete and buggy. Is there any alternative?
A language-agnostic alternative is to use PowerDNS pipe backend. Because it communicates with a name server across a simple pipe, it can be written in any language, including Ruby. (The simple example in the documentation uses Perl but it should be easy to translate.)
RubyDNS is what you're looking for.
Checkout an another approach of DNS server in ruby using celluloid: https://github.com/celluloid/celluloid-dns
The original celluloid-dns is horribly incomplete (v0.0.1). Recently, RubyDNS is being copied into celluloid-dns (I'm doing this as we speak). RubyDNS will be modified to work with the updated celluloid-dns since all core functions will be moved from RubyDNS to celluloid-dns.
If you want something that works right now, use RubyDNS. However, in the future, if you just want the low level APIs, use celluloid-dns.
Have you looked at Dnsruby?
It aims to be fully RFC compliant, although it focuses primarily on the client side. It is, however, possible to write your own server - use Dnsruby::Message#decode to decode incoming packets, and a zone of RRSets holding your test records. You can then encode your packets to send back to the client.

Resources