├── .gitignore ├── .rspec ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console ├── pry └── setup ├── exe └── rdpcmd ├── lib ├── rdpcmd.rb └── rdpcmd │ ├── rdpcmd.rb │ ├── remmina.rb │ └── version.rb ├── rdpcmd.gemspec └── spec ├── rdpcmd_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: ruby 3 | rvm: 4 | - 2.3.1 5 | before_install: gem install bundler -v 1.12.5 6 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of 4 | fostering an open and welcoming community, we pledge to respect all people who 5 | contribute through reporting issues, posting feature requests, updating 6 | documentation, submitting pull requests or patches, and other activities. 7 | 8 | We are committed to making participation in this project a harassment-free 9 | experience for everyone, regardless of level of experience, gender, gender 10 | identity and expression, sexual orientation, disability, personal appearance, 11 | body size, race, ethnicity, age, religion, or nationality. 12 | 13 | Examples of unacceptable behavior by participants include: 14 | 15 | * The use of sexualized language or imagery 16 | * Personal attacks 17 | * Trolling or insulting/derogatory comments 18 | * Public or private harassment 19 | * Publishing other's private information, such as physical or electronic 20 | addresses, without explicit permission 21 | * Other unethical or unprofessional conduct 22 | 23 | Project maintainers have the right and responsibility to remove, edit, or 24 | reject comments, commits, code, wiki edits, issues, and other contributions 25 | that are not aligned to this Code of Conduct, or to ban temporarily or 26 | permanently any contributor for other behaviors that they deem inappropriate, 27 | threatening, offensive, or harmful. 28 | 29 | By adopting this Code of Conduct, project maintainers commit themselves to 30 | fairly and consistently applying these principles to every aspect of managing 31 | this project. Project maintainers who do not follow or enforce the Code of 32 | Conduct may be permanently removed from the project team. 33 | 34 | This code of conduct applies both within project spaces and in public spaces 35 | when an individual is representing the project or its community. 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 38 | reported by contacting a project maintainer at kost@linux.hr. All 39 | complaints will be reviewed and investigated and will result in a response that 40 | is deemed necessary and appropriate to the circumstances. Maintainers are 41 | obligated to maintain confidentiality with regard to the reporter of an 42 | incident. 43 | 44 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 45 | version 1.3.0, available at 46 | [http://contributor-covenant.org/version/1/3/0/][version] 47 | 48 | [homepage]: http://contributor-covenant.org 49 | [version]: http://contributor-covenant.org/version/1/3/0/ -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in rdpcmd.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Vlatko Kosturjak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rdpcmd 2 | 3 | Execute commands on Windows via RDP. Helps if you need to run commands on large number of hosts. It is using remmina and xdotool to execute commands. 4 | 5 | 6 | ## Installation 7 | 8 | Quick installation: 9 | 10 | ``` 11 | sudo apt-get install xdotool remmina 12 | gem install rdpcmd 13 | remmina # just to generate global secret 14 | rdpcmd --help # you'll find your way further 15 | ``` 16 | 17 | Add this line to your application's Gemfile: 18 | 19 | ```ruby 20 | gem 'rdpcmd' 21 | ``` 22 | 23 | And then execute: 24 | 25 | $ bundle 26 | 27 | Or install it yourself as: 28 | 29 | $ gem install rdpcmd 30 | 31 | ## Usage 32 | 33 | Run whoami on 192.168.1.1 and wait 5 seconds after: 34 | 35 | ``` 36 | rdpcmd -u user -p password -i 192.168.1.1 -c 'whoami' -x 3 37 | ``` 38 | 39 | Enable WinRM on 192.168.1.1 and wait 5 seconds to finish winrm quikconfig before exiting terminal: 40 | 41 | ``` 42 | rdpcmd -u user -p password -i 192.168.1.1 -e -c 'winrm quickconfig -quiet -force' -x 5 43 | ``` 44 | 45 | 46 | ## Development 47 | 48 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. 49 | 50 | To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). 51 | 52 | ## Contributing 53 | 54 | Bug reports and pull requests are welcome on GitHub at https://github.com/kost/rdpcmd-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. 55 | 56 | 57 | ## License 58 | 59 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 60 | 61 | ## Known Limitations 62 | 63 | Since it is Proof of Concept (PoC), it have some limitations (pull requests are welcome!): 64 | 65 | * You have to match keyboard layout (or command you type will differ from what is typed on RDP session) 66 | * Don't use it from untrusted inputs (google 'command injection' and how scary it is) 67 | * Does not handle errors well (connection failure, wrong credentials, etc) 68 | * Will not report if it fails 69 | * You have to play with timeouts/sleeps if you have slow network or host targets 70 | 71 | 72 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "rdpcmd" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /bin/pry: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "rdpcmd" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | require "pry" 10 | Pry.start 11 | 12 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /exe/rdpcmd: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'bundler/setup' 4 | require 'tempfile' 5 | require 'yaml' 6 | require 'optparse' 7 | require 'logger' 8 | require 'rdpcmd' 9 | require 'csv' 10 | 11 | require 'pp' 12 | 13 | $PRGNAME="rdpcmd" 14 | $options = {} 15 | $options['loglevel'] = 'WARN' 16 | $options['logname'] = nil 17 | $options['domain'] = '' 18 | 19 | # helpful class for logger 20 | class MultiDelegator 21 | def initialize(*targets) 22 | @targets = targets 23 | end 24 | 25 | def self.delegate(*methods) 26 | methods.each do |m| 27 | define_method(m) do |*args| 28 | @targets.map { |t| t.send(m, *args) } 29 | end 30 | end 31 | self 32 | end 33 | 34 | class <@log) 122 | 123 | @log.debug("Connecting to IP: #{ip} with user #{user} and domain #{domain}") 124 | unless rcmd.connect('server'=>ip,'username'=>user,'password'=>password,'domain'=>domain) then 125 | @log.error("Exiting...") 126 | rcmd.terminate() 127 | return 128 | end 129 | 130 | @log.info("Executing: #{$options["cmd"]}") 131 | 132 | if $options['elevated'] 133 | rcmd.singleElevated($options["cmd"],$options["exit"].to_i) 134 | else 135 | rcmd.singleNormal($options["cmd"],$options["exit"].to_i) 136 | end 137 | 138 | rcmd.terminate() 139 | end 140 | 141 | if $options['ip'] then 142 | ip=$options['ip'] 143 | user=$options['user'] 144 | password=$options['password'] 145 | domain=$options['domain'] || "" 146 | 147 | handle_single(ip,domain,user,password,$options["cmd"]) 148 | end 149 | 150 | pp $options 151 | 152 | if $options['file'] then 153 | CSV.foreach($options['file']) do |row| 154 | ip=row[0] 155 | user=$options['user'] 156 | password=$options['password'] 157 | domain=$options['domain'] || "" 158 | cmd=$options["cmd"] 159 | pp domain 160 | pp row.size 161 | if row.size>1 then 162 | domain=row[1] 163 | end 164 | if row.size>2 then 165 | user=row[2] 166 | end 167 | if row.size>3 then 168 | password=row[3] 169 | end 170 | if row.size>4 then 171 | cmd=row[4] 172 | end 173 | 174 | pp row 175 | pp ip, domain, user, password, cmd 176 | 177 | handle_single(ip,domain,user,password,cmd) 178 | end 179 | end 180 | 181 | 182 | -------------------------------------------------------------------------------- /lib/rdpcmd.rb: -------------------------------------------------------------------------------- 1 | require "rdpcmd/version" 2 | require "rdpcmd/remmina" 3 | require "rdpcmd/rdpcmd" 4 | 5 | module Rdpcmd 6 | # Your code goes here... 7 | end 8 | -------------------------------------------------------------------------------- /lib/rdpcmd/rdpcmd.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | module Rdpcmd 4 | 5 | class Rdpcmd 6 | def initialize(params={}) 7 | @log=params.fetch('log','') 8 | end 9 | 10 | def wait4window(pid,ip) 11 | waitfor=5 12 | wid=nil 13 | 14 | waitfor.times do |i| 15 | @log.debug("Waiting for Remmina to start: #{i}") 16 | f = IO.popen("xdotool search --all --pid #{pid} --name #{ip}") 17 | # puts f.readlines 18 | idline='' 19 | f.each do |line| 20 | @log.debug("Parsed xdotool line: #{line}") 21 | idline=line 22 | end 23 | unless idline.to_s.strip.empty? then 24 | wid=idline.chomp 25 | @log.debug("Taking wid: #{wid}") 26 | f.close 27 | break 28 | end 29 | f.close 30 | sleep(1) 31 | end 32 | @wid=wid 33 | return wid 34 | end 35 | 36 | def systeml(str) 37 | @log.debug(str) 38 | system(str) 39 | end 40 | 41 | def connect (opts={}) 42 | rem=Remmina.new 43 | rem.readconfig 44 | 45 | @log.debug("Connecting to IP: #{opts['server']} with user #{opts['username']} and domain #{opts['domain']}") 46 | tempconfig=rem.genconfig(opts) 47 | 48 | @t = Tempfile.new(["rdpcmd",".remmina"]) 49 | @t.write(tempconfig) 50 | @t.close 51 | 52 | cmdspawn="remmina -c #{@t.path}" 53 | @log.debug("Spawning Remmina with cmdline: #{cmdspawn}") 54 | 55 | @pid=Process.spawn(cmdspawn) 56 | 57 | @log.debug("Spawned Remmina with PID: #{@pid}") 58 | 59 | sleepfor=3 60 | @log.debug("Sleeping for #{sleepfor}") 61 | sleep(sleepfor) 62 | 63 | wait4window(@pid,opts['server']) 64 | if @wid.nil? then 65 | @log.error("Cannot find Remmina window, is Remmina and xdotool installed? Connection problem.") 66 | return false 67 | end 68 | 69 | @log.debug("Remmina contacted, window ID: #{@wid}") 70 | 71 | end 72 | 73 | def terminate() 74 | @t.unlink 75 | @log.debug("Killing Remmina with PID: #{@pid}") 76 | Process.kill("KILL", @pid) 77 | end 78 | 79 | def prepareremmina() 80 | systeml("xdotool windowactivate --sync #{@wid} key --clearmodifiers 'Control_R'") 81 | end 82 | 83 | def startrun(cmd) 84 | systeml("xdotool windowactivate --sync #{@wid} key 'Super+r' sleep 2 type '#{cmd}'") 85 | systeml('xdotool key --clearmodifiers "Return"') 86 | end 87 | 88 | def startrunele(sleepfor=4) 89 | startrun("powershell Start-Process cmd -Verb runAs") 90 | systeml("xdotool sleep 3 key 'Alt+y' sleep #{sleepfor}") 91 | end 92 | 93 | def closecmd(sleepfor) 94 | @log.debug("Sleeping for final things to settle") 95 | sleep sleepfor 96 | systeml("xdotool type 'exit'") 97 | systeml('xdotool key --clearmodifiers "Return"') 98 | end 99 | 100 | def cleanupremmina() 101 | systeml("xdotool windowactivate --sync #{@wid} key --clearmodifiers 'Control_R'") 102 | @log.debug("Sleeping for final things to settle") 103 | sleep 1 104 | end 105 | 106 | def sendline(cmd) 107 | systeml("xdotool type '#{cmd}'") 108 | systeml('xdotool key --clearmodifiers "Return"') 109 | end 110 | 111 | def sendkeys(keys,clearmod=false) 112 | opts='' 113 | if clearmod then 114 | opts << " --clearmodifiers " 115 | end 116 | systeml("xdotool key #{opts} #{keys}") 117 | end 118 | 119 | def sendfile(file, sleepfor=0.5) 120 | input= File.new(file, "r") 121 | input.each do |line| 122 | sendline(line) 123 | sleep(sleepfor) 124 | end 125 | input.close 126 | end 127 | 128 | def copyfile(src,dest,sleepfor=0.5) 129 | sendline("copy con #{dest}") 130 | sleep(sleepfor) 131 | sendfile(src, sleepfor) 132 | sleep(sleepfor) 133 | systeml('xdotool key --clearmodifiers "Ctrl+Z" "Return"') 134 | end 135 | 136 | def singleElevated(cmd,toexit=0) 137 | prepareremmina() 138 | startrunele() 139 | sendline(cmd) 140 | if toexit > 0 then 141 | closecmd(toexit) 142 | end 143 | cleanupremmina() 144 | end 145 | 146 | def singleNormal(cmd,toexit=0) 147 | prepareremmina() 148 | startrun('cmd.exe') 149 | sendline(cmd) 150 | if toexit > 0 then 151 | closecmd(toexit) 152 | end 153 | cleanupremmina() 154 | end 155 | end 156 | 157 | end 158 | -------------------------------------------------------------------------------- /lib/rdpcmd/remmina.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'openssl' 4 | require 'base64' 5 | 6 | module Rdpcmd 7 | 8 | class Remmina 9 | def genconfig(opts={}) 10 | conf=templateconfig 11 | conf.gsub!("{{server}}",opts.fetch('server','127.0.0.1')) 12 | conf.gsub!("{{username}}",opts.fetch('username','user')) 13 | conf.gsub!("{{domain}}",opts.fetch('domain','')) 14 | 15 | password=opts.fetch('password','') 16 | conf.gsub!("{{password}}",encrypt_password(password)) 17 | 18 | return conf 19 | end 20 | 21 | def templateconfig() 22 | <<-HEREDOC 23 | [remmina] 24 | disableclipboard=0 25 | ssh_auth=0 26 | clientname= 27 | quality=0 28 | ssh_charset= 29 | ssh_privatekey= 30 | sharesmartcard=0 31 | resolution=1024x768 32 | group= 33 | password={{password}} 34 | name={{server}} 35 | ssh_loopback=0 36 | shareprinter=0 37 | ssh_username= 38 | ssh_server= 39 | security= 40 | protocol=RDP 41 | execpath= 42 | sound=off 43 | exec= 44 | ssh_enabled=0 45 | username={{username}} 46 | sharefolder= 47 | console=0 48 | domain={{domain}} 49 | server={{server}} 50 | colordepth=8 51 | window_maximize=0 52 | window_height=1041 53 | window_width=956 54 | viewmode=1 55 | HEREDOC 56 | end 57 | 58 | def readconfig() 59 | confdir=ENV['HOME']+"/"+'.remmina/' 60 | conffile=confdir+'remmina.pref' 61 | secret=nil 62 | 63 | input= File.new(conffile, "r") 64 | 65 | input.each do |line| 66 | if (line =~ /^secret=/) then 67 | secini=line.split("=",2) 68 | secret=secini[1] 69 | break 70 | end 71 | end 72 | 73 | input.close 74 | 75 | unless secret.nil? then 76 | secret64=Base64.decode64(secret) 77 | @key=secret64[0..23] 78 | @iv=secret64[24..48] 79 | end 80 | end 81 | 82 | def encrypt_password (password) 83 | cipher = OpenSSL::Cipher::Cipher.new('DES3') 84 | cipher.encrypt 85 | cipher.iv=@iv 86 | cipher.key=@key 87 | cipher.padding=0 88 | strpad=password+"\0"*(8-password.length%8) 89 | str=strpad.encode("ascii") 90 | enc=cipher.update(str)+cipher.final 91 | b64=Base64.encode64(enc) 92 | return b64.chomp 93 | end 94 | end 95 | 96 | end 97 | -------------------------------------------------------------------------------- /lib/rdpcmd/version.rb: -------------------------------------------------------------------------------- 1 | module Rdpcmd 2 | VERSION = "0.1.0" 3 | end 4 | -------------------------------------------------------------------------------- /rdpcmd.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'rdpcmd/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "rdpcmd" 8 | spec.version = Rdpcmd::VERSION 9 | spec.authors = ["Vlatko Kosturjak"] 10 | spec.email = ["vlatko.kosturjak@gmail.com"] 11 | 12 | spec.summary = %q{Execute commands on Windows via RDP.} 13 | spec.description = %q{Execute commands on Windows via RDP. Helps if you need to run commands on large number of hosts. It is using remmina and xdotool to execute commands.} 14 | spec.homepage = "https://github.com/kost/rdpcmd-ruby" 15 | spec.license = "MIT" 16 | 17 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 18 | spec.bindir = "exe" 19 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 20 | spec.require_paths = ["lib"] 21 | 22 | spec.add_development_dependency "bundler", "~> 1.12" 23 | spec.add_development_dependency "rake", "~> 10.0" 24 | spec.add_development_dependency "rspec", "~> 3.0" 25 | spec.add_development_dependency "pry", "> 0" 26 | end 27 | -------------------------------------------------------------------------------- /spec/rdpcmd_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Rdpcmd do 4 | it 'has a version number' do 5 | expect(Rdpcmd::VERSION).not_to be nil 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 2 | require 'rdpcmd' 3 | --------------------------------------------------------------------------------