Homokozó:Programozás/4

A Wikiforrásból
← 3. leckeProgramozás
szerző: Szervác Attila
4. lecke
5. lecke →
Ch4: Preparing for our next Games

.4 Preparing for our next Games[szerkesztés]

Attacking files[szerkesztés]

We will use more files & more data – "bigger data" :)

So first of all let’s write an other classical hacker program: called "brute force".

Let’s assume that we have file entries named with 3 [a-z] letters in a directory that we don’t have the read – r – right to list, thus we have to guess the wanted filename by our "brute force" method.

Now known the decimal ASCII codes of [a-z]: 97-122. With \000 octal form we can printf a character, check:

sh -c 'a=97; o=`printf %o $a`; printf "\\$o"';

So well, let’s create a string containing all letters:

sh -c 'i=97; while [ $i -le 122 ]; do o=`printf %o $i`; l=`printf "%b " "$l\\\\$o"`; i=$((i+1)); done; printf "$l\n"'

Thus, with a simple nested for loop we can generate all 3-letter [a-z] string :)

sh -c 'i=97; while [ $i -le 122 ]; do o=`printf %o $i`; l=`printf "%b " "$l\\\\$o"`; i=$((i+1)); done; for a in $l; do for b in $l; do for c in $l; do printf "$a$b$c "; done; done; done; printf "\n"'

Finally we only need an if to printf only the existing file entries. Let’s try it on our / directory:

sh -c 'i=97; while [ $i -le 122 ]; do o=`printf %o $i`; l=`printf "%b " "$l\\\\$o"`; i=$((i+1)); done; for a in $l; do for b in $l; do for c in $l; do if [ -e "/$a$b$c" ]; then printf "$a$b$c\n"; fi; done; done; done;'

Yes, we can try it directly: make 2 new regular files named “asd” & ztr” in a new dir then remove read permission of your new dir. Now you cannot know what files are in your dir; you can't list them.

for fn in *; do printf "$fn\n"; done

gives only * for now, you cannot list directly your directory.

But if you test if with your "brute force" in the way above, then you can find out what files are in the top_secret_directory!! :)

Manage more data with the power of eval command[szerkesztés]

In the next chapters we will use more data with different, complex methods.

Thus, for this we have to learn the powerful eval command that allow us to manage more data.

One most typical way to manage more data is using arrays indexed numerically.

The standard shell doesn’t handle indexed arrays, because the Standard – the ISO 945 – doesn’t specify indexed arrays.

But of course in the standard shell we can use pseudo indexed arrays – “PSIAs” – with the eval command.

See the background – and the tricky way.

Background: we need the evaluation of a recursive command process. The eval command first re-parses all its arguments to get final form of a command THEN executes it.

Let’s generate a pseudo indexed array of the squares of the first 12345 natural numbers:

sh -c 'i=1; while [ $i -le 12345 ]; do eval l$i=$((i*i)); i=$((i+1)); done; printf "$l12345\n"'

While our program executes we manage no lesser than 12345 variable in the memory of our gigantic & powerful computer! Yay! :)

And finally run the Code below:

sh -c 'a=35; s="–––"; sl="0.04"; printf "\e[2J\e[2H\e[10B\e[$aC$s$s"; sleep $sl; i=1; while [ $i -le 14 ]; do printf "\r\e[2K\e[$(($a-i))C$s\e[$((i*2))C$s"; sleep $sl; i=$((i+1)); done; printf "\r\e[10B"'

)

Test #4[szerkesztés]

1: Write a program that gets random values from a pseudo indexed array by a loop and uses the printf to write them

2: Write a program that moves 2 objects on the screen