Hack to the Future

day two: the internet

{ social engineering }

  • We will give you one hour to finish your social engineering scheme presentation
  • Both teams will present their plans, and we will choose the one we think is best
    • Based on: quality and quantity of information, discreetness, lack of suspicion and is believable
  • The winning team gets to have their plan executed!

{ review exercise }

  • Using the terminal, complete these tasks:
    • Move your python_work directory from your Documents to your named folder (on your Desktop)
    • Create a new directory in your Documents named after your favorite food style
      • Create 3 files in that folder named after your favorite foods from that style
  • Create a Python file people.py and use concatenation/placeholders to make a string with a cohesive sentence stating the people you dislike

{ another exercise }

  • Create a list of your favorite memes, and then create another list of your favorite games (limit 5 each).
    • Create a third list, called "favorite_things" with three items in it.
      • The first and third elements of this list will be from your favorite memes, referencing the index of those memes
      •  The second item of this list will be one of the games, referencing the index of the games.
      • Print out the three lists

{ comments }

  • The # symbol comments out the line of code
  • The computer skips over it when compiling & executing, comments are purely for the user/human so they understand what's happening
  • Comment your code to make it easy to read!

{ if-statements }

  • if is followed by a 'condition' and a colon (:), and if the condition is met, the following block of code executes 
    • ex if age > 17:
      • print('you are too old!')

 

  • Conditional operators
    • ==, !=, >, <, >=, <=

{ white space }

  • White space is the white space between statements
  • The white space before statements inside of something is important!
  • A block of code is a group of statements, and the white space (tab) separate blocks of code
    • Consistent spacing
  • The 3 dots (. . . instead of >>>) in the interpreter means you're typing in a block of code
  • Blocks placed inside each other require extra spacing

{ white space }

this is block 1
this is block 1
      this is block 2
      this is block 2
      this is block 2
            this is block 3
            this is block 3
      this is block 2

      this is block 2
this is block 1
this is block 1

{ what else is there? }

  • If-statements can be extended with an if-then-else-statement
  • So, if the condition isn't met, it would execute a different block of code (if something is true, then do this, otherwise do that); ex:
  • >>> age = 18

    >>> if age == 12:

    . . .      print('Hello')

    . . .  else:

    . . .      print('Goodbye')

{ else-if-statements }

  • If-statements can be further extended using elif (else-if)
  • If the previous if-statement's condition isn't met (so it evaluates to False), then it check the else-if's condition, and so forth.

{ elif example }

>>> pets = 2

>>> if pets == 1:

. . .      print("You have 1 pet")

. . .  elif pets == 2:

. . .      print("You have 2 pets")

. . .  elif pets < 3:

. . .      print("You have less than 3 pets")

. . .  else:

. . .      print("You have more than 2 pets")

 

What will be printed?

{ combining conditions }

  • You can have multiple conditions in any conditional statements, by using and and or
  • >>> if age ==10 or age == 11 or age ==12:

    . . .       print("You are %s" % age)

  • If any of the conditions are met, then the code runs, hence the or
  • >>> if age >= 10 and age <= 12:

    . . .       print("You are %s" % age)

  • In this case, all of the conditions have to be true for the code to run, hence the and

{ none }

  • Another value that can be assigned to a variable is None 
  • This is an empty value, with nothing in it (also known as null or NIL in other languages)
  • None can be used to reset variables and make them unused
  • An example is if we don't want to print a message when a variable is empty
    • >>> money = None
    • >>> if money == None:
    • . . .      print("You have no money!")

{ exercise }

  • Imagine an alien was just shot down in a game. Create a variable called alien_color and assign it a value of 'green', 'yellow', or 'red'
    • Write an if statement to test whether the alien's color is green. If it is, print a message that the player just earned 5 points. If it isn't green, print a statement saying the player earned 10 points.

 

  • Write a sequence of conditional statements (or just one) that prints a number if it is equivalent to 20, 21, 22, 25, 26, 27, or 30)

{ geany }

  • Geany is an IDE (Integrated Development Environment)
    • Can write your code, compile, and run it all in the program!
    • Feature-filled
  • Open Geany (the lamp icon in your sidebar, or type geany [file.py] in your terminal.
  • Open a .py file you've created in this class in Geany
    • In the menu bar: Build > Set Build Commands
      • Change the "python" into "python3"
      • Compile command: python3 -m py_compile "%f"
      • Execute command: python3 "%f"
  • Run your Python code with F5

{ loops }

  • Repeatedly executing code written in the loop until/while a condition is met
  • for loop & while loop
  • How can this be useful?

{ for loops }

  • For loops iterate a limited amount of times through a list with a variable taking on the values in the given list
  • for variable in list:
    • The variable iterates through the list and is assigned the value of the current iteration

  • Like if statements, the for loop is defined with a colon and followed by a block of code that is executed
for x in range(5):
    y = x + 1
    print(x+y)
for s in myList:
    print(s)

{ for loops }

  • The variable that iterates through the list only exists in the for loop, and is purely used for the loop
    • It takes on every value in the list before finishing the loop
  • Example of a for loop: Assuming I earn $35 a week, and I have $0 in my savings account, I can calculate my money earned throughout the weeks for a year!

{ nested loops }

  • Nested loops finish looping through the entire inner loop before returning to the next iteration of the outer loop
  • Try typing this in your interpreter:
    • >>> myList = ['a', 'b', 'c']
    • >>> for u in myList:
    • ...     print(u)
    • ...     for v in myList:
    • ...         print(v)

{ loop exercise }

If Dean has 100 memes in his meme folder, the size of his meme folder grows by 3% every year, how many memes will he have for each year, up to 10 years?

{ while we're on loops }

  • While loops iterate though the block of code while a condition is met
    • Different from for loops, because it's while condition == true, not iterating through a list
while condition:
    # block of code

{ break }

  • You can break out of a loop before the condition is met
    • >>> while True:
    • . . .        # tons of code
    • . . .        if someCondition == True:
    • . . .                break
  • Games use while True to keep the game running and refreshing the screen!

{ another loop exercise }

Answer the previous question using a while loop

 

If you don't remember the task:

If Dean has 100 memes in his meme folder, the size of his meme folder grows by 3% every year, how many memes will he have for each year, up to 10 years?

{ input }

  • You can take in user-inputted text in the code, and store it in a variable
  • input("text")
    • The user types something in and presses Enter
    • Whatever they typed in is returned by input() as a string
  • >>> name = input("Please enter your name: ")
  • >>> print("Hello, %s!" % name)
  • No matter what they input, it'll return a string

{ functions }

  • Functions allow you to write code just once, then reuse that code in your programs multiple times.
    • A group of lines of code that should all serve one purpose 
  • Functions take in parameters, which is a variable you give it that is only available in the body of the function (the block of code inside of it). 
def function(parameter):
    # block of code

{ function example }

You define (make) the function like this:

def printName(name):
    print("Hello, %s!" % name)

And call (use) it like this:

printName("John")

{ function example }

Another example, except with return instead of print():

def savings(chores, job, spend):
    return chores + job - spend
    # or return(chores+job-spend)

And call it like this:

print(savings(10, 10, 5))
# OR
money = savings(10, 10, 5)

{ functions }

  • Return
    • Must be used/saved to a variable or the value disappears
    • Useful for holding the value instead of immediately printing it
  • General facts
    • Anything with parentheses is a function
      • ex. print("string"), input(), append()
    • You create the parameter when defining the function, and pass it in (give the value) when calling it

{ scope }

  • Not all variables are accessible from all parts of our program.
  • Where a variable is accessible depends on how it is defined.
  • We call the part of a program where a variable is accessible its scope.
  • ​A variable which is defined in the main body of a file is called a global variable.
    • It will be visible throughout the file, and also inside any file which imports that file.
  • A variable which is defined inside a function is local to that function.
    • It is accessible from the point at which it is defined until the end of the function
# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1

def my_function(c):
    # this is a local variable
    d = 3
    print(c)
    print(d)

# Now we call the function, passing the value 7
# as the first and only parameter
my_function(7)

# a and b still exist
print(a)
print(b)

# c and d don't exist anymore -- these statements
# will give us name errors!
print(c)
print(d)

{ function exercises }

  • Write a function called practice_function() that takes in a number as a parameter. If the number is less than 50, print the value. If the number is greater than 50 but less than 1000, print a string. If the number is any other value, return the number, then print it. Call the function for three different values: 5, 100, and 2000

 

  •  Write a function called describeStudent() that accepts the name of a student and a positive personality trait as parameters. The function should return a simple sentence (for example: "John is awesome"). Call your function for three different students, store them in a list, and print the items in the list separately.
    • item1                 NOT      ['item1', 'item2', 'item3']
    • item2
    • item3

{ the internet }

  • How the internet works
    • Network of interconnected machines
  • IP Address
    • Unique number assigned to each device on the internet
  • Difference between the internet and world wide web
    • WWW is a way of accessing the information over the internet
  • How we connect to the internet
    • Servers, sockets, and ports

{ server/client }

{ sockets & ports }

{ firewall }

  • Network security system that monitors incoming and outgoing traffic on a network
  • Prevents unauthorized access to/from a private network
  • Filters these things to keep you safe from attackers or malware

{ scp }

  • Secure Copy protocol
  • The scp command allows you to remotely copy files from:
    • Your host computer to another computer
    • Another computer to your host
    • Another computer to another computer
  • Copies files between hosts on a network
  • Syntax:
    • scp user@host1:source user@host2:destination
      • user is the user on the host
      • host is the computer; usually the IP address
      • source is the path to the file being copied to the destination

{ scp example }

  • ex. scp ~/Documents/file.txt junior@192.168.0.2:~/Desktop
    • The localhost doesn't need the first part before the colon (user@host:)
    • Find out your IP address by typing ifconfig into your terminal and look at your "inet address" under wlp3s0

{ scp exercise }

  • Use scp to send your partner a file containing a message
    • Place it in their Desktop
  • Remember to use the man command if you forgot how to use SCP

{ ssh }

  • Secure Shell
  • ssh allows you to remotely access a machine by logging in and executing commands
  • Syntax:
    • ssh user@hostname
    • Similarly to scp, you need the password of the remote host
  • You can't ssh from one host into another

{ vpn }

  • Virtual private network
    • A private network that extends across a public network (like the internet)
    • Allows you to privately send and receive data (aka do things) as if you were directly connected to another private server even if you're on an untrusted network
    • You can secure your wireless logins and transactions
    • Circumvent geographical restrictions and censorhsip
    • Shield your location from insecure network traffic

{ wargames }

  • In hacking, a wargame is a cybersecurity challenge and mind sport in which the competitors must exploit/defend a vulnerability in a system or application, or gain/prevent access to a computer system
  • https://overthewire.org
    • Select Bandit
    • Advance through the levels

{ survey time! }

  • https://www.surveymonkey.com/r/BVV75TQ

Hack to the Future Day 2

By jtheadland

Hack to the Future Day 2

  • 757