Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Saturday, 14 December 2013

Learn Ruby Language and Be an exploit coder-21


Functions Can Return Something

You have been using the = character to name variables and set them to numbers or strings. We're now going to blow your mind again by showing you how to use = to set variables to be a value from a function. There will be one thing to pay close attention to, but first type this in.

  • def add(a, b)
  •   puts "ADDING #{a} + #{b}"
  •   a + b
  • end

  • def subtract(a, b)
  •   puts "SUBTRACTING #{a} - #{b}"
  •   a - b
  • end

  • def multiply(a, b)
  •   puts "MULTIPLYING #{a} * #{b}"
  •   a * b
  • end

  • def divide(a, b)
  •   puts "DIVIDING #{a} / #{b}"
  •   a / b
  • end

  • puts "Let's do some math with just functions!"

  • age = add(30, 5)
  • height = subtract(78,4)
  • weight = multiply(90, 2)
  • iq = divide(100, 2)

  • puts "Age: #{age}, Height: #{height}, Weight: #{weight}, IQ: #{iq}"

  • # A puzzle for the extra credit, type it in anyway.
  • puts "Here is a puzzle."

  • what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

  • puts "That becomes: #{what} Can you do it by hand?"

We are now doing our own math functions for add, subtract, multiply, and divide. The important thing to notice is the last line where we say a + b (in add). What this does is the following.



  1. Our function is called with two arguments: a and b.
  2. We print out what our function is doing, in this case "ADDING".
  3. Then we tell Ruby to do something kind of backward: we return the addition of a + b. You might say this as, "I add a and b then return them." In Ruby, the last evaluated statement in a method is its return value. You can be more explicit if you want and type return a + b, but that is totally optional.
  4. Ruby adds the two numbers. Then when the function ends any line that runs it will be able to assign this a + b result to a variable.


As with many other things in this book, you should take this real slow, break it down and try to trace what's going on. To help there's extra credit to get you to solve a puzzle and learn something cool.

What You Should See.




  • $ ruby exploit21.rb
  • Let's do some math with just functions!
  • ADDING 30 + 5
  • SUBTRACTING 78 - 4
  • MULTIPLYING 90 * 2
  • DIVIDING 100 / 2
  • Age: 35, Height: 74, Weight: 180, IQ: 50
  • Here is a puzzle.
  • DIVIDING 50 / 2
  • MULTIPLYING 180 * 25
  • SUBTRACTING 74 - 4500
  • ADDING 35 + -4426
  • That becomes:  -4391 Can you do it by hand?


Extra Credit.



  1. If you aren't really sure what return values are, try writing a few of your own functions and have them return some values. You can return anything that you can put to the right of an =.
  2. At the end of the script is a puzzle. I'm taking the return value of one function, and using it as the argument of another function. I'm doing this in a chain so that I'm kind of creating a formula using the functions. It looks really weird, but if you run the script you can see the results. What you should do is try to figure out the normal formula that would recreate this same set of operations.
  3. Once you have the formula worked out for the puzzle, get in there and see what happens when you modify the parts of the functions. Try to change it on purpose to make another value.
  4. Finally, do the inverse. Write out a simple formula and use the functions in the same way to calculate it.



This exercise might really whack your brain out, but take it slow and easy and treat it like a little game. Figuring out puzzles like this is what makes programming fun, so I'll be giving you more little problems like this as we go.

Friday, 13 December 2013

Learn Ruby Language and Be an exploit coder-20


Functions And Files

Remember your checklist for functions, then do this exercise paying close attention to how functions and files can work together to make useful stuff.

  • input_file = ARGV[0]

  • def print_all(f)
  •   puts f.read()
  • end

  • def rewind(f)
  •   f.seek(0, IO::SEEK_SET)
  • end

  • def print_a_line(line_count, f)
  •   puts "#{line_count} #{f.readline()}"
  • end

  • current_file = File.open(input_file)

  • puts "First let's print the whole file:"
  • puts # a blank line

  • print_all(current_file)

  • puts "Now let's rewind, kind of like a tape."

  • rewind(current_file)

  • puts "Let's print three lines:"

  • current_line = 1
  • print_a_line(current_line, current_file)

  • current_line = current_line + 1
  • print_a_line(current_line, current_file)

  • current_line = current_line + 1
  • print_a_line(current_line, current_file)


Pay close attention to how we pass in the current line number each time we run print_a_line.

What You Should See



  • $ ruby exploit20.rb test.txt
  • First let's print the whole file:

  • To all the people out there.
  • I say I don't like my hair.
  • I need to shave it off.

  • Now let's rewind, kind of like a tape.
  • Let's print three lines:
  • 1 To all the people out there.
  • 2 I say I don't like my hair.
  • 3 I need to shave it off.



Extra Credit.



Go through and write English comments for each line to understand what's going on.
Each time print_a_line is run you are passing in a variable current_line. Write out what current_line is equal to on each function call, and trace how it becomes line_count in print_a_line.
Find each place a function is used, and go check its def to make sure that you are giving it the right arguments.
Research online what the seek function for file does. Look at the rdoc documentation using the ri command and see if you can figure it out from there.

Research the shorthand notation += and rewrite the script to use that.

Learn Ruby Language and Be an exploit coder-19


Functions And Variables

Functions may have been a mind-blowing amount of information, but do not worry. Just keep doing these exercises and going through your checklist from the last exercise and you will eventually get it.

There is one tiny point though that you might not have realized which we'll reinforce right now: The variables in your function are not connected to the variables in your script. Here's an exercise to get you thinking about this.

  • def cheese_and_crackers(cheese_count, boxes_of_crackers)
  •   puts "You have #{cheese_count} cheeses!"
  •   puts "You have #{boxes_of_crackers} boxes of crackers!"
  •   puts "Man that's enough for a party!"
  •   puts "Get a blanket."
  •   puts # a blank line
  • end

  • puts "We can just give the function numbers directly:"
  • cheese_and_crackers(20, 30)

  • puts "OR, we can use variables from our script:"
  • amount_of_cheese = 10
  • amount_of_crackers = 50
  • cheese_and_crackers(amount_of_cheese, amount_of_crackers)

  • puts "We can even do math inside too:"
  • cheese_and_crackers(10 + 20, 5 + 6)

  • puts "And we can combine the two, variables and math:"
  • cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)


This shows all different ways we're able to give our function cheese_and_crackers the values it needs to print them. We can give it straight numbers. We can give it variables. We can give it math. We can even combine math and variables.

In a way, the arguments to a function are kind of like our = character when we make a variable. In fact, if you can use = to name something, you can usually pass it to a function as an argument.


What You Should See


You should study the output of this script and compare it with what you think you should get for each of the examples in the script.


  • $ ruby exploit19.rb
  • We can just give the function numbers directly:
  • You have 20 cheeses!
  • You have 30 boxes of crackers!
  • Man that's enough for a party!
  • Get a blanket.

  • OR, we can use variables from our script:
  • You have 10 cheeses!
  • You have 50 boxes of crackers!
  • Man that's enough for a party!
  • Get a blanket.

  • We can even do math inside too:
  • You have 30 cheeses!
  • You have 11 boxes of crackers!
  • Man that's enough for a party!
  • Get a blanket.

  • And we can combine the two, variables and math:
  • You have 110 cheeses!
  • You have 1050 boxes of crackers!
  • Man that's enough for a party!
  • Get a blanket.
  • $


Extra Credit



Go back through the script and type a comment above each line explaining in English what it does.
Start at the bottom and read each line backwards, saying all the important characters.

Write at least one more function of your own design, and run it 10 different ways.

Thursday, 12 December 2013

Learn Ruby Language and Be an exploit coder-18


Names, Variables, Code, Functions

Big title right? I am about to introduce you to the function! Dum dum dah! Every programmer will go on and on about functions and all the different ideas about how they work and what they do, but I will give you the simplest explanation you can use right now.

Functions do three things.

  1. They name pieces of code the way variables name strings and numbers.
  2. They take arguments the way your scripts take ARGV.
  3. Using #1 and #2 they let you make your own "mini scripts" or "tiny commands".

You can create a function (also called "methods") by using the word def in Ruby. I'm going to have you make four different functions that work like your scripts, and then show you how each one is related.

  • # this one is like your scripts with argv
  • def puts_two(*args)
  •   arg1, arg2 = args
  •   puts "arg1: #{arg1}, arg2: #{arg2}"
  • end

  • # ok, that *args is actually pointless, we can just do this
  • def puts_two_again(arg1, arg2)
  •   puts "arg1: #{arg1}, arg2: #{arg2}"
  • end

  • # this just takes one argument
  • def puts_one(arg1)
  •   puts "arg1: #{arg1}"
  • end

  • # this one takes no arguments
  • def puts_none()
  •   puts "I got nothin'."
  • end

  • puts_two("InnOxent","StoKer")
  • puts_two_again("InnOxent","StoKer")
  • puts_one("First!")
  • puts_none()

Let's break down the first function, puts_two which is the most similar to what you already know from making scripts:


  1. First we tell Ruby we want to make a function using def for "define".
  2. On the same line as def we then give the function a name, in this case we just called it "puts_two" but it could be "peanuts" too. It doesn't matter, except that your function should have a short name that says what it does.
  3. Then we tell it we want *args (asterisk args) which is a lot like your ARGV parameter but for functions.
  4. After the definition, all the lines that are indented 2 spaces will become attached to this name, puts_two. Our first indented line is one that unpacks the arguments the same as with your scripts.
  5. To demonstrate how it works we print these arguments out, just like we would in a script. Now, the problem with puts_two is that it's not the easiest way to make a function. In Ruby we can skip the whole unpacking args and just use the names we want right inside (). That's what puts_two_again does.


After that you have an example of how you make a function that takes one argument in puts_one.

Finally you have a function that has no arguments in puts_none.

Warning

This is very important. Do not get discouraged right now if this doesn't quite make sense. We're going to do a few exercises linking functions to your scripts and show you how to make more. For now just keep thinking "mini script" when I say "function" and keep playing with them.

What You Should See


If you run the above script you should see:


  • $ ruby exploit18.rb
  • arg1: 'InnOxent', arg2: 'StoKer'
  • arg1: 'InnOxent', arg2: 'StoKer'
  • arg1: 'First!'
  • I got nothin'.
  • $


right away you can see how a function works. Notice that you used your functions the way you use things like File.exists?, File.open, and other "commands". In fact, I've been tricking you because in Ruby those "commands" are just functions. This means you can make your own commands and use them in your scripts too.

Extra Credit


Write out a function checklist for later exercises. Write these on an index card and keep it by you while you complete the rest of these exercises or until you feel you do not need it:


  1. Did you start your function definition with def?
  2. Does your function name have only characters and _ (underscore) characters?
  3. Did you put an open parenthesis ( right after the function name?
  4. Did you put your arguments after the parenthesis ( separated by commas?
  5. Did you make each argument unique (meaning no duplicated names).
  6. Did you put a close parenthesis ) after the arguments?
  7. Did you indent all lines of code you want in the function 2 spaces?
  8. Did you close your function body by typing "end"?


And when you run (aka "use" or "call") a function, check these things:


  1. Did you call/use/run this function by typing its name?
  2. Did you put ( character after the name to run it? (this isn't required, but is idiomatic)
  3. Did you put the values you want into the parenthesis separated by commas?
  4. Did you end the function call with a ) character.


Use these two checklists on the remaining lessons until you do not need them anymore.

Finally, repeat this a few times:

"To 'run', 'call', or 'use' a function all mean the same thing."

Wednesday, 11 December 2013

Learn Ruby Language and Be an exploit coder-17



More Files

Now let's do a few more things with files. We're going to actually write a Ruby script to copy one file to another. It'll be very short but will give you some ideas about other things you can do with files.


  • from_file, to_file = ARGV 
  • script = $0
  • puts "Copying from #{from_file} to #{to_file}"
  • # we could do these two on one line too, how?
  • input = File.open(from_file)
  • indata = input.read()
  • puts "The input file is #{indata.length} bytes long"
  • puts "Does the output file exist? #{File.exists? to_file}"
  • puts "Ready, hit RETURN to continue, CTRL-C to abort."
  • STDIN.gets
  • output = File.open(to_file, 'w')
  • output.write(indata)
  • puts "Alright, all done."
  • output.close()
  • input.close()
Here we used a new method called File.exists?. This returns true if a file exists, based on its name in a string as an argument. It returns false if not. We'll be using this function in the second half of this book to do lots of things.

What You Should See.

Just like your other scripts, run this one with two arguments, the file to copy from and the file to copy it to. If we use your test.txt file from before we get this.

  • $ ruby exploit17.rb test.txt copied.txt
  • Copying from test.txt to copied.txt
  • The input file is 81 bytes long
  • Does the output file exist? False
  • Ready, hit RETURN to continue, CTRL-C to abort.

  • Alright, all done.

  • $ cat copied.txt
  • To all the people out there.
  • I say I don't like my hair.
  • I need to shave it off.
  • $

It should work with any file. Try a bunch more and see what happens. Just be careful you do not blast an important file.

Warning.

you see that trick I did with cat? It only works on Linux or OSX, on Windows use type to do the same thing.

Extra Credit.

Go read up on Ruby's require statement, and start Ruby to try it out. Try importing some things and see if you can get it right. It's alright if you do not.
This script is really annoying. There's no need to ask you before doing the copy, and it prints too much out to the screen. Try to make it more friendly to use by removing features.
See how short you can make the script. I could make this 1 line long.
Notice at the end of the WYSS I used something called cat? It's an old command that "con*cat*enates" files together, but mostly it's just an easy way to print a file to the screen. Type man cat to read about it.
Windows people, find the alternative to cat that Linux/OSX people have. Do not worry about man since there is nothing like that.
Find out why you had to do output.close() in the code.


Monday, 9 December 2013

Learn Ruby Language and Be an exploit coder-15

Reading Files


Everything you've learned about STDIN.gets and ARGV is so you can start reading files. You may have to play with this exercise the most to understand what's going on, so do it carefully and remember your checks. Working with files is an easy way to erase your work if you are not careful.

This exercise involves writing two files. One is your usual exploit15.rb file that you will run, but the other is named exploit15_sample.txt. This second file isn't a script but a plain text file we'll be reading in our script. Here are the contents of that file:

This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

What we want to do is "open" that file in our script and print it out. However, we do not want to just "hard code" the name exploit15_sample.txt into our script. "Hard coding" means putting some bit of information that should come from the user as a string right in our program. That's bad because we want it to load other files later. The solution is to use ARGV and STDIN.gets to ask the user what file they want instead of "hard coding" the file's name.

filename = ARGV.first
prompt = "> "
txt = File.open(filename)
puts "Here's your file: #{filename}"
puts txt.read()
puts "I'll also ask you to type it again:"
print prompt
file_again = STDIN.gets.chomp()
txt_again = File.open(file_again)
puts txt_again.read()

A few fancy things are going on in this file, so let's break it down real quick:

Line 1-3 should be a familiar use of ARGV to get a filename and setting up the prompt. Next we have line 4 where we use a new command File.open. Right now, run ri File.open from the command line and read the instructions. Notice how like your own scripts, it takes a parameter and returns a value you can set to your own variable. You just opened a file.

Line 6 we print a little line, but on line 7 we have something very new and exciting. We call a function on txt. What you got back from open is a file, and it's also got commands you can give it. You give a file a command by using the . (dot or period), the name of the command, and parameters. Just like with File.open. The difference is that when you say txt.read() you are saying, "Hey txt! Do your read command with no parameters!"

The remainder of the file is more of the same, but we'll leave the analysis to you in the extra credit.

What You Should See.


I made a file called "exploit15_sample.txt" and ran my script.

$ ruby exploit15.rb exploit15_sample.txt
Here's your file exploit15_sample.txt:
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
I'll also ask you to type it again:
> exploit15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
$


Extra Credit



This is a big jump so be sure you do this extra credit as best you can before moving on.

Above each line write out in English what that line does.
If you are not sure ask someone for help or search online. Many times searching for "ruby THING" will find answers for what that THING does in Ruby. Try searching for "ruby file.open".
I used the name "commands" here, but they are also called "functions" and "methods". Search around online to see what other people do to define these. Do not worry if they confuse you. It's normal for a programmer to confuse you with their vast extensive knowledge.
Get rid of the part from line 9-15 where you use STDIN.gets and try the script then.
Use only STDIN.gets and try the script that way. Think of why one way of getting the filename would be better than another.
Run ri File and scroll down until you see the read() command (method/function). See all the other ones you can use? Try some of the other commands.
Startup IRB again and use File.open from the prompt. Notice how you can open files and run read on them right there?
Have your script also do a close() on the txt and txt_again variables. It's important to close files when you are done with them.

Learn Ruby Language and Be an exploit coder-13

Parameters, Unpacking, Variables

In this lesson we will cover one more input method you can use to pass variables to a script (script being another name for your .rb files). You know how you type ruby exploit13.rb to run the exploit13.rb file? Well the exploit13.rb part of the command is called an "argument". What we'll do now is write a script that also accepts arguments.

Type this program and I'll explain it in detail.
  • first, second, third = ARGV 

  • puts "The script is called: #{$0}"
  • puts "Your first variable is: #{first}"
  • puts "Your second variable is: #{second}"
  • puts "Your third variable is: #{third}"

The ARGV is the "argument variable", a very standard name in programming, that you will find used in many other languages. It's in all caps because it's a constant, meaning you shouldn't change the value once it's been assigned. This variable holds the arguments you pass to your Ruby script when you run it. In the exercises you will get to play with this more and see what happens.

Line 1 "unpacks" ARGV so that, rather than holding all the arguments, it gets assigned to three variables you can work with: first, second, and third. The name of the script itself is stored in a special variable $0, which we don't need to unpack. This may look strange, but "unpack" is probably the best word to describe what it does. It just says, "Take whatever is in ARGV, unpack it, and assign it to all of these variables on the left in order."

After that we just print them out like normal.

What You Should See.

Run the program like this.

  • ruby exploit13.rb first 2nd 3rd

 This is what you should see when you do a few different runs with different arguments.

  • $ ruby exploit13.rb first 2nd 3rd
  • The script is called: exploit13.rb
  • Your first variable is: first
  • Your second variable is: 2nd
  • Your third variable is: 3rd
  • $ ruby exploit13.rb cheese apples bread

  • The script is called: exploit13.rb
  • Your first variable is: cheese
  • Your second variable is: apples
  • Your third variable is: bread

  • $ ruby exploit13.rb InnoXent | StoKer
  • The script is called: exploit13.rb
  • Your first variable is: innoXent
  • Your second variable is: |.
  • Your third variable is: Stoker

You can actually replace "first", "2nd", and "3rd" with any three things. You do not have to give these parameters either, you can give any 3 strings you want.


  • ruby exploit13.rb stuff I like
  • ruby exploit13.rb anything 6 7 

Extra Credit

Try giving fewer than three arguments to your script. What values are used for the missing arguments?
Write a script that has fewer arguments and one that has more. Make sure you give the unpacked variables good names.
Combine STDIN.gets.chomp() with ARGV to make a script that gets more input from a user.

Learn Ruby Language and Be an exploit coder-12

Libraries


Hello buddies...

Take a look on this code...

  • require 'open-uri'

  • open("http://writingexploit.blogspot.com") do |f|
  •   f.each_line {|line| p line}
  •   puts f.base_uri         # <URI::HTTP:0x40e6ef2 URL:http://writingexploit.blogspot.com/>
  •   puts f.content_type     # "text/html"
  •   puts f.charset          # "iso-8859-1"
  •   puts f.content_encoding # []
  •   puts f.last_modified    # Thu Dec 05 02:45:02 UTC 2002
  • end

On line 1 we have what's called a "require". This is how you add features to your script from the Ruby feature set or other sources (e.g., Ruby Gems, stuff you wrote yourself). Rather than give you all the features at once, Ruby asks you to say what you plan to use. This keeps your programs small, but it also acts as documentation for other programmers who read your code later.

Hold Up! Features Have Another Name

I call them "features" here (these little things you require to make your Ruby program do more) but nobody else calls them features. I just used that name because I needed to trick you into learning what they are without jargon. Before you can continue, you need to learn their real name: libraries.

From now on we will be calling these "features" that we require libraries. I'll say things like, "You want to require the open-uri library." They are also called "modules" by other programmers, but let's just stick with libraries.

Extra Credit

Research the difference between require and include. How are they different?
Can you require a script that doesn't contain a library specifically?
Figure out which directories on your system Ruby will look in to find the libraries you require.

Sunday, 8 December 2013

Learn Ruby Language and Be an exploit coder-11

Asking Questions

Now it is time to pick up the pace. I have got you doing a lot of printing so that you get used to typing simple things, but those simple things are fairly boring. What we want to do now is get data into your programs. This is a little tricky because you have learn to do two things that may not make sense right away, but trust me and do it anyway. It will make sense in a few exercises.

Most of what software does is the following:
  1. Take some kind of input from a person.
  2. Change it.
  3. Print out something to show how it changed.
So far you have only been printing, but you haven't been able to get any input from a person, or change it. You may not even know what "input" means, so rather than talk about it, let's have you do some and see if you get it. Next lesson we'll do more to explain it.

  • print "How old are you? "
  • age = gets.chomp()
  • print "How tall are you? "
  • height = gets.chomp()
  • print "How much do you weigh? "
  • weight = gets.chomp()
  • puts "So, you're #{age} years old, #{height} tall and #{weight} heavy."

Note:

Notice that we are using print instead of puts to do the prompting. print doesn't add a new line automatically, so your answer can go on the same line as the question. puts on the other hand, adds a newline automatically.

What you should see.

  • $ ruby exploit11.rb
  • How old are you? 35
  • How tall are you? 6'2"
  • How much do you weigh?  180lbs
  • So, you're 35 old, 6'2" tall and 180lbs heavy.
  • $

Extra Credit

Go online and find out what Rubys gets and chomp methods do.
Can you find other ways to use gets.chomp? Try some of the samples you find.
Write another "form" like this to ask some other questions. 

Learn Ruby Language and Be an exploit coder-10

What Was That?


In Lesson 9 I threw you some new stuff, just to keep you on your toes. I showed you two ways to make a string that goes across multiple lines. In the first way, I put the characters \n (back-slash n) between the names of the months. What these two characters do is put a new line character into the string at that point.

This use of the \ (back-slash) character is a way we can put difficult-to-type characters into a string. There are plenty of these "escape sequences" available for different characters you might want to put in, but there's a special one, the double back-slash which is just two of them \\. These two characters will print just one back-slash. We'll try a few of these sequences so you can see what I mean.

Another important escape sequence is to escape a single-quote ' or double-quote ". Imagine you have a string that uses double-quotes and you want to put a double-quote in for the output. If you do this "I "understand" joe." then Ruby will get confused since it will think the " around "understand" actually ends the string. You need a way to tell Ruby that the " inside the string isn't a real double-quote.

To solve this problem you escape double-quotes and single-quotes so Ruby knows to include in the string. Here's an example:

"I am 6'2\" tall."  # escape double-quote inside string
'I am 6\'2" tall.'  # escape single-quote inside string
The second way is by using here document syntax, which uses <<NAME and works like a string, but you also can put as many lines of text you as want until you type NAME again. We'll also play with these.

  • tabby_cat = "\tI'm tabbed in."
  • persian_cat = "I'm split\non a line."
  • backslash_cat = "I'm \\ a \\ cat."
  • fat_cat = <<MY_HEREDOC
  • I'll do a list:
  • \t* Cat food
  • \t* Fishies
  • \t* Catnip\n\t* Grass
  • MY_HEREDOC
  • puts tabby_cat
  • puts persian_cat
  • puts backslash_cat
  • puts fat_cat


 What You Should See
Look for the tab characters that you made. In this exercise the spacing is important to get right.


  • $ ruby exploit10.rb
  •     I'm tabbed in.
  • I'm split
  • on a line.
  • I'm \ a \ cat.
  • I'll do a list:
  •     * Cat food
  •     * Fishies
  •     * Catnip
  •     * Grass
  • $

Extra Credit

Search online to see what other escape sequences are available.
Combine escape sequences and format strings to create a more complex format. 

Friday, 6 December 2013

Learn Ruby Language and Be an exploit coder-7

More Printing


Now we are going to do a bunch of exercises where you just type code in and make it run. I won't be explaining much since it is just more of the same. The purpose is to build up your chops. See you in a few exercises, and do not skip! Do not paste!

  • puts "Mary had a little lamb."
  • puts "Its fleece was white as %s." % 'snow'
  • puts "And everywhere that Mary went."
  • puts "." * 10  # what'd that do?

  • end1 = "C"
  • end2 = "h"
  • end3 = "e"
  • end4 = "e"
  • end5 = "s"
  • end6 = "e"
  • end7 = "B"
  • end8 = "u"
  • end9 = "r"
  • end10 = "g"
  • end11 = "e"
  • end12 = "r"

  • # notice how we are using print instead of puts here. change it to puts
  • # and see what happens.
  • print end1 + end2 + end3 + end4 + end5 + end6
  • print end7 + end8 + end9 + end10 + end11 + end12

  • # this just is polite use of the terminal, try removing it
  • puts

 What You Should See


  • $ ruby exploit7.rb
  • Mary had a little lamb.
  • Its fleece was white as snow.
  • And everywhere that Mary went.
  • ..........
  • CheeseBurger 

Extra Credit

For these next few exercises, you will have the exact same extra credit.

Go back through and write a comment on what each line does.
Read each one backwards or out loud to find your errors.
From now on, when you make mistakes write down on a piece of paper what kind of mistake you made.
When you go to the next exercise, look at the last mistakes you made and try not to make them in this new one.
Remember that everyone makes mistakes. Programmers are like magicians who like everyone to think they are perfect and never wrong, but it's all an act. They make mistakes all the time. 

Learn Ruby Language and Be an exploit coder-6

Strings And Text


While you have already been writing strings, you still do not know what they do. In this exercise we create a bunch of variables with complex strings so you can see what they are for. First an explanation of strings.

A string is usually a bit of text you want to display to someone, or "export" out of the program you are writing. Ruby knows you want something to be a string when you put either " (double-quotes) or ' (single-quotes) around the text. You saw this many times with your use of puts when you put the text you want to go to the string inside " or ' after the puts. Then Ruby displays it.

Strings may contain the format characters you have discovered so far. You simply put the formatted variables in the string, and then a % (percent) character, followed by the variable. The only catch is that if you want multiple formats in your string to print multiple variables, you need to put them inside [ ](brackets) separated by , (commas). It's as if you were telling me to buy you a list of items from the store and you said, "I want milk, eggs, bread, and soup." Only as a programmer we say, "[milk, eggs, bread, soup]".

Another way of injecting variables into your strings is to use something called "string interpolation", which uses the #{ } (pound and curly brace) characters. So, instead of using format strings:

name1 = "Joe"
name2 = "Mary"
puts "Hello %s, where is %s?" % [name1, name2]

We can type:

name1 = "Joe"
name2 = "Mary"
puts "Hello #{name1}, where is #{name2}?"

We will now type in a whole bunch of strings, variables, formats, and print them. You will also practice using short abbreviated variable names. Programmers love saving themselves time at your expense by using annoying cryptic variable names, so let's get you started being able to read and write them early on.


  • x = "There are #{10} types of people."
  • binary = "binary"
  • do_not = "don't"
  • y = "Those who know #{binary} and those who #{do_not}."

  • puts x
  • puts y

  • puts "I said: #{x}."
  • puts "I also said: '#{y}'."

  • hilarious = false
  • joke_evaluation = "Isn't that joke so funny?! #{hilarious}"

  • puts joke_evaluation

  • w = "This is the left side of..."
  • e = "a string with a right side."

  • puts w + e 

What You Should See .


There are 10 types of people.
Those who know binary and those who don't.
I said: There are 10 types of people..
I also said: 'Those who know binary and those who don't.'.
Isn't that joke so funny?! false
This is the left side of...a string with a right side.


Extra Credit

  1. Go through this program and write a comment above each line explaining it.
  2. Find all the places where a string is put inside a string. There are four places.
  3. Are you sure there's only four places? How do you know? Maybe I like lying.
  4. Explain why adding the two strings w and e with + makes a longer string.

Learn Ruby Language and Be an exploit coder-5

More Variables And Printing

Helow readers!

Now we'll do even more typing of variables and printing them out. This time we'll use something called a "format string". Every time you put " (double-quotes) around a piece of text you have been making a string. A string is how you make something that your program might give to a human. You print them, save them to files, send them to web servers, all sorts of things.

Strings are really handy, so in this exercise you will learn how to make strings that have variables embedded in them. You embed variables inside a string by using specialized format sequences and then putting the variables at the end with a special syntax that tells Ruby, "Hey, this is a format string, put these variables in there."

As usual, just type this in even if you do not understand it and make it exactly the same.

  • my_name = 'stoker'
  • my_age = 35 # not a lie
  • my_height = 74 # inches
  • my_weight = 180 # lbs
  • my_eyes = 'Blue'
  • my_teeth = 'White'
  • my_hair = 'Brown'

  • puts "Let's talk about %s." % my_name
  • puts "He's %d inches tall." % my_height
  • puts "He's %d pounds heavy." % my_weight
  • puts "Actually that's not too heavy."
  • puts "He's got %s eyes and %s hair." % [my_eyes, my_hair]
  • puts "His teeth are usually %s depending on the coffee." % my_teeth

  • # this line is tricky, try to get it exactly right
  • puts "If I add %d, %d, and %d I get %d." % [
  •     my_age, my_height, my_weight, my_age + my_height + my_weight]


What You Should See.


  • $ ruby exploit5.rb
  • Let's talk about Stoker.
  • He's 74 inches tall.
  • He's 180 pounds heavy.
  • Actually that's not too heavy.
  • He's got Blue eyes and Brown hair.
  • His teeth are usually White depending on the coffee.
  • If I add 35, 74, and 180 I get 289.
  • $





Extra Credit


  1. Change all the variables so there isn't the my_ in front. Make sure you change the name everywhere, not just where you used = to set them.
  2. Try more format sequences.
  3. Search online for all of the Ruby format sequences.
  4. Try to write some variables that convert the inches and pounds to centimeters and kilos. Do not just type in the measurements. Work out the math in Ruby.


Thursday, 5 December 2013

Learn Ruby Language and Be an exploit coder-4

Variables And Names

Hola Guyz!

Now you can print things with puts and you can do math. The next step is to learn about variables. In programming a variable is nothing more than a name for something so you can use the name rather than the something as you code. Programmers use these variable names to make their code read more like English, and because they have lousy memories. If they didn't use good names for things in their software, they'd get lost when they tried to read their code again.

If you get stuck with this exercise, remember the tricks you have been taught so far of finding differences and focusing on details:

  1. Write a comment above each line explaining to yourself what it does in English.
  2. Read your .rb file backwards.
  3. Read your .rb file out loud saying even the characters.
  • cars = 100
  • space_in_a_car = 4.0
  • drivers = 30
  • passengers = 90
  • cars_not_driven = cars - drivers
  • cars_driven = drivers
  • carpool_capacity = cars_driven * space_in_a_car
  • average_passengers_per_car = passengers / cars_driven
  • puts "There are #{cars} cars available."
  • puts "There are only #{drivers} drivers available."
  • puts "There will be #{cars_not_driven} empty cars today."
  • puts "We can transport #{carpool_capacity} people today."
  • puts "We have #{passengers} passengers to carpool today."
  • puts "We need to put about #{average_passengers_per_car} in each car."


Note
The _ in space_in_a_car is called an underscore character. Find out how to type it if you do not already know. We use this character a lot to put an imaginary space between words in variable names. 


What You Should See.


  • $ ruby exploit4.rb
  • There are 100 cars available.
  • There are only 30 drivers available.
  • There will be 70 empty cars today.
  • We can transport 120.0 people today.
  • We have 90 passengers to carpool today.
  • We need to put about 3 in each car.
  • $

Here's more extra credit:


  1. I used 4.0 for space_in_a_car, but is that necessary? What happens if it's just 4?
  2. Remember that 4.0 is a "floating point" number. Find out what that means.
  3. Write comments above each of the variable assignments.
  4. Make sure you know what = is called (equals) and that it's making names for things.
  5. Remember _ is an underscore character.
  6. Try running IRB as a calculator like you did before and use variable names to do your calculations. Popular variable names are also i, x, and j. 

Learn Ruby Language and Be an exploit coder-8

Printing, Printing

  • formatter = "%s %s %s %s"

  • puts formatter % [1, 2, 3, 4]
  • puts formatter % ["one", "two", "three", "four"]
  • puts formatter % [true, false, false, true]
  • puts formatter % [formatter, formatter, formatter, formatter]
  • puts formatter % [
  •     "I had this thing.",
  •     "That you could type up right.",
  •     "But it didn't sing.",
  •     "So I said goodnight."
  • ]

What You Should See.


  • $ ruby exploit8.rb
  • 1 2 3 4
  • one two three four
  • true false false true
  • %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
  • I had this thing. That you could type up right. But it didn't sing. So I said goodnight.

Extra Credit.

Do your checks of your work, write down your mistakes, try not to make them on the next exercise.

Learn Ruby Language and Be an exploit coder-9

Printing,Printing,Printing!

  • # Here's some new strange stuff, remember type it exactly.
  • days = "Mon Tue Wed Thu Fri Sat Sun"
  • months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
  • puts "Here are the days: ", days
  • puts "Here are the months: ", months
  • puts <<PARAGRAPH
  • There's something going on here.
  • With the PARAGRAPH thing
  • We'll be able to type as much as we like.
  • Even 4 lines if we want, or 5, or 6.
  • PARAGRAPH

What You Should See .



  • $ ruby exploit9.rb
  • Here are the days:
  • Mon Tue Wed Thu Fri Sat Sun
  • Here are the months:
  • Jan
  • Feb
  • Mar
  • Apr
  • May
  • Jun
  • Jul
  • Aug
  • There's something going on here.
  • With the PARAGRAPH thing
  • We'll be able to type as much as we like.
  • Even 4 lines if we want, or 5, or 6.

Extra Credit 


Do your checks of your work, write down your mistakes, try not to make them on the next exercise.

Learn Ruby Language and Be an exploit coder-3

Numbers And Math

Ain't it boring? well i promise that u guyz will enjot next article but u must have to learn basics first.
Lets move to our tutorial.

Every programming language has some kind of way of doing numbers and math. Do not worry, programmers lie frequently about being math geniuses when they really aren't. If they were math geniuses, they would be doing math, not writing ads and social network games to steal people's money.
This exercise has lots of math symbols. Let's name them right away so you know what they are called. As you type this one in, say the names. When saying them feels boring you can stop saying them. Here are the names:

  • + plus
  • - minus
  • / slash
  • * asterisk
  • % percent
  • < less-than
  • > greater-than
  • <= less-than-equal
  • >= greater-than-equal

Notice how the operations are missing? After you type in the code for this exercise, go back and figure out what each of these does and complete the table. For example, + does addition.

  • puts "I will now count my chickens:"

  • puts "Hens", 25 + 30 / 6
  • puts "Roosters", 100 - 25 * 3 % 4

  • puts "Now I will count the eggs:"

  • puts 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

  • puts "Is it true that 3 + 2 < 5 - 7?"

  • puts 3 + 2 < 5 - 7

  • puts "What is 3 + 2?", 3 + 2
  • puts "What is 5 - 7?", 5 - 7

  • puts "Oh, that's why it's false."

  • puts "How about some more."

  • puts "Is it greater?", 5 > -2
  • puts "Is it greater or equal?", 5 >= -2
  • puts "Is it less or equal?", 5 <= -2


What You Should See :


  • $ ruby exploit3.rb
  • I will now count my chickens:
  • Hens
  • 30
  • Roosters
  • 97
  • Now I will count the eggs:
  • 7
  • Is it true that 3 + 2 < 5 - 7?
  • false
  • What is 3 + 2?
  • 5
  • What is 5 - 7?
  • -2
  • Oh, that's why it's false.
  • How about some more.
  • Is it greater?
  • true
  • Is it greater or equal?
  • true
  • Is it less or equal?
  • false
  • $


Extra Credit.

Above each line, use the # to write a comment to yourself explaining what the line does.
Remember in First Lecture when you started IRB? Start IRB this way again and using the above characters and what you know, use Ruby as a calculator.
Find something you need to calculate and write a new .rb file that does it.
Notice the math seems "wrong"? There are no fractions, only whole numbers. Find out why by researching what a "floating point" number is.
Rewrite exploit3.rb to use floating point numbers so it's more accurate (hint: 20.0 is floating point).

Learn Ruby Language and Be an exploit coder-2




Comments And Pound Characters.


So hows u guyz? hope u'll be fine . Lets move to our next practice.
Comments are very important in your programs. They are used to tell you what something does in English, and they also are used to disable parts of your program if you need to remove them temporarily. Here's how you use comments in Ruby:

  • # A comment, this is so you can read your program later.
  • # Anything after the # is ignored by Ruby.

  • puts "I could have code like this." # and the comment after is ignored

  • # You can also use a comment to "disable" or comment out a piece of code:
  • # puts "This won't run."

  • puts "This will run."

What You Should See:

  • $ ruby exploit2.rb
  • I could have code like this.
  • This will run.
  • $

Extra Credit

Find out if you were right about what the # character does and make sure you know what it's called (octothorpe or pound character).
Take your exploit2.rb file and review each line going backwards. Start at the last line, and check each word in reverse against what you should have typed.
Did you find more mistakes? Fix them.
Read what you typed above out loud, including saying each character by its name. Did you find more mistakes? Fix them.