Rails, writing module. Where is to store setting?
Finally, I got the answer, not sure that is the best way, if no, let me know, please:
module Staticizable
extend ActiveSupport::Concern
included do
class << self;
attr_accessor :has_child_custom
end
end
module ClassMethods
def check_children child
self.has_child_custom = child
end
end
def was_staticized
unless staticized_date
return false
end
(DateTime.now - self.staticized_date.to_datetime).to_i
end
def staticized?
if self.class.has_child_custom.present?
self.send(self.class.has_child_custom).each do |child|
unless child.staticized?
return false
end
end
else
if self.staticize_period > was_staticized
true
else
false
end
end
end
def staticized
self.staticized_date = DateTime.now
save
end
end
Also I've created a file in /config/initializers with
ActiveRecord::Base.send :include, Staticizable
And set it up in model with:
class Building < ActiveRecord::Base
check_children :blocks
...
end
How to get started writing Metasploit modules/exploits?
If you follow this link:
How to get started with writing an auxiliary module
You'll find not only a helpful reference for setting up a metasploit module, but also an entire wiki with (at time of writing) 106 pages on metasploit development and use.
Now, I say reference and not tutorial because making metasploit modules 10% boilerplate code that you need to look up and 90% good-old-ruby that has nothing to do with metasploit.
Take, for example, this simple template module:
require 'msf/core'
class MetasploitModule < Msf::Auxiliary
include Msf::Auxiliary::Scanner
def initialize(info = {})
super(update_info(info,
'Name' => 'Module name',
'Description' => %q{
Say something that the user might want to know.
},
'Author' => [ 'Name' ],
'License' => MSF_LICENSE
))
end
def run
# use `print_status` to print to the metasploit console, instead of `puts`
end
end
Walking through it line by line:
require 'msf/core'
First we require the metasploit files so we can use them.
class MetasploitModule < Msf::Auxiliary
Then we define a new class which inherits from the metasploit auxiliary class.
include Msf::Auxiliary::Scanner
Here we include the metasploit scanner so we can use it in our code. You can include any of metasploit's modules here to use them within your own module; however, you likely won't find tutorials for the modules. You're expected to read the documentation to learn how to use them.
def initialize(info = {})
super(update_info(info,
'Name' => 'Module name',
'Description' => %q{
Say something that the user might want to know.
},
'Author' => [ 'Name' ],
'License' => MSF_LICENSE
))
end
This initialize method is basically boilerplate code that tells metasploit information about your module so it can display said information to users inside the metasploit console.
def run
# use `print_status` to print to the metasploit console, instead of `puts`
end
This is where your code goes! This is also where metasploit ends and good-old-ruby begins. If you're making an HTTP server to echo a malicious payload, use an http server gem and write your logic. You can use metasploit modules here, but you use them (and learn how to use them) the same way you would any other ruby gem or library: look up the documentation and the API reference.
end
And that's it! Ultimately, you're discovering what makes IT security such a difficult field. There aren't any tutorials that can teach you how to hack or frameworks that can help you create exploits. Metasploit is more of a tool for curating collections of exploits, and writing your own module is just "plugging" your exploit into metasploit so other people can easily use it. The exploit itself is simply some ruby code, made to do something clever using basic network libraries.
Creating a completely new and useful hacking tool would be a big deal that some paid security professionals only dream of achieving. I suggest you pick a hacking tool which already exists (password cracker, network scanner, web crawler, etc), research that tool's purpose and functions, get well-acquainted with using it, and work on creating your own version of one. Then, once you've got it doing what you want, take your code and wrap it in a metasploit template so it can be accessed from metasploit.
If you get stuck along the way, you can come back to StackOverflow with more specific questions (eg "How can I scan for open ports on an IP?" or "How do I access options inside a metasploit module?") and we'll be happy to help you with those!
Cheers and good luck!
How to create and use a module using Ruby on Rails 3?
To 1. A module is created/opened
by simply saying:
module MyModule
def first_module_method
end
end
To 2. The lib
folder. If you want to organize your modules in the lib
folder, you can put them into modules themselves. For example, if you wanted a subfolder super_modules
your modules would be defined as follows:
module SuperModules
module MyModule
def first_module_method
end
end
end
To 3./5. When including the module in a class you can simply call the modules methods as if they were defined within the class:
class MyClass
include MyModule
def some_method
first_module_method #calls module method
end
end
To 4.
Frst, make sure that your module is really needed in every class of your application. If it isn't it makes sense to only include it where it is need so as not to bloat the classes that don't need it anyways. If you really want the module everywhere, include look at the class hierarchy of your classes in the app. Do you want the module in all models? You could open ActiveRecord::Base and add add your module there.
Ruby module to print all method calls and returns in class
You can achieve that by defining a new wrapper method for each public instance method of the class that performs the log logic that you need.
module Loginator
def logify_me
self.public_instance_methods.each do |method|
proxy = Module.new do
define_method(method) do |*args|
puts "Method #{method}(#{args.join(', ')}) called"
# added join to match the exact desired output
value = super *args
puts "returns #{value}"
value
end
end
self.prepend proxy
end
end
end
class A
extend Loginator
def add(a, b)
a + b
end
def sub(a, b)
a - b
end
logify_me
end
Which yields the following output:
>> A.new.sub(1,2)
Method 'sub' called with args '[1, 2]'
returns -1
=> -1
>> A.new.add(4,7)
Method 'add' called with args '[4, 7]'
returns 11
=> 11
Explanation for the future :)
define_method(method)
:Defines an instance method in the receiver. The method parameter can be a Proc, a Method, or an UnboundMethod object. If a block is specified, it is used as the method body. This block is evaluated using instance_eval ref. can be found here
prepend
prepend will insert the module at the bottom of the chain, even before the class itself.fourth ref on my post
Section to Edit by @pedrosfdcarneiro
please, add some brief explanation about the second inner module proxy = Module.new do end
and why it's important for the return value and the usage of the super.
plus why did you preferred to use the public_instance_methods
over instance_methods
.
please kindly add some explanation about it because first its a bit unclear to me and secondly for the other noobs out there.
thanks in advance :)
This answer was heavily based on this fantastic answer: https://stackoverflow.com/a/23898539/1781212.
Rails: How do I write tests for a ruby module?
IMHO, you should be doing functional test coverage that will cover all uses of the module, and then test it in isolation in a unit test:
setup do
@object = Object.new
@object.extend(Greeter)
end
should "greet person" do
@object.stubs(:format).returns("Hello {{NAME}}")
assert_equal "Hello World", @object.greet("World")
end
should "greet person in pirate" do
@object.stubs(:format).returns("Avast {{NAME}} lad!")
assert_equal "Avast Jim lad!", @object.greet("Jim")
end
If your unit tests are good, you should be able to just smoke test the functionality in the modules it is mixed into.
Or…
Write a test helper, that asserts the correct behaviour, then use that against each class it's mixed in. Usage would be as follows:
setup do
@object = FooClass.new
end
should_act_as_greeter
If your unit tests are good, this can be a simple smoke test of the expected behavior, checking the right delegates are called etc.
how to write a ruby module to allow methods in class definition
module Foo
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def initializers *names
names.each do |name|
define_method name do
'ok'
end
end
end
end
def self.included(base)
base.extend ClassMethods
end
end
class Anything
include Foo
initializers :hello, :goodbye
end
puts Anything.new.hello #=> ok
How to write test cases for ruby on rails module without an associated database table?
You can create a new class, include your module, and test it:
let(:dummy) do
Class.new do
include TheModule
end
end
subject { dummy.new.get_employee(parameters) }
context "..." do
let(:parameters) { ... }
it "..." do
expect(subject).to eq(...)
end
end
It doesn't imply there should be a database table anywhere, just in the case Employee is a model backed by a database table, you'll need it. For the module you don't need one, it just depends on what it does.
Related Topics
Sort an Array of Strings by Their Integer Values
Gem::Loaderror: Specified 'Sqlite3' for Database Adapter, But the Gem Is Not Loaded
How to Do Sane "Set-Difference" in Ruby
Why Does Foreman Not Output Some Things Until I Press Control-C
How to Configure an Extra/Different Migrations Folder
How to Split String Across New Lines and Keep Blank Lines
Ruby Unable to Parse a CSV File: CSV::Malformedcsverror (Illegal Quoting in Line 1.)
Are There Unintended Consequences of Ruby's 'Begin ... End' Without 'Rescue' Used as a Code Block
How to Download a CSV File in Ruby on Rails
How to Monitor Uptime of 20 Websites (Ping or Http) in Node.Js/Ror
Ruby on Rails 3 - Public Live Chat
Unicorn Unable to Write Pid File
Ruby Request to Https - "In 'Read_Nonblock': Connection Reset by Peer (Errno::Econnreset)"
Rails 3: Generate Unique Codes (Coupons)
How to Do Basic Authentication Over Https in Ruby