Black Bytes
Share this post!

All posts by Jesus Castello

Mass renaming

Say you have a bunch of files that you want to rename, there is 2 easy approaches to this problem: you can use the aptly named ‘rename‘ utility which should be available on most Linux or you can use a FOR loop. Let’s explore see both ways of mass renaming.

Our job is to rename these images to have a lowercase extension:

Using the rename tool

We can achieve this with the rename command:

Problem solved, easy as that! As you can see the syntax is similar to sed. Basically we are saying “substitute .JPG for .jpg for all files in this directory”

Using a for loop

Now using a for loop we are going to convert these extensions back to uppercase.

There is also a slight variation:

Ruby: fine grained sorting

Ruby sorting is really easy, lets see some examples. If we had this array: ["abc", "aaa", "add", "bcc", "baa"] sorting normally we would get:

Let’s say we wanted to sort by the second letter, we could do this using the sort_by method:

Now lets see a more complex example, if we wanted to sort an email list:

First by the host name and then by the user name we can use the sort_by method like this:

We pass a block to sort_by with the ‘rules’ we want to sort by, we are using a regular expression to express how we want to sort, they are /@./ which matches everything after the at sign and /.@/ which matches everything before.

Finally we can apply the same idea to uniq so that we can get unique data based on a pattern, I use this in Dirfuzz to filter the results and avoid duplicates when I have duplicate results that aren’t exactly the same.

This regexp will allow me to get rid of duplicates with this data:

This would stay the same with a simple uniq, but passing a block with that regexp will get rid of the duplicates.

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)