Skip to content

Instantly share code, notes, and snippets.

@bararchy
Last active August 29, 2015 14:10
Show Gist options
  • Save bararchy/b2a8bce91a0b516ccf5d to your computer and use it in GitHub Desktop.
Save bararchy/b2a8bce91a0b516ccf5d to your computer and use it in GitHub Desktop.

Revisions

  1. Bar Hofesh revised this gist Nov 25, 2014. 1 changed file with 47 additions and 20 deletions.
    67 changes: 47 additions & 20 deletions sshsock.rb
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,42 @@
    require 'rubygems'
    require 'ffi'


    module SSHSocket
    extend FFI::Library
    ffi_lib "/usr/lib/libssh.so"
    attach_function :ssh_bind_new, [], :pointer
    attach_function :ssh_new, [], :pointer
    attach_function :ssh_bind_options_set, [:pointer, :varargs], :int
    attach_function :ssh_bind_listen, [:pointer], :int
    attach_function :ssh_bind_accept, [:pointer, :pointer], :string
    attach_function :ssh_get_error, [:pointer], :string
    extend FFI::Library
    ffi_lib_flags :now, :global
    ffi_lib 'libssh'
    attach_function :ssh_init, [], :int
    attach_function :ssh_bind_new, [], :pointer
    attach_function :ssh_new, [], :pointer
    attach_function :ssh_bind_options_set, [:pointer, :varargs], :int
    attach_function :ssh_bind_listen, [:pointer], :int
    attach_function :ssh_bind_accept, [:pointer, :pointer], :string
    attach_function :ssh_get_error, [:pointer], :string
    attach_function :ssh_handle_key_exchange, [:pointer], :string
    attach_function :ssh_message_get, [:pointer], :string
    attach_function :ssh_message_type, [:string], :string
    end

    module Options
    SSH_BIND_OPTIONS_BINDADDR = 0
    SSH_BIND_OPTIONS_BINDPORT = 1
    SSH_BIND_OPTIONS_BINDPORT_STR = 2
    SSH_BIND_OPTIONS_HOSTKEY = 3
    SSH_BIND_OPTIONS_DSAKEY = 4
    SSH_BIND_OPTIONS_RSAKEY = 5
    SSH_BIND_OPTIONS_BANNER = 6
    SSH_BIND_OPTIONS_LOG_VERBOSITY = 7
    SSH_BIND_OPTIONS_LOG_VERBOSITY_STR = 8
    SSH_BIND_OPTIONS_ECDSAKEY = 9
    end


    def check_error(result, pointer)
    if result.to_i < 0
    if result.class != String && result != nil && result < 0
    puts "Error #{result.to_i}: #{SSHSocket.ssh_get_error(pointer)}"
    exit 1
    elsif result.class == String && result == ""
    puts "Error #{result.to_i}: #{SSHSocket.ssh_get_error(pointer)}"
    exit 1
    else
    @@ -24,23 +47,27 @@ def check_error(result, pointer)

    begin
    sshbind = SSHSocket.ssh_bind_new
    puts "sshbind pointer: #{sshbind}"
    # Configure the session
    result = SSHSocket.ssh_bind_options_set(sshbind, :string, "SSH_BIND_OPTIONS_BINDADDR", :string, "0.0.0.0")
    result = SSHSocket.ssh_bind_options_set(sshbind, :int, Options::SSH_BIND_OPTIONS_BINDADDR, :string, "0.0.0.0")
    check_error(result, sshbind)
    result = SSHSocket.ssh_bind_options_set(sshbind, :string, "SSH_BIND_OPTIONS_BINDPORT", :int, 50505)
    result = SSHSocket.ssh_bind_options_set(sshbind, :int, Options::SSH_BIND_OPTIONS_BINDPORT_STR, :string, "5555")
    check_error(result, sshbind)
    result = SSHSocket.ssh_bind_options_set(sshbind, :string, "SSH_BIND_OPTIONS_RSAKEY", :string, "/home/unshadow/Desktop/rsa.key")
    result = SSHSocket.ssh_bind_options_set(sshbind, :int, Options::SSH_BIND_OPTIONS_RSAKEY, :string, "/home/unshadow/Desktop/keys_for_ssh/ssh_host_rsa_key")
    check_error(result, sshbind)
    result = SSHSocket.ssh_bind_options_set(sshbind, :int, Options::SSH_BIND_OPTIONS_DSAKEY, :string, "/home/unshadow/Desktop/keys_for_ssh/ssh_host_dsa_key")
    check_error(result, sshbind)

    sshsession = SSHSocket.ssh_new
    puts "sshsession pointer: #{sshsession}"
    result = SSHSocket.ssh_bind_listen(sshbind)
    check_error(result, sshbind)
    sshaccept = SSHSocket.ssh_bind_accept(sshbind, sshsession)
    puts "sshaccept should return 'SSH_OK': #{sshaccept}"
    result = SSHSocket.ssh_bind_accept(sshbind, sshsession)
    puts "should return 'SSH_OK': #{result}"
    result = SSHSocket.ssh_handle_key_exchange(sshsession)
    check_error(result, sshsession)
    msg = SSHSocket.ssh_message_get(sshsession)
    puts msg
    type = SSHSocket.ssh_message_type(msg)
    puts type
    rescue Exception => e
    puts "Error #{e.message}"
    #puts "Error #{e.backtrace}"
    end

    puts "Error #{e.backtrace}"
    end
  2. Bar Hofesh created this gist Nov 23, 2014.
    46 changes: 46 additions & 0 deletions sshsock.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    require 'ffi'


    module SSHSocket
    extend FFI::Library
    ffi_lib "/usr/lib/libssh.so"
    attach_function :ssh_bind_new, [], :pointer
    attach_function :ssh_new, [], :pointer
    attach_function :ssh_bind_options_set, [:pointer, :varargs], :int
    attach_function :ssh_bind_listen, [:pointer], :int
    attach_function :ssh_bind_accept, [:pointer, :pointer], :string
    attach_function :ssh_get_error, [:pointer], :string
    end

    def check_error(result, pointer)
    if result.to_i < 0
    puts "Error #{result.to_i}: #{SSHSocket.ssh_get_error(pointer)}"
    exit 1
    else
    puts "No Error: #{result}"
    end
    end


    begin
    sshbind = SSHSocket.ssh_bind_new
    puts "sshbind pointer: #{sshbind}"
    # Configure the session
    result = SSHSocket.ssh_bind_options_set(sshbind, :string, "SSH_BIND_OPTIONS_BINDADDR", :string, "0.0.0.0")
    check_error(result, sshbind)
    result = SSHSocket.ssh_bind_options_set(sshbind, :string, "SSH_BIND_OPTIONS_BINDPORT", :int, 50505)
    check_error(result, sshbind)
    result = SSHSocket.ssh_bind_options_set(sshbind, :string, "SSH_BIND_OPTIONS_RSAKEY", :string, "/home/unshadow/Desktop/rsa.key")
    check_error(result, sshbind)

    sshsession = SSHSocket.ssh_new
    puts "sshsession pointer: #{sshsession}"
    result = SSHSocket.ssh_bind_listen(sshbind)
    check_error(result, sshbind)
    sshaccept = SSHSocket.ssh_bind_accept(sshbind, sshsession)
    puts "sshaccept should return 'SSH_OK': #{sshaccept}"
    rescue Exception => e
    puts "Error #{e.message}"
    #puts "Error #{e.backtrace}"
    end