Ruby parsing error - ruby

I have the following recipe for Chef:
def prestashop_deployDatabase (username)
sql_path = '/tmp/prestashop_create_tables.sql'
template sql_path do
source "prestashop152.sql.erb"
owner "root"
group node['mysql']['root_group']
mode "0600"
variables(
:username => #{username}
)
action :create
end
end
For some reason; it cannot understand the 'username' argument i'm passing.
PS: I'm a Ruby n00b.

#{username} is a comment in ruby. You should write "#{username}", or better in this case, just username.

In ruby:
# in code starts a one-line comment
#{} in a string starts interpolation - everything in the braces will be interpreted as ruby code.
Since you're using # in code here, it comments out the rest of the line {username}, so in effect your code says this:
variables(
:username =>
)
which will give you a syntax error.

Related

metasploit couldnt find my new created modules

I created a new folder call ~/.msf6/modules/auxiliary/scanner/mssql first, and then I created a new ruby file call ihaz_sql.rb
and here is the code
require 'msf/core'
class MetasploitModule < Msf::Auxiliary
include Msf::Exploit::Remote::MSSQL
include Msf::Auxiliary::Scanner
def initialize
super(
'Name' => 'I HAZ SQL Utility',
'Version' => '$Revision: 7243 $',
'Description' => 'This just prints some funny stuff.',
'Author' => 'TESTs security',
'License' => MSF_LICENSE
)
deregister_options('RPORT', 'PHOST')
end
def run_host(ip)
begin
puts 'I HAZ SQL!!!!'
end
end
end
and then I open metasploit using msfconsole
and do search ihaz and search ihaz_sql, but all of above results shows
[-] No results from search
here is the tutorial I am following https://www.offensive-security.com/metasploit-unleashed/building-module/
I myself am pretty new to Metasploit, so this answer may or may not help you.
I came across such an issue before, the "fix" for it was just to run the command:
use <full pathname to the ruby script>. For example, after adding a ~/.msf6/modules/exploits/cgi/webapps/example.rb ruby script file, run the updatedb command and directly run the use exploit/cgi/webapps/example.rb command to use the exploit.
Hope this solves your issue.

How to use YAML.load with handlers

irb(main):001:0> a="run: yes"
=> "run: yes"
irb(main):002:0> require 'yaml'
=> true
irb(main):003:0> YAML.load a
=> {"run"=>true}
irb(main):004:0> YAML.load(a, handlers => {'bool#yes' = identity})
SyntaxError: (irb):4: syntax error, unexpected '=', expecting =>
YAML.load(a, handlers => {'bool#yes' = identity})
^
from /usr/bin/irb:11:in `<main>
I want the yaml val is yes and i google find the handler will help.
But seems i do not use correct syntax.
I try to search related docs but fail.
The problems with the listed code are
that handlers isn't defined anywhere, you likely wanted :handlers
that identity isn't defined anywhere, maybe wanted :identity that
you are missing a > on your hash rocket (=>).
So to get this code to run it should (likely) look like
YAML.load("run: yes", :handlers => {'bool#yes' => :identity})
However, so far as I know the second parameter to YAML.load is a filename.
If you are able to change the input YAML, simply quoting the value "yes" will cause it come through as a string
YAML.load("a: 'yes'")
# => {"a"=>"yes"}
If you require the un-quoted string 'yes' in the YAML to be treated as 'yes', not true in ruby after parsing. I cobbled this together (with help from this question), using Psych::Handler and Pysch::Parser. Though I'm not sure if there's another easier/better way to do this without having to hack this all together like this.
require 'yaml'
class MyHandler < Psych::Handlers::DocumentStream
def scalar(value, anchor, tag, plain, quoted, style)
if value == 'yes'
super(value, anchor, tag, plain, true, style)
else
super(value, anchor, tag, plain, quoted, style)
end
end
end
def my_parse(yaml)
parser = Psych::Parser.new(MyHandler.new{|node| return node})
parser.parse yaml
false
end
my_parse("a: yes").to_ruby
# => {"a"=>"yes"}
my_parse("a: 'yes'").to_ruby
# => {"a"=>"yes"}
my_parse("a: no").to_ruby
# => {"a"=>false}
Sidenote in the console (and the source):
YAML
# => Psych

Why do rspec filter hooks only work with arrow syntax?

In RSpec tests, I'm using hooks/flags to run subsets of tests, similar to what is shown in the examples
# spec_helper.rb
RSpec.configure do |c|
c.filter_run_excluding('broken')
end
This syntax works
# my_spec.rb
describe 'broken test', 'broken' => true do
...
end
This syntax fails with error syntax error, unexpected ':', expecting end-of-input
# my_spec.rb
describe 'broken test', 'broken': true do
...
end
What is the difference between them which causes one to work and the other to fail?
Your first example
{'broken' => true}
# => {"broken" => true}
creates a Hash with a String as the key. When you use the colon syntax however, the hash will have a Symbol key:
{'broken': true} # This is only valid syntax since Ruby 2.2
# => {:broken => true}
{broken: true}
# => {:broken => true}
Since you are specifically excluding specs marked with a String key, the symbol won't match.
You can either change your rspec config to
RSpec.configure do |c|
c.filter_run_excluding(:broken)
end
or continue to use String keys in your specs.
As a tiny post scriptum: the colon-syntax with a quoted string you used in your first spec example is only valid since Ruby 2.2. Older Ruby versions produce the syntax error you quote in your (edited) question.

Reading a file and creating a user in Chef

I have a list of IP address along with me. In front of those IP I have a username. What I am trying to do is make Chef read the file having IP and username and once it encounter the IP, it should create a user of that name.
But when I do I get a user but the name of the user comes out to be a number.
Here is my recipe
File.open("/tmp/users.txt", "r") do |file|
file.readlines.each_with_index do |ip,user|
if ip = node[:ipaddress]
user ip[user] do
action :create
supports :manage_home => true
comment 'Test User'
home '/home/ip[user]'
shell '/bin/bash'
password 'password'
end
end
end
my users.txt file
231.27.59.232, test1
272.27.59.15, tes2
985.54.25.22, test3
Now when I run the recipe this is what I get
Recipe: repo_update::users
* cookbook_file[/tmp/users.txt] action create (up to date)
* user[1] action create
- create user 1
* user[7] action create
- create user 7
* user[2] action create
- create user 2
Please tell me what is wrong here.
Lots of problem here... The answer of Tejay is the way to go, I'll just try to explain why your code don't work and how to fix it so it could be of some use later :)
File.open("/tmp/users.txt", "r") do |file|
file.readlines.each_with_index do |ip,user|
puts "values are #{ip} and #{user}"
end
end
Gives:
values are 231.27.59.232, test1
and 0
values are 272.27.59.15, tes2
and 1
values are 985.54.25.22, test3
and 2
each_with_index won't split magically your line into two part, it will just assign the last parameter the actual index in the iteration.
A fixed version of your code would be:
File.open("/tmp/users.txt", "r") do |file|
file.readlines.each do |line| # just iterate and get line
ip,myuser=line.gsub("\n",'').split(',') # set ip and myuser variable with values comma separated, using myuser to avoid conflict with the resource name. Using gsub to remove traling carriage return in user name
if ip == node[:ipaddress] # test equality, a single = will assign ip a value and always be true.
user myuser do # create the user using the variable, no need to interpolate here
action :create
supports :manage_home => true
comment 'Test User'
home "/home/#{myuser}" # use interpolation here inside double quotes (won't work in single quotes)
shell '/bin/bash'
password 'password'
end
end
end
end
The problem is this line:
user ip[user] do
You are calling the [] method on the ip string. Furthermore, you're going to get a name collision between the resource user and the block variable. Finally, you are giving each user the home of '/home/ip[user]'. You need to put the string in "'s and wrap the variable in #{ and } Try this:
File.open("/tmp/users.txt", "r") do |file|
file.readlines.each do |line|
entries = line.split(',')
ip = entries[0].strip
username = entries[1].strip
if ip = node[:ipaddress]
user username do
action :create
supports :manage_home => true
comment 'Test User'
home "/home/#{username}"
shell '/bin/bash'
password 'password'
end
end
end
Also, reading this all from a file is a very non cheffy thing to do. Either use a databag or a hash stored in an environment variable, which also saves you from needing to loop at all:
userhash = node['my_users'][node['ipadddress']]
user userhash['username']
action :create
supports :manage_home => true
comment 'test user'
home userhash['home'] || "/home/#{userhash['username']"
shell userhash['shell'] || '/bin/bash'
password userhash['password'] || 'password'
end

Ruby Blather gem, trying to downcase input

I am using Blather to make a chatbot.
I am taking this example from the documentation:
message :chat?, :body => 'hello' do |m|
say m.from, 'world'
end
but I would like it to account for ANY 'case' of hello, i.e. hEllo, HELLO, Hello, and respond with world.
How would I go about doing that?
You should be able to use a regular expression. In this case, just make a simple case-insensitive match:
message :chat?, :body => /hello/i do |m|
# ... etc
If you'll read the "guards" documentation you'll see:
# Hash with regular expression (:body => /exit/)
# Calls the key on the stanza and checks for a match
# Equivalent to stanza.body.match /exit/
message :body => /exit/

Resources