有没有" pwntools" Ruby的等效gem?

时间:2015-07-23 00:08:53

标签: ruby sockets gem

或者无论如何都要使这个函数在ruby套接字编程中工作:

conn = pwn.remote('localhost', 4567)
conn.recvuntil("> ")

使用本机Ruby套接字模块, socket.read(bytes)将等待,直到它收到确切的字节数,我无法决定apriori。并且, socket.recv(bytes),只要获得一些数据就会打印出来,无论消息是否完整。

因为,我知道消息格式及其结尾,如果有如上所述的gem或以任何方式获取Ruby中的功能,那将很容易。

例如,如果我知道来自服务器的消息以冒号结束,

socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in(port, hostname)
socket.connect( sockaddr )
socket.recvuntil ": "
# do something
socket.send "y"

1 个答案:

答案 0 :(得分:0)

require 'socket'

def read_until(socket, re)
  accu = ''
  re = Regexp.escape(re) if String === re
  re = /#{re}\Z/
  socket.each_char do |c|
    accu << c
    return accu if re === accu
  end
  return accu
end

socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in(80, 'www.google.com')
socket.connect(sockaddr)
socket.write "GET / HTTP/1.0\n\n"
headers = read_until(socket, /\r\n\r\n/)
body = socket.read
puts "HEADERS:\n#{headers}BODY:\n#{body}"