How can I pass arguments to msfconsole -r resource.rc <arg1> <arg2> - ruby

So everything is in the title.
Is there a way I can pass arguments to :
msf> resource path/to/resource.rc <arg1> <arg2>
Or
msfconsole -r resource.rc <arg1> <arg2>
Those arguments would passed into the ruby resource code as follow:
<ruby>
ip = ARGV[1]
port = ARGV[2]
...
...
</ruby>

Unfortunately resource files don't accept arguments, but they do accept ruby blocks. So you can do it with a bit of trickery. Make a resource file that looks something like this:
Where it's using the ruby ENV command to pull in the environmental variable "DSTIP"
metasploit-framework [git:master]$ cat /tmp/test.rc
<ruby>
run_single("set RHOST #{ENV['DSTIP']}")
</ruby>
Now when I run msfconsole, I can set that DSTIP variable and it will set the RHOST when I start up MSF to whatever was in that environmental variable:
metasploit-framework [git:master]$ DSTIP=192.168.1.1 ./msfconsole -r /tmp/test.rc -Lq
[*] Processing /tmp/test.rc for ERB directives.
[*] resource (/tmp/test.rc)> Ruby Code (40 bytes)
RHOST => 192.168.1.1
You can do this with as many environmental variables as you want. Now if you want to run it from within MSFCONSOLE I tried changing the environmental variable after msfconsole was running with no luck. I'm sure there is a way that a beardy linux master will have to do it but I don't I'm sorry.
Side note: you can also use ruby file reads to pull in text from. (Think configuration file)
Hope this help!
mubix

Related

Chef - use env variable with package resource what created with bash resource

I would get the changed project from git commit and install the package based on this.
Here is my code
bash 'get_project' do
code <<-EOH
filelist=$(git diff-tree --no-commit-id --name-only -r $1)
for file in ${filelist[#]}; do
project=$(echo $file | cut -d "/" -f1)
projectList+=($project)
done
for changedProject in $(echo "${projectList[#]}" | sort | uniq); do
INSTALLABLE_RPM=application-$changedProject
done
EOH
environment 'INSTALLABLE_RPM' => '$INSTALLABLE_RPM'
end
zypper_package ENV['INSTALLABLE_RPM']
My idea is to generate the INSTALLABLE_RPM variable with bash and install the package with zypper. Unfortunately it doesn't work. The zypper_package resource cant recognize.
I ran out of ideas :-(
The environment property of the bash resource is to supply existing environment variables to execute the bash command(s).
(These variables must exist for a command to be run successfully.)
Specifying environment variables here will not set them in the shell. Also from within the code block you will not be able to access the Ruby's ENV hash.
There may not be a straight-forward way to do this. One of the options is to write this package (list?) to a file. Then we can read the file contents into variable, and use it with zypper_package resource.
Example:
Since you have used for loop in Shell, I believe you get a list of packages, so I am considering pkg_list as Array. I've set compile_time to true as the variable assignment below bash resource will only run during compile time.
bash 'get_project' do
code <<-EOH
# your code as-it-is
for changedProject in $(echo "${projectList[#]}" | sort | uniq); do
echo "application-$changedProject" >> /tmp/rpm_packages
done
EOH
compile_time true
end
pkg_list = File.read('/tmp/rpm_packages').split
zypper_package pkg_list
# remove the file for good measure :)
file '/tmp/rpm_packages' do
action :delete
end

Execute mustache (as ruby script) in cmake execute_process

I would like to use mustache as a simple templating engine in cmake for code generation.
I tried to execute it with execute_process as follow:
execute_process( COMMAND "/path/to/mustache" "<data> <template>" )
But it said its not a valid WIN32 application. And indeed, mustache is a ruby script:
#!D:/programs/Ruby23/bin/ruby.exe
#
# This file was generated by RubyGems.
#
# The application 'mustache' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0.a"
if ARGV.first
...
So I tried:
execute_process( COMMAMD "/path/to/ruby" "/path/to/mustache --help" )
But it don't work either... No such file or directory -- D:/programs/Ruby23/bin/mustache --help (LoadError)
How to execute a ruby script in cmake execute_process?
execute_process(COMMAND < cmd1 > [args1...]] ...)
Arguments must be passed as list, not as string.
# path to executables
set(RUBY_EXECUTABLE D:/programs/Ruby23/bin/ruby.exe CACHE STRING "ruby executable")
set(MUSTACHE_SCRIPT D:/programs/Ruby23/bin/mustache CACHE STRING "mustache ruby script")
# function that call mustache
function(apply_mustache data template result)
execute_process(
COMMAND ${RUBY_EXECUTABLE} -E UTF-8 ${MUSTACHE_SCRIPT} ${data} ${template}
OUTPUT_VARIABLE result_t
)
set(${result} ${result_t} PARENT_SCOPE)
endfunction()
bonus: -E UTF-8 prevent ruby to mess with utf-8 characters...

shell cd command in Ruby

Im trying to execute shell commands using ruby, but i cant change directory to PATH with blank spaces.
variable = %x[cd #{ENV["HOME"]}/Virtual\\ VMs/]
This is not working.
Thank you
To be absolutely safe:
path = File.join [ENV["HOME"], 'Virtual VMs']
variable = %x[cd '#{path}']
Please note, that cd has empty output, so to make sure it works one probably wants to do smth like:
path = File.join [ENV["HOME"], 'Virtual VMs']
variable = %x[cd '#{path}' && ls -la]
#⇒ "total 32\ndrwxr-xr-x ....."
What is ist supposed to do? You try to chdir into a directory, but then don't do anything in it. Your variable will be empty in any case. Aside from the fact that it is pointless to do, you can not reliably execute a cd by itself in this way, because it is not an executable file. You can see this if you just execute %x[cd]. You will get an Errno::ENOENT exception.
Maybe you should first describe in a broader context, what you want to achieve with your code. Where would you like to change the working directory? Within the Ruby process - in which case you have to use Dir.chdir - or in the child process - in which case you have to execute some command after the cd.

ec2-aws create-volume & attach-volume script

I'm trying to make a ruby script to attach an EBS volume in AWS EC2. I'm missing some minor detail because I got an error at attaching the volume. Please see the error below:
#!/usr/bin/env ruby
require 'rubygems'
cmd = "ec2-create-volume --size 10 --region us-east-1 --availability-zone us-east-1a --type gp2"
system(cmd, :out => ['/tmp/volid', 'w'])
volid = `awk '{print $2}' /tmp/volid`
puts "#{volid}" -----> PUTS THE VARIABLE AS EXPECTED
cmd = "ec2-attach-volume #{volid} --instance i-2e69a1b5 --device /dev/xvdg"
system(cmd) ----> It shows ERROR -i instance/--instance is missing, somehow is putting the variable incorectly, it's like is not reading the command after the volid variable.
If I declare up voldid = 'vol-123' the script is working but if I put it from a file is not working even if output it shows vol-123.
At guess there is a end of line character at the end of the file you are reading the volume id from. The strip function removes leading and trailing whitespace which should clear this up.
It also seems like it would be easier to do
volid = File.read("/tmp/volid").strip
Rather than shelling out to awk. You can also avoid the use of a temporary file altogether by using one the various method that allow you to capture command output (backticks, popen, open3 etc). The aws ruby sdk is also pretty easy to use, although that is obviously a different kettle of fish.

Loading variables from a separate file for Rake

I am trying to get into Ruby / Rake. I thought it would be a good idea to separate some configuration out of the Rakefile in a file called configuration. This file has the following content:
email="nobody#nowhere.com"
password="somepassword"
proxy_server="someproxy.com:8080"
puts "config loaded"
Then in my Rakefile I load the configuration file by
load 'configuration'
and use the variables defined later on, e.g.:
task :dummy do
sh = "echo #{proxy_server}"
end
But then I get an error stating that the variable is not defined:
rake aborted!
undefined local variable or method `proxy_server' for #<Object:0xb783595c>
How can I access the configuration variables defined in the configuration file?
Visibility: variables from configuration file are not visible in the script that has executed it. You need to establish a common context, for example using global variables (even if globals are evil:-)) like:
$proxy_server="someproxy.com:8080"
and then
task :dummy do
sh = "echo #{$proxy_server}"
end
But since rake files are Ruby themselves why use a separate configuration file in the first place? If you need to change the config you can as well edit rake file.

Resources