RubyGuides
Share this post!

All posts by Jesus Castello

Beyond Bash

If you use Linux you are probably using the bash shell, but turns out there is life beyond bash!

When using aptitude or apt-get I’m sure you tried this at least once:

aptitude ins<tab>
"Oops, that didn't work but I wish it did!"

Getting started with ZSH

Well that and much more is possible using another shell: ZSH, to get started you will want to install it (should be available in your distro repo) . After that just run ‘zsh’ you may get a dialog asking if you would like to create a basic configuration file (.zshrc just like .bashrc) just skip it but don’t get scared when your prompt looks like this:

[e]0;u@h: wa]${debian_chroot:+($debian_chroot)}u@h:w$

This is happening because it’s trying to use your bash prompt which uses a different syntax, now we are going to install “oh-my-zsh” which contains a decent default config, various plugins and it will set zsh as your default shell (you can revert using chsh -s /bin/bash user) also it comes with themes which are nothing more than prompt configurations, you can see images on how they look here:

https://github.com/robbyrussell/oh-my-zsh/wiki/themes

To install oh-my-zsh issue this command:

Now we are ready to start playing with our new shell, let’s explore some of the features.

Better Tab completion

Zsh is able to autocomplete the arguments for most system commands, and even give us a menu-like interface if you tab twice.

This also works with the kill command:

zsh

Typo correction

Zsh will help you to fix those annoying typos you make all the time:

Auto push

If you jump a lot between different dirs you are going to love this, zsh can autopush the dirs you cd into, this means you will be able to list the recent dirs you have been in using the ‘d’ command (which is an alias for dirs -v) and then change to them just typing the corresponding number.

And that’s just a small taste of what zsh has to offer, now it’s your turn to try it and decide if you like it.

Zsh FAQ: http://zsh.sourceforge.net/FAQ/
Zsh documentation: http://zsh.sourceforge.net/Doc/

Ruby vs Java – Strings

Let’s see how we can do some basic operations with strings with 2 languages, starting with how we declare a string variable. This is Ruby vs Java!

As you may know Java is a strong typed language, which means you need to declare the variable type, Ruby infers the type from the contents.

– Length

This one is almost identical, in fact you could use () with Ruby but it’s not required so we leave it out.

– Obtaining individual characters

You can use a Ruby string like an array, in Java you will need to use the charAt method.

– Comparing

Notice how you can’t use == for comparing strings in Java.

– Replacing

These will only replace the word ‘strings’ once, if you wanted to do it for all the repetitions of the word you need to use gsub and replaceAll.

It’s important to remember that Java strings are immutable, meaning that they can’t be modified in place, but you can assign the return value to the same variable, which internally will create a new variable and assign the new value.

On Ruby while they are mutable but you still need to assign the output of sub/gsub because these methods don’t change the string, some methods in ruby have a variant that does change the variable, these usually end with ! (an exclamation mark) like sort! and uniq! in this case we could use sub!/gsub!

And finally here are the links for the documentation for the String class for Ruby and Java:

http://ruby-doc.org/core-1.9.3/String.html

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

CLI ninja: Ping Sweep

Ever wanted to do a ping sweep in this new network you just broke in but you don’t want (or can’t, for some reason, AVs etc…) to upload any tools? Or even in your own network but you don’t have time to install nmap for whatever reason?

Well, you can still do it by leveraging the OS built-in tools. With a for loop we can launch a ping for a whole class C in about 3 min in windows and in about 10 sec in Linux.

Linux Ping Sweep

for i in {1..254} ;do (ping -c 1 192.168.1.$i | grep "bytes from" &) ;done

What this does is a for loop from 1 to 254, $i takes the value of the current iteration so in the first one it will be 1 then 2, 3… and so on, then we tell it to call the ping command with the -c option which means only ping once otherwise it would ping forever after that we pipe the output to grep so we only see the hosts that actually responded and the & at the end send it to the background so it will launch all the pings in parallel. If we only want the ip address and not the whole line we can further filter this using cut.

Windows Ping Sweep

for /L %i in (1,1,255) do @ping -n 1 -w 200 192.168.1.%i > nul && echo 192.168.1.%i is up.

As you can see the idea is the same, -n being the equivalent of -c in Linux’s ping and -w is the timeout, then we send the output to nul and echo only if the ping command was successful (that’s what the && is for)

Introducing Dirfuzz

Dirfuzz is a tool for directory discovery of web applications, by default it uses a dictionary based approach which is in data/fdirs.txt it can also use the crawler module to find links up to 1 level of depth.

dirfuzz

Dirfuzz is designed to give you plenty of information fast and without having to scroll through hundreds of pages of output or deal with a clunky GUI. Dirfuzz is also capable of crawling a page to retrieve links, email address and potential injection points. To activate this feature use the -l1 option.

The project is hosted at github and you can download it and get a bit more info from there:

https://github.com/matugm/dirfuzz

Linux: Monitoring resources – CPU

In this post on the Linux resources series we are going to talk about monitoring our CPU usage. When everything slows down this might be a good first suspect to check, you can use the iostat command with the -c option to see current CPU load, the second parameter specifies the time interval in seconds between each report. If you don’t have iostat you will need to install the sysstat package.

iostat CPU Load

This shows a breakdown of the current CPU activity, for a description of what each value means check the man page for iostat.

Using top

If we want to know which process are using most of the CPU we can use the top command, which will show us information about what’s going on in our system including: running process, cpu, memory usage, uptime…

It will automatically refresh every 3 seconds and it is sorted by the %CPU column by default, you can sort by a different column by pressing the F (capital f) key and then the corresponding key for the column you want to sort by. You can also kill a process directly from top pressing k and then the pid of the process.

Using top for CPU load

One detail that is relevant for CPU usage on top and from the uptime command is the load average (sometimes shortened as loadavg), this gives us an idea on how busy our CPU is over time. There is three numbers for load average, they represent the loadavg of the last 1min, 5min, and 15min, so how does this load average work? Well, this value represents the number of process that are ready to run by the cpu but that are waiting on the queue because the cpu is busy executing some other process, every CPU or core in our system can process 1 unit of work so if we have only a single core CPU and our load average is constantly under 1.0 we are ok, but if the load average is constantly over 1.0 it means that our CPU has more work than it can handle. For a more detailed explanation of this subject head over here.