======October Lessons Learned (Part 2)====== =====Entries===== ====Entry 1: October 3, 2012==== Today we went over the **numeric based For loop** and we spent all class on it. Here's an example that I've somewhat broken down: for((i=0;i<6;i++));do * the **i** is the **looping variable** * the **0** is the **starting value** * the **6** is the end variable or looping as long as * the ++ indicates it will advance i by +1 or for short **i = i + 1** * Similarly, **i+ = 2** means i will advance by +2 or for short **i = i + 2** * **i*=2** means it will advance multiplied by 2 Here is a sample script we created: echo -n "Enter a number: " read number echo -n "How many times: " read times for((i=$number;i=$times;i++));do let x=i*i echo "[$i] $x" done We then were introduced to a few new commands such as **cut**, **sort**, and **uniq**. * **cut** is used to cut away redundant information from a list such as the output of ls -l. You can use **cut -d ' ' F1** where F1 indicates the first side or the left side of the cut where the space is. Cut can also cut specific characters in the list by doing cut -d -f -c 20-24, 56-60 * **sort** took the listed information and sorted or put remaining redundant information together. * **uniq** polished the output of redundant information and showed all unique information in the list. We then went over the **list based for loop** or in other words, a loop that is typed out as a command/incantation id username for user in echo -n "[$user]" id $user ====Entry 2: October 10, 2012==== This day I thought it was Monday so I wasn't actually around for the lecture, but here's what someone else learned. **mkdir -p** * the -p is **parent folder nesting**, which when used with mkdir creates multiple directories accordingly to the absolute or relative path given, creating folders that do not exist as well. For Example! lab46:~/closet$ mkdir -p parent/child/seamen lab46:~/closet$ ls a_used_napkin login parent ricky seplogin cabinet loosemoose parentchildseamen script2 skeleton cake math pirate_swag script3 thelab lab46:~/closet$ cd parent lab46:~/closet/parent$ ls child lab46:~/closet/parent$ cd child lab46:~/closet/parent/child$ ls seamen There was also a script that was created.. I'll just put that down here and interpret it later. The ^ is a reg function #!/bin/bash # name ="`cat ${0}.conf|grep '^name='|cut -d '=' -f2 2>/dev/null`" if [-z "${name}"]; then echo -n "Who are you?" read name echo "Pleased to meet you $name" echo "name=$name">${0}.conf else echo "Welcome back, ${name}" fi **exit1**: exit/return value as false **exit0**: exit/return value ====Entry 3: October 12, 2012==== On this day, we discussed Programming Paradigms and several types. Here's a bunch, ===Structured/Imperative/Procedural=== * lua * Java * Assembly * Paskel * PHP * Python * C * Bash * Visual Basic ===Functional=== Lisp - great for lists ===Logical=== Prolog and Huskel, focuses on **as or** and **nots** ===Object Oriented=== * C++, Java, Python, Eiffel, Small Talk ====Entry 4: October 24th, 2012==== On this October day, we discussed a few common debugging strategies in reference to puzzlebox2, I have created a complete Puzzlebox2 guide way down below that entails much of the debugging strategies. We also discussed.. ===Regular Expressions!=== * Here's a bunch of them listed! * ^ match beginning of line * $ match end of line * \< match beginning of word * \> match end of word * . match any single character * * zero or more of previous * [] character class match any of the enclosed * [^ ] inverted " " do not match any of the enclosed * () grouping * | or * + match 1 or more of the previous =====Keywords===== {{page>unixpart2&nofooter}} =====Kahmayhamayha!===== ====Question==== Can you create an incantation that is capable of make a directory, change into directory, creating two files, creating input to output into said files, rename them, change out of directory, archive it, and then compress it AND THEN decompress and extract them and **cat** them both as a sign of completion. And if this works, to make this better, make the incantation an application/program. ====Resources==== In class lectures and the O'Reilly Unix book. ====Hypothesis==== The incantation itself may be correct and will execute but I feel like a runtime error is possible. Possible problems could be when creating the two files and redirecting output into them and where compression and decompress will happen, as they are back to back. ====Experiment==== First, I'm going to do everything the normal way, normal as in typing each step out in seperate commands to get a feel for how it will look and ensuring I can assume file names when I tar and gzip. After doing that, I'll create the incantation itself and if it runs correctly, I will adjust it to be outputted into a file named "MobileCommandCenter" that will be the executable of this incantation, then I'll try running it in my practice area, then I'll move MobileCommandCenter into a seperate practice directory and do it again for consistency. ====Data==== lab46:~/closet/thelab/Experiment2/Practice2$ mkdir directory ; cd directory ; touch file1 file2 ; ls -l /home/$USER > file1 ; ls / > file2 ; mv file1 listhome ; mv file2 listroot ; cd .. ; tar cf directory.tar directory ; gzip directory.tar ; rm -rf directory ; gzip -d directory.tar.gz ; tar xf directory.tar ; cd directory ; cat listhome listroot > completion Discovered the difference between the | and the ;. Where the | expects output and applies it to the next command, the semicolon ; just does the command on the left and simply moves onto the next without the output carrying over. lab46:~/closet/thelab/Experiment2/Practice2/directory$ echo 'echo `mkdir directory ; cd directory ; touch file1 file2 ; ls -l /home/$USER > file1 ; ls / > file2 ; mv file1 listhome ; mv file2 listroot ; cd .. ; tar cf directory.tar directory ; gzip directory.tar ; rm -rf directory ; gzip -d directory.tar.gz ; tar xf directory.tar ; cd directory ; cat listhome listroot > completion`' > MobileCommandCenter lab46:~/closet/thelab/Experiment2$ ./MobileCommandCenter lab46:~/closet/thelab/Experiment2$ ls MobileCommandCenter directory directory.tar lab46:~/closet/thelab/Experiment2$ cd directory lab46:~/closet/thelab/Experiment2/directory$ ls completion listhome listroot lab46:~/closet/thelab/Experiment2/directory$ cat completion total 4 drwxr-xr-x 2 escoute1 lab46 6 Sep 5 14:50 Desktop drwxr-xr-x 2 escoute1 lab46 6 Sep 5 14:50 Documents drwxr-xr-x 2 escoute1 lab46 21 Sep 5 15:07 Downloads lrwxrwxrwx 1 escoute1 lab46 18 Sep 2 12:17 Maildir -> /var/mail/escoute1 drwxr-xr-x 2 escoute1 lab46 6 Sep 5 14:50 Music drwxr-xr-x 2 escoute1 lab46 6 Sep 5 14:50 Pictures drwxr-xr-x 2 escoute1 lab46 6 Sep 5 14:50 Public drwxr-xr-x 2 escoute1 lab46 6 Sep 5 14:50 Templates drwxr-xr-x 2 escoute1 lab46 6 Sep 5 14:50 Videos drwxr-x--x 3 escoute1 lab46 79 Sep 21 14:32 archives drwxr--r-- 4 escoute1 lab46 4096 Oct 3 16:21 closet lrwxrwxrwx 1 escoute1 lab46 28 Sep 7 11:25 data -> /usr/local/etc/data/escoute1 drwxr-x--- 3 escoute1 lab46 38 Oct 3 13:39 naming drwx-----x 2 escoute1 lab46 6 Aug 26 2009 public_html drwx------ 6 escoute1 lab46 84 Sep 28 16:52 src bin boot dev etc home initrd.img lib lib32 lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var vmlinuz ====Analysis==== Based on the data collected: * I was wrong to assume that the incantation would "trip over itself" or shoot off commands too fast. * The incantation generated the results I was only half expecting and I wish I could apply this further into a script, complete with options and actually useful data instead of just listings, possible Experiment 3? ====Conclusions==== As long as you can supply the semicolon and pipe properly, you can make your incantations as long as you please, in theory! =====Projects===== ====PuzzleBox2==== First we started with a file dubbed **minecraft.jar** but do not be fooled, this is not what it seems! NO MINECRAFT FOR YOU! Anyway so we check to see what file type this really is using the **file** command and are returned with information regarding the file. lab46:~/closet/thelab/puzzlebox2/Opus$ file minecraft.jar minecraft.jar: 7-zip archive data, version 0.3 As you can see, it tells us of a **7-zip archive** file so that must mean that this file is expected to have a **.7z** file extension in order to be operated upon correctly. So we use the **mv** command to rename the **minecraft.jar** file to **minecraft.7z** and use **ls** to double check that it is indeed what we want. lab46:~/closet/thelab/puzzlebox2/Opus$ mv minecraft.jar minecraft.7z lab46:~/closet/thelab/puzzlebox2/Opus$ ls minecraft.7z Now we need to get the content out of this 7zip archive using the **7za** command with the **e** option. lab46:~/closet/thelab/puzzlebox2/Opus$ 7za e minecraft.7z 7-Zip (A) 9.04 beta Copyright (c) 1999-2009 Igor Pavlov 2009-05-30 p7zip Version 9.04 (locale=en_US,Utf16=on,HugeFiles=on,2 CPUs) Processing archive: minecraft.7z Extracting ls -l/result Extracting ls -l Everything is Ok Folders: 1 Files: 1 Size: 878 Compressed: 765 lab46:~/closet/thelab/puzzlebox2/Opus$ ls ls -l minecraft.7z result As you can see, we now have a directory known as **ls -l** and a **result** file. The directory has nothing in it, perhaps some sort of hint, I dont even know but the main show is in the **return** file. Lets **cat** this file. lab46:~/closet/thelab/puzzlebox2/Opus$ cat result #!/bin/b@ntehuiOUXUXUX@ash # # result - s@OUEUEIPYEDEYP@cript to finish up the task # echo -n "[p@OEUIDYPYDD@uzzlebox] " if [ -x "${0@DPY445dyp.pd@}" ]; then echo -n @EUEOUEDUIDUIDIUED@"1 " if [ -L @EIDUIDIUDUIEDUID@"${0}" ] && [ "${0#**/}" = "solution" ]; then echo@UIDUIDIUDUIDUIEDUI@ -n "2 " if [@%>PI$F%DYP$F%DYPF$%@ "`tail -1 ${0} | grep \"^#${USER}:.*$\" | wc -l`" -eq 1 ]; then @EUIOUIPD>YDYPH%@echo -n "YES, here is your token: " @IDD6d64464665hyph@md5sum "${0}" | cut -d' ' -f1 else@uideuydtnhiud@ @uidiuedud@echo "Very close." @iudiudiudiudui@exit 1 fi@uiduiduidiud@ else@uiduiduiduidiu@ echo@thndhtndthnth@ "Closer." exit@ngyncnycgcgcg@ 1 fi@dhni887ngccg@ else@gf8y778utguddu@ echo "No@uidhu7h6hf@t yet." exit 1@h7pp6h6h6h7@ fi@fhy890idu908@ exit 0@iuedidiuduid@ #username:@crg56rdhinthntiu@Fill out the previous field as appropriate Holy craziness batman! Look at all that junk, what's a pattern however? Notice the @ signs and that in every instance of the @ sign, we have a bunch of junk. So we want to just remove all of the content inside these @ signs and the signs themselves in **vi**. **result** should now look like this when **cat**ed, I'm displaying it in script as it is going to be a script file of sorts. #!/bin/bash # # result - script to finish up the task # echo -n "[puzzlebox] " if [ -x "${0}" ]; then echo -n "1 " if [ -L "${0}" ] && [ "${0#**/}" = "solution" ]; then echo -n "2 " if [ "`tail -1 ${0} | grep \"^#${USER}:.*$\" | wc -l`" -eq 1 ]; then echo -n "YES, here is your token: " md5sum "${0}" | cut -d' ' -f1 else echo "Very close." exit 1 fi else echo "Closer." exit 1 fi else echo "Not yet." exit 1 fi exit 0 #username:Fill out the previous field as appropriate As you can see, we can easily recognize this as a script of some sort, waiting to be executed. But the file is a "normal readable file", what do we do? Let's do the **chmod** command followed with **744 result** to activate Owner's Executable with Read and Write, while Groups and World remain at Read. Now that our result file is now executable, we now run it with ./result (assuming you are in the directory it is in. lab46:~/closet/thelab/puzzlebox2/Opus$ chmod 744 result lab46:~/closet/thelab/puzzlebox2/Opus$ ls ls -l minecraft.7z result lab46:~/closet/thelab/puzzlebox2/Opus$ ./result [puzzlebox] 1 Closer. Our new **result** script seems to stop at our second if statement, let's disect what might be the cause of the intrusion, as we definately assume we have not reached our end with this puzzlebox. I pose the first question that you should wonder right away: What does the **${0}** stand for? What does it mean? Well, take note that the **$** indicates it is a **variable.** To what value or string is it? How do we find this out you may ask? Why not, **echo** our variables to see what they hide! Be sure to enter them before any if statement in order to prevent some issues. Of course, we edit in vi. ALSO notice that there are two variables. lab46:~/closet/thelab/puzzlebox2/Opus$ vi result lab46:~/closet/thelab/puzzlebox2/Opus$ cat result #!/bin/bash # # result - script to finish up the task # echo -n "[puzzlebox] " echo -n "${0} " echo -n "${0#**/} " if [ -x "${0}" ]; then echo -n "1 " if [ -L "${0}" ] && [ "${0#**/}" = "solution" ]; then echo -n "2 " if [ "`tail -1 ${0} | grep \"^#${USER}:.*$\" | wc -l`" -eq 1 ]; then echo -n "YES, here is your token: " md5sum "${0}" | cut -d' ' -f1 else echo "Very close." exit 1 fi else echo "Closer." exit 1 fi else echo "Not yet." exit 1 fi exit 0 #username:Fill out the previous field as appropriate And we run result once again.. lab46:~/closet/thelab/puzzlebox2/Opus$ ./result [puzzlebox] ./result result 1 Closer. Ah now that is interesting! We have been returned a **(dot)/result** and a **result** for our output along with the resulting **if statement's** output. That must mean that our variable {0} is the executable version of result, whilst our other variable is the file itself. Now lets look at where we are getting our 1 Closer. Here's a better view of what **if statement** we are speaking of. if [ -L "${0}" ] && [ "${0#**/}" = "solution" ]; then echo -n "2 " Assuming you've done some research on the [ using the **man**, we would recognize the -L as a symbolic link. So the first part is **a symbolic link of the executable result**. The **&&** indicates an **AND** operator, indicating this **symbolic link is to fulfill both the executable and the file itself based on the second variable**, and then we see an **= "solution"**, meaning it is looking for a symbolic link that is named solution that points to the result data file. With all that in mind, what do you think we should do? CREATE a symbolic link! (AYUAYAYAHAY!) We do this using **ln -s [TARGET FILE] [SYMBOLIC LINK NAME]** then we shall run our new symbolic link **solution** lab46:~/closet/thelab/puzzlebox2/Opus$ ln -s result solution lab46:~/closet/thelab/puzzlebox2/Opus$ ls ls -l minecraft.7z result solution lab46:~/closet/thelab/puzzlebox2/Opus$ ./solution [puzzlebox] ./solution solution 1 2 Very close. NOTICE that our variable's values have changed in this instance? The symbolic link simply replaces the return name in all instances when ran from the symbolic link solution, so any variable that means to point at result is pointing at solution (or what I'd call, pseudo-result). Lets move onto our next and last if statement to our final prize! if [ "`tail -1 ${0} | grep \"^#${USER}:.*$\" | wc -l`" -eq 1 ]; then echo -n "YES, here is your token: " md5sum "${0}" | cut -d' ' -f1 The only part of this if statement we need to focus on is what tail -1 means. What does it indicate? It indicates or points to the end of the file result(or solution). So why don't we look at the end? #username:Fill out the previous field as appropriate Seems meaningless, but read what it says. We need to fill out the field(placeholder username) with our **username**, more specifically whatever username is connected to your personalized **$USER**, we figure this from the if statement itself where it **grep**s for the **$USER**. So for this scenario, let's use **jackrabbits** as the username, just remember to use YOUR username or whatever is logged in when running this program. #jackrabbits:Fill out the previous field as appropriate Now that we filled that out, lets run our symbolic link **solution**. lab46:~/closet/thelab/puzzlebox2/Opus$ ./solution [puzzlebox] 1 2 YES, here is your token: 0a9563f07993a814e860c2c616db0413 Viola! The puzzlebox is solved! Congrats! I helped you cheat!