background image

Meterpreter Scripting

241

else
        print_error("You must provide at least one IP!")
end

Meterpreter API

During a penetration test, you might be unable to find an existing script that 
matches what you need in order to perform a required task. If you under-
stand the basic concepts of programming, it should be relatively easy for you 
to pick up the Ruby syntax and use it to write additional scripts.

Let’s start off with a basic print statement that uses the interactive Ruby 

shell, also known as 

irb

. From the Meterpreter console, issue the 

irb

 command 

and begin typing commands:

meterpreter > 

irb

 

[*] Starting IRB shell
[*] The 'client' variable holds the meterpreter client
>> 

After you are inside the interactive shell, you can use it to test the differ-

ent API calls from Meterpreter.

Printing Output

Let’s start with the 

print_line()

 call, which will print the output and add a 

carriage return at the end:

>> 

print_line("you have been pwnd!")

you have been pwnd!
=> nil

The next call is 

print_status()

 and is used most often in the scripting 

language. This call will provide a carriage return and print the status of what-
ever is executing, with a 

[*]

 prefixed at the beginning:

>> 

print_status("you have been pwnd!")

[*] you have been pwnd!
=> nil

The next call is 

print_good()

, which is used to provide the results of an 

action or to indicate that the action was successful:

>> 

print_good("you have been pwnd")

[+] you have been pwnd
=> nil