Author Topic: [Ruby]IP logging webserver  (Read 559 times)

0 Members and 1 Guest are viewing this topic.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
[Ruby]IP logging webserver
« on: June 13, 2014, 10:19:39 pm »
Hopefully this doesn't seem like shit posting. This is something I put together this morning, and I'm looking for some more ideas on what to do with this. Fr those of you who can't make sense of simple code, what this does is listen on whatever port you tell it to and then logs all IP addresses that connect to it in a file. When the client connects it prints the client's IP address to the client. It's like a What'sMyIP server. Just fire this server up and enter the IP and port into your URL on the client. Here's the code:
Code: (ruby) [Select]
#!/usr/bin/env ruby


require 'socket' # We need this shit


# Build the socket
server = TCPServer.new(ARGV[0])


# Begin for exception handling
begin
   
    loop do # Keep server alive
        Thread.new(server.accept) do |c| # Threaded for simultaneus connections
       
            # Prints requests to server for debugging
            request = c.gets
            STDERR.puts request
   
    # The whole point of this script
            res = "Your IP address is: #{c.peeraddr[2]}\n"
           
    # Build a proper HTTP response
    # Be sure to include required whitespacing
    c.print "HTTP/1.1 200 OK\r\n" +
        "Content-Type: text/plain\r\n" +
        "Content-Length: #{res.bytesize}\r\n" +
                        "Connection: close\r\n"

    c.print "\r\n"
    c.print res # Print the actual response
       
    # Logs connecting clients IP addresses to a file
    # Saves the exact time of connection
    # Appends logs
            File.open("iplog.txt", "a") do |log|
                log.puts "Connection from #{c.peeraddr[2]} logged at: #{Time.new}"
            end
       
    # Close that shit
            c.close
       
        end
    end

# Gets rid of ugly error messages with Ctrl-C
rescue Interrupt
    puts " Connection terminated"

end
The only trouble I am having is that the exception for SocketError doesn't actually go into effect. It posts the actual Ruby error instead of the output I specified. Not a huge deal really, but I've done it plenty of times before successfully. I'm looking for ways to further this code.

EDIT: Almost forgot. Here's the client code if you want to try it via command-line. I just stole this from a Rubydoc website because it wasn't my focus and it's extremely simple code.
Code: (ruby) [Select]
#!/usr/bin/env ruby


require 'socket'


req = "GET / HTTP/1.0\r\n\r\n"
s = TCPSocket.new ARGV[0], ARGV[1]
s.print(req)
res = s.read
headers, body = res.split("\r\n\r\n", 2)
print body
Note: The server only works on port 1023 and above.
« Last Edit: June 15, 2014, 06:58:01 am by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: [Ruby]IP logging server
« Reply #1 on: June 13, 2014, 10:51:07 pm »
IP echo tools are always nice :)
Perhaps a reverse DNS lookup, time the response, traceroute, portscan ?
Make it respond to a webbrowser, give back the agent etc etc
« Last Edit: June 13, 2014, 10:51:45 pm by proxx »
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [Ruby]IP logging server
« Reply #2 on: June 13, 2014, 11:06:20 pm »
IP echo tools are always nice :)
Perhaps a reverse DNS lookup, time the response, traceroute, portscan ?
Make it respond to a webbrowser, give back the agent etc etc
Hehe actually, I've already coded a portscanner, and a DNS lookup(not reverse). The only thing is that they are separate snippets. I'm trying to combine all these together to create a nice all-in-one command line tool, but working with options parsing on this scale is still a little daunting. Good idea about the user agent though.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: [Ruby]IP logging server
« Reply #3 on: June 13, 2014, 11:24:44 pm »
Hehe actually, I've already coded a portscanner, and a DNS lookup(not reverse). The only thing is that they are separate snippets. I'm trying to combine all these together to create a nice all-in-one command line tool, but working with options parsing on this scale is still a little daunting. Good idea about the user agent though.
Yeah I meant like a tool that if you connect to it with a webbrowser it does that stuff and bounces it back to you.
Or you already got that and I am just drunk :) which would be a valid argument.
« Last Edit: June 13, 2014, 11:25:19 pm by proxx »
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [Ruby]IP logging webserver
« Reply #4 on: June 15, 2014, 07:07:51 am »
I'm curious actually. I spent a good while debugging this code because I couldn't get it to connect consistently in firefox. More then half the time it would give me a connection reset error. I tried everything and none of it made a difference. Until I added this bit of code:

Code: [Select]
            request = c.gets
            STDERR.puts request
Which just prints the HTTP requests to the server. For some reason that I don't understand, that fixed the problem. Could someone explain why to me? I don't like that there's some part of my code that I don't fully understand.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python