Table of Contents

C/C++ Programming Journal

January 31, 2015 | Week Two

In C/C++ programming, the very basics were learned. First off, the “#” symbol is the representation of the start of something that is not actually a C statement. It is a preprocessor symbol, something that happens before the program is actually compiled. In the cases we have used this symbol, it is to reference what libraries we will be using certain functions from. The only libraries which we have used so far are the following: stdio.h and stdlib.h. Both are standard libraries in the C standard. In order to indicate which library will be used with the “#”, the following can be written:

In C programs, all C statements are written within functions. For example, in VBS, statements could be written outside of a function, but in C, all of these statements must be nested within a function. Now, the questions comes up: Which function will the program start with if every statement MUST be in a function? Well, when a C program is first ran, the function which it looks for first is a function called main which returns an int.

int main() {

  // do stuff
  return 0;

}

The number indicates the error level. Normally, a 0 indicates that the program ran without any errors. Any other number usually indicates that something went wrong, and programmers will use different numbers to indicate different kind of errors.

Whenever a C program is ran, three different files are created automatically: stdout, stdin, and stderr. stdout is a file which represents where output will go (usually your monitor/terminal). stdin is a file which represents where input comes from (usually your keyboard). stderr is a file which handles errors.

In order to output a message with C, the following can be performed.

int main() {

   fprintf(stdout, "Hello, world!");
   return 0;

}

This will print a simple message. The function printf() can also be used. The only different is that fprintf takes in the argument of the output (in this case, stdout) whereas printf() will always default to stdout.

To take in user input, the following can be performed:

int main() {

   int input = 0; // this is declaration and initialization of a variable
   printf("Enter in a number: ");
   fscanf(stdin, "%d", &input);
   return 0;

}

So, what just happened? We set the variable input equal to whatever number the user input. The fscanf function (just like the fprintf function) can be replaced with scanf, where the argument of where the input should come from is not needed and simply defaults to stdin.

The second argument of fscanf is an indicator as to what kind of data type we will be working with. %d indicates that we will be working with an int. The following are the indicators for other kind of data types (the d stands for signed, the u stands for unsigned):

The third argument of the fscanf function is the variable which the value of the input will be assigned to. The ampersand character which is seen preceding the variable is a character which will retrieve the address of the variable.

February 22, 2015 | Week Four

I know this entry is late, but I don't care. The reason I didn't do it is because a friend and I were too busy setting up and running the best Minecraft server to hit this generation.

Let's talk about functions! A function needs a name, an output, and an input. The name of the function can be anything. The input and the output must be some sort of datatype, such as an int or a char. Or, the input could be nothing (void). For example…

int getAge() {

return 17;

}

So, the name of this function is “getAge”. The input of this function is nothing, or, void. This can be seen by how there is nothing inside of the parenthesis. The output is an int. This can be seen by how before the name, it clearly states “int”. That shows the return type, or the output.

So, let's make a function which will take in a non-void input now.

int add(int number) {

return ++number;

}

First off, you might be wondering what the “++” means. I will go over that later.

So, in this case, the name of the function is “add”. The output of this function is an int. The input of this function is also an int. This can be seen how there is an integer parameter inside of the parenthesis. What this function will do is return whatever number is put in as the input added by one. So, if the input is 17, the return value would be 18.

In order to call a function, all you would have to do is the following.

int main() {

int number = add(17);
printf("%d", number);
return 0;

}

This will assign the value of the function “add(int)” to the int variable “number”, which, in this case, will be 18.

Now, back to whatever that “++” thing was. This is a symbol which can be used to increment an int. So, for example…

int main() {

int number = 1;
number++;
return number;

}

So, first, we initialize a variable, which is an int known as number. Next, we increment the value of number by one. Then, we return it.

There can also be a “++” before the variable as well (++number). This will do the same thing, except is used within a function or return type. If used within a function or return scope, the value of number will first be increased then passed through. If the “++” is after the variable name, the variable value will be passed through first then incremented. For example…

int main() {

int number = 1;
printf("%d", number++);
return number;

}

What this will do is first, print out the current value of “number”, which is one, then increment it. So, the return value will be two. However, if put like this…

int main() {

int number = 1;
printf("%d", ++number);
return number;

}

…first the value of number will be incremented, then the value will be printed out and returned.

This can also be used with subtraction to decrement. The syntax for this is a “–”. For example…

int main() {

int number = 0;
number--;
return number;

}

Instead of incrementing by one, it will be subtracted by one. The rest of the areas shown above can also be applied with this.

As well as this shortcut for incrementing and decrementing, there are also shortcuts to performing certain math operations to the value of itself. For example, if you want to add five to a certain number, how would you do that?

int main() {

int number = 5;
number = number + 5;
return number;

}

OR

int main() {

int number = 5;
number += 5;
return number;

}

This shortcut can apply to any mathematical operation. For example…

int main() {

int number = 5;
number += 5;
number -= 5;
number *= 5;
number /= 5;
number %= 5;
return 0;

}

March 17, 2015 | Week Seven

I was not able to fill out an entry for this week on time because I was very busy the days before this. I'm going to do it anyways.

In C/C++ Programming, we learned about using loops within loops to our advantage. In class, Matt gave us a file filled completely with hexadecimal values. The objective was to decode this file to create a cool piece of ASCII art.

The dimensions of the ASCII art was known to be 59 by 25. This would mean we would need two loops, one which would loop 59 times and one which would loop 25 times. These loops would slowly extract the contents of this file piece by piece and printf it.

Another important aspect of this project was to learn how to work with files in C. C has a datatype known as FILE. Within stdio.h are functions which work in regards to opening files and stuff like that. In order to open a file in C, the function fopen(const char * filename, const char * mode) which have to be used.

FILE * file = fopen(“random.txt”, “r”);

The first argument of the function is the path to the file you want to open. In this case, we are using a relative path to a file called “random.txt” within out current directory.

The second argument of the function is what we will essentially be doing with the file. Reading, writing, stuff like that. In this case, we will only be reading from the file, hence the “r”. Here is a list of what can be put in as a second argument.

r: read w: write a: append “r+”: read/update “w+” write/update “a+”: append/update

Since we only need to read from the file, we will use the “r” option.

Once the file is open, it is a good practice to check if the FILE pointer is NULL. The FILE pointer would be NULL if the specified file does not exist. Simply perform a NULL check on the FILE pointer.

if(file == NULL) {

  fprintf(stdout, "Error opening file.");
  // below function from stdlib.h
  exit(1);

}

This will check if the FILE pointer is NULL. If it is, a message will be printed out and the program will be terminated with an error level of one.

The next step would be to create the nested for loops. That would look something like the following.

int i, j; for(i = 0; i < 25; i++) {

  for(j = 0; j < 59; j++) {
      // DO STUFF
  }

}

This whole block of code (excluding the first declaration of the ints) will be ran a total of 25 * 59 times, which is 1475.

Now all that we need to do is read in a byte worth of data at a time from the file, convert it to ASCII format, and then print it out on the screen. Since we need to read in only a byte worth of data from the file at a time, we will initialize a char before the loops start. Note that at this point, there should be a variable which is a FILE pointer to the hexadecimal dump.

char c; int i, j; for(i = 0; i < 25; i++) {

  for(j = 0; j < 59; j++) {
      // DO STUFF
  }

}

To read data in from a FILE pointer, we simply use the fscanf(FILE * stream, const char * format, …) function. This function not only reads input from stdin, but any file.

When reading information in from a file, it acts just like a cursor. As you read more from the file, the cursor moves more towards the end of the file. For example, if you have a file with the following contents…

0a 56 74 23 75 83 56

…before you read anything in, the cursor is at the very beginning. After you have read one byte of information in, the cursor is before the 56, etc, etc.

Since we will be reading in a hexadecimal value, we need to use the format specifier “%hhx”.

char c; int i, j; for(i = 0; i < 25; i++) {

  for(j = 0; j < 59; j++) {
      fscanf(file, "%hhx", &c);
  }

}

This new line will read in one byte at a time worth of hexadecimal value.

Now, all we need to do is print this value out. This is where the beauty of format specifiers come into play. Instead of having to do fancy stuff to take this hexadecimal value and convert it to it's ASCII equivalent, all we need to do is use the format specifier “%c” for char when printing out the value at “c”!

char c; int i, j; for(i = 0; i < 25; i++) {

  for(j = 0; j < 59; j++) {
      fscanf(file, "%hhx", &c);
      fprintf(stdout, "%c", c);
  }

}

This new line will print out the value of “c” in an ASCII format to stdout, which, as we know, is out terminal.

Now, there is one last step that must be performed anytime you are ever dealing with files. We must close the file. If we opened it, wouldn't it make sense to close the file as well? Yup, it does. Luckily, the standard C library comes with a handy dandy function known as fclose(FILE * stream) which is located in “stdio.h”. This function will simply close the FILE pointer which is passed into it. This functions returns an int value. If the function successfully closed the file, it will return a zero. If there is any other number, the function failed to close the file. In some cases, I suppose it would be reasonable to check this int value to ensure that the file was indeed, properly closed.

March 22, 2015 | Week Eight

This has been a busy week. I've basically been going home and doing homework. I guess that's what I get for doing 19-credit hours.

Anyways, this week in C/C++ Programming lab, we learned about these things called a struct. A struct will essentially allow you to put multiple different datatypes into one defined type. For example, if you want to match up an int with a char *, you could use a struct to do this. It's kind of like objects in Java or C++; the only difference is that with objects, you can also implement functions/methods. With structs, you can only use variables. BUT, you can put function pointers within structs, but let's not get into that.

Another way to think of a struct is like a bag of Halloween candy. It is a bag of many different types of candies, rather than one type of candy.

So, let's get to the syntax!

#include <stdio.h> #include <stdlib.h>

int main() {

  struct candy {
      
  };
  exit(0);

}

Notice how we are using curly braces, yet we still have the semicolon after the last curly brace. Make sure that is there, otherwise the compiler will yell at you.

Now, we have to put what variables we want to store within this struct.

#include <stdio.h> #include <stdlib.h>

int main() {

  struct candy {
      char name[80];
      char good;
      char sweet;
      char soft;
      char wrapped;
      int quantity;
  };
  exit(0);

}

Notice how we added multiple different datatypes. First, we added an array/pointer to chars with a maximum storage space of 80 bytes. Next, we added some chars, which will be used as boolean (true/false) typed characters (0 = true, 1 = false), and then a single integer which specifies the quantity of the type of candy.

Another thing to notice with structs is that we put this struct inside of the main function body. This means that this struct will only be available to code placed within the body of the main function. If you wanted to make it so that any function could use this struct, perform the following.

#include <stdio.h> #include <stdlib.h>

struct candy {

  char name[80];
  char good;
  char sweet;
  char soft;
  char wrapped;
  int quantity;

};

int main() {

  exit(0);

}

Notice how now, the struct is now in any body. This makes the struct publicly available to any other function within the same file. So if we needed to use this struct in other function, this is now possible!

Alright, so now let's get to how we can use this struct to our advantage.

#include <stdio.h> #include <stdlib.h>

struct candy {

  char name[80];
  char good;
  char sweet;
  char soft;
  char wrapped;
  int quantity;

};

int main() {

  struct candy myCandy;
  exit(0);

}

With that new line, we have successfully created a new instance of the struct candy and named it “myCandy”! Now, if it is annoying to have to declare the variable as “struct candy NAME”, we can actually shorten this declaration process!

#include <stdio.h> #include <stdlib.h>

struct candy {

  char name[80];
  char good;
  char sweet;
  char soft;
  char wrapped;
  int quantity;

}; typedef struct candy Candy;

int main() {

  //struct candy myCandy;
  Candy myCandy;
  exit(0);

}

Now, the line “struct candy myCandy;” and “Candy myCandy” will perform the exact same action! In fact, you can actually use this with any datatype in C! Simply follow the general formula below.

typedef DATATYPE NEW_NAME; typedef struct candy Candy; typedef int Number; typedef char Character;

So, now that we have successfully created a new instance of the sturct candy, let's figure out how to actually use it.

#include <stdio.h> #include <stdlib.h> #include <string.h>

struct candy {

  char name[80];
  char good;
  char sweet;
  char soft;
  char wrapped;
  int quantity;

}; typedef struct candy Candy;

int main() {

  //struct candy myCandy;
  Candy myCandy;
  strcat(myCandy.name, "twix");
  myCandy.good = '0';
  myCandy.sweet = '0';
  myCandy.soft = '0';
  myCandy.wrapped = '0';
  myCandy.quantity = 2;
  exit(0);

}

Alright, so, what are we doing here? Here, we are initializing each of the variables inside of the struct. The general way to access a variable within a struct is to mention the struct name, followed by a dot (period) and then the variable name inside of the struct (as shown above).

Alright, so that was how to initialize the variables. Now how do we access these variables? It would be the same as when you were initializing them!

#include <stdio.h> #include <stdlib.h> #include <string.h>

struct candy {

  char name[80];
  char good;
  char sweet;
  char soft;
  char wrapped;
  int quantity;

}; typedef struct candy Candy;

int main() {

  //struct candy myCandy;
  Candy myCandy;
  strcat(myCandy.name, "twix");
  myCandy.good = '0';
  myCandy.sweet = '0';
  myCandy.soft = '0';
  myCandy.wrapped = '0';
  myCandy.quantity = 2;
  printf("%s\n", myCandy.name);
  printf("%c\n", myCandy.good);
  printf("%c\n", myCandy.sweet);
  printf("%c\n", myCandy.soft);
  printf("%c\n", myCandy.wrapped);
  printf("%d\n", myCandy.quantity);
  exit(0);

}

What we are essentially doing here is printing out all of the values within the struct we created. All you have to do to get a variable within a struct is refer to the struct with a dot separator, and then the name of the variable.

That's basically all there is to structs! Enjoy!

Alright. So, I finished a lot of my hex editor as well. I thought I would post the current source code here and explain it a little bit, stuff like that.

#include <stdio.h> #include <stdlib.h>

long getFileSize(FILE *); void printPage(int, long, char *);

int main(int argc, char * argv[]) {

if(argc != 2) {
	fprintf(stdout, "Invalid use: ./hex-editor.exe FILE_NAME\n");
	exit(1);
}
FILE * file = fopen(argv[1], "r");
long fileSize = getFileSize(file);
char bytes[fileSize];
fseek(file, 0, SEEK_SET);
long i;
char c;
for(i = 0; i < fileSize; i++) {
	fscanf(file, "%c", &c);
	bytes[i] = c;
}
fclose(file);
int quit = 0;
while(quit == 0) {
	printf("\n\n\n\n\nPossible actions: 1 (change page) - 2 (modify hex) - 3 (quit hex editor)\n");
      	printf("Enter action: ");
      	int action = 0;
      	scanf("%d", &action);
	if(action == 1) {
		int page = 1;
		printf("Enter page number: ");
		scanf("%d", &page);
		if(page < 1) {
			printf("The page number must be greater than zero!");
		} else {
			printPage(page, fileSize, bytes);
		}
	} else if(action == 2) {
		
	} else if(action == 3) {
		quit = 1;
	} else {
		printf("%c is not a valid action!", action);
	}
}
printf("\n\n\n\n\n\nGoodbye!\n");
exit(0);

}

void printPage(int page, long size, char bytes[]) {

int index = (256 * (page - 1));
printf("\n      00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f  0123456789abcdef\n\n");
int i, j, k;
for(i = 0; i < 16; i++) {
	printf("0x%x0  ", i);
	char ascii[16];
	for(j = 0; j < 16; j++) {
		if(index >= size) {
			ascii[j] = '.';
			printf("00 ");
		} else {
			ascii[j] = bytes[index];
			printf("%02x ", bytes[index++]);
		}
	}
	printf(" ");
	for(j = 0; j < 16; j++) {
		k = (int) ascii[j];
		if(k < 32) {
			printf(".");
		} else {
			printf("%c", ascii[j]);
		}
	}
	printf("\n");
}

}

long getFileSize(FILE * file) {

fseek(file, 0, SEEK_END);
return ftell(file);

}

You're amazed by this, aren't you. I know, it's a lot to take in.

At this point in time, all this hex editor will do is display the hex of any file you want in pages.

In order to run this hex editor, compile it, and run it through terminal. Compile the file with gcc -o hex-editor.exe hex-editor.c (putting the above source code in a file called hex-editor.c). This will spit out a file called hex-editor.exe.

To properly run this program, type into the terminal “./hex-editor FILENAME”.

For example: ./hex-editor random.txt ./hex-editor ~/todo.txt ./hex-editor /home/mmalik1/todo.txt

The FILENAME can simply be the path to the file rather than just the file name. This will open the file.

Next, a prompt will appear. This will ask you what you want to do. Currently, all this program will do is display hexadecimal values of each byte, displayed by pages, each page containing 256 bytes.

Currently, I need to add in a modifying feature. Basically how this will work is it will ask you to input the hexadecimal bytes you want to input somewhere. Then, it will ask you where you want the first byte of whatever you input to be placed. This will then be saved into the file!

Hopefully I can find good use out of this program, regardless of the fact that it is a program I have to create for Joe's class. I can find some pretty good uses out of this program. If I can keep expanding off of it, I cane easily make it a very good hex editor.

That's it for this week. I plan on starting the C project Matt told us about last class but hasn't posted yet. Along with that, I need to get the projects for Joe done, otherwise, I'll end up procrastinating those and not doing them, and that wouldn't be good!

March 30, 2015 | Week Nine

Whelp, I just wrote my UNIX/Linux journal entry. My hands really hurt because earlier today I was playing soccer and spikeball with some friends, and then the typing didn't really help. So yea, they kind of hurt a bit. I'm making a bunch of sloppy mistakes so writing this entry is probably going to require a lot of backspacing. In fact, so far, I've probably backspaced about 50 times so far. Okay, maybe not that many times, but you get the point.

Anyways, this week in Joe's class, I don't really remember what we went over specifically. Something about the C standard and these crazy projects we need to complete. Not sure.

However, in Matt's C lab, we did some pretty cool stuff. We drew stuff in a picture using C and this library known as gd.h.

So, for starters, we included stdio.h, stdlib.h, and gd.h. We then defined some stuff, which were:

BLACK 0 RED 1 GREEN 2 BLUE 3 WHITE 4

These will come in handy later.

In main, we began by declaring a file pointer called out, an array of unsigned ints with a max storage size of 5 unsigned ints, an unsigned short int wide and high, wide which was set equal to 800 and high which was set equal to 600, and something else we have never worked with before until this point. Something known as a gdImagePtr. This gdImagePtr was set equal to gdImageCreateTrueColor(wide, high). This creates an image with the set dimensions.

Next, we had to define the colors. We did that by using the function gdImageColorAllocate(gdImagePtr, char, char, char). We defined each color for each space in the array which was made earlier. This is where the defined stuff comes into handy. Instead of associating black with the number zero, all we have to type in is BLACK. The compiler will automatically associate this keywork with the number zero. This makes the work so easy.

After this, we can begin making shapes and stuff. Some of the functions which we played with to create shapes are the following.

gdImageFilledRectangle(gdImagePtr, int, int, int, int, int) gdImageFilledArc(gdImagePtr, int, int, int, int, int, int, int, gdArc)

There are a lot more, but you can look them up yourself.

So, after you are done drawing shapes onto a picture, you need to save this. First, define what the file pointer out will be save to.

Next, use the function gdImagePngEx(gdImagePtr, out, -1)

This will successfully export a PNG of whatever you drew.

All that is left to do is to close your file and close the gdImagePtr using the function gdImageDestroy(gdImagePtr).

Also, for the record, I had to backspace so much during this. It was terrible.

April 20, 2015 | Week Eleven

In C, we learned about stuff. Stuff that I can't remember, give me a minute…

Oh yea! We learned about inheritance!

Inheritance deals with parent and child classes. Basically a class can inherit everything from another class. All functions, variables, stuff like that.

This makes creating classes which needs stuff from another class a lot easier. So that instead of having to copy/paste everything over, you can just use inheritance.

Here is an example of where it would be in handy.

Let's say you have a class called “Kit” which is a list of items you get for a game. If a certain kit needed an extra function to operate, that class could inherit everything from the “Kit” class. This would bring all variables/functions over from “Kit” to this new class.

We learned about nodes, as well as how that is the main focus of data structures, which is apparently the second best class we will ever take.

April 26, 2015 | Week Thirteen

Well, here we are again. Writing another Opus entry. Let's do this.

So, in C/C++ programming, we learned about this cool concept known as polymorphism. Polymorphism is kind of complicated, so stay with me.

It is linked with inheritance, which is what I talked about last time. Basically, with inheritance, certain classes can inherit all functions or objects or variables in other classes. So, for instance, if I have a class called Animal which has the function called void makeSound(), and I make another class called Dog which inherits everything from Animal, Dog will also have the makeSound() function.

Now, this is where polymorphism comes into play. Let's say makeSound() prints out to the screen “Sound”. But, a Dog doesn't make this sound. Dog makes a “Woof” sound. This is where polymorphism comes into play. We can change the makeSound function for the Dog class. So, whenever the makeSound() function is called in reference to the Animal class, “Sound” is printed to the screen. Whenever the function makeSound() is called in reference to the Dog class, the text “Woof” is printed.

That's basically all there is to polymorphism. Unfortunately, most people don't see any use in this neat concept until they start working on bigger projects than school projects, perhaps the next big game? Or a meme generator? Who knows.

That's essentially all we did in C/C++ lab. So, I'll talk about what we did in Joe's class.

Let me think… I don't really remember what we did on Monday. On Wednesday, I asked Joe what exactly he wanted for the assembler project.

Joe explained to me how he essentially wants us to make a program which will convert from machine language to assembly language, or what other way around. He recommended that we do machine to assembly because I guess it is easier.

Joe said how he only wants us to do the ADD instruction, so that definitely makes the project a lot easier than before, considering there are a ton of instructions.

That was really all we did on Wednesday. Of course, Joe showed of the 8086 spreadsheet and we went through some examples. He also said that he would create some test code, so that we can test whatever we write and see if it actually works.

On Friday, we went over exactly how the random number generator. During this time, I wasn't exactly paying attention like I should have been (I was working on a Java project for something else), but Joe wrote a text file filled with everything that we had learned from that class. HOWEVER, he hasn't committed it yet. This makes me sad, considering I planned on completing the random number generator this weekend, but I guess I will have to wait until Wednesday (class is cancelled on Monday).

That's essentially everything that happened last week. So, I'll talk about BSidesROC.

BSidesROC is a hacking convention. It is in Rochester. I went to the convention with Casper and Dan. We ended up having to leave at 6:00am, which sucked. We ended up getting to the convention about 30 minutes before doors opened, which was pretty cool I guess.

The convention wasn't as good as Casper and Dan has hyped it up. In fact, the entire time we tried playing this hacker battleship game, which we ended up failing at terribly.

What hacker battleship is is basically there is a board of maybe 10 by 10. Within that board are hidden battleships. Each block can be fired at if you can answer something related to hacking or just a puzzle in general. I don't think anyone was able to figure out any of them because by the time we left, no team had gone above one point (the default score). Some of the questions on there were pretty easy, but for some reason, it would say they were incorrect whenever we typed it in. We thought maybe it was just really sensitive, yet we still couldn't figure out how to put our answers in.

We ended up doing that the whole time. Well, that and playing around with a pick-locking area they had. I wasn't any good at it, but Casper and Dan were masters of it. I can only guess they have done it before.

After that we went to eat lunch. Tom and Harry joined us and we ended up sitting in the store for about 3 hours before we just decided to leave.

Overall, it wasn't as great as I had expected it to be. Casper and Dan said it was not as good as last year.

UNIX/Linux Fundamentals Journal

January 23, 2015 | Week One

Very basic knowledge was learned this week, mainly commands used to navigate, create files, edit files, stuff like that. These commands included, but are not limited to: cd, ls, mkdir, touch, nano, rm, cp, mv, ftp. Other stuff learned for this week was how Lab46 works and how to actually gain access to Lab46 via SSH. If on a computer in the lair, the command “ftp lab46” can be simply typed into the console and woosh, access granted. Accessing Lab46 outside of the lair is a bit different. If on Windows, an SSH client is required. If on UNIX, Linux, Mac, etc, the same “ftp” command will work. Except, instead of just typing in “ftp lab46”, you must type in “ftp USERNAME@lab46.corning-cc.edu”. With an SSH client, simply type in “lab46.corning-cc.edu” into the host slot and hit “Connect”. Enter the username and password and woosh, access granted.

February 4, 2015 | Week Three

This week in UNIX/Linux, we learned about the best text editor ever! That being vi (well, actually vim, vim being a more updated version of vi), just in-case if you didn't know.

“vi” stands for visual editor. It is a text editor which comes on a vast majority of UNIX based operating systems. It was originally created for the purpose of modifying text without the use of arrow keys, a mouse, delete buttons, all those extra buttons that keyboards did not have back then.

vi works in a manner called “moded editing”. There are two modes. The command mode, and the insert mode. The command mode allows you to push certain buttons to perform certain actions. Here is a list of commands which can be performed:

There is also something in vim called extended commands. These commands are usually preceded by a colon. Here is a list of extended commands:

It was explained how vim may not be seen as a very useful tool at the beginning. The commands will be difficult to remember and some people will simply go back to using a text editor such as nano or notepad. The use in vim can be seen, however. The many commands which are implemented within vim, if used properly, can allow one to edit text and manipulate it very easily.

March 2, 2015 | Week Five

This week in UNIX/Linux, we learned about scripting and wildcards.

Scripting is basically a shortcut to performing something over and over again a million times. For instance, if you were starting up some sort of service daemon every time a system started up, you could write a script instead which could be ran on system start-up. Or, if you were moving a ton of files with similar file names (for instance; file-X; where X is a number one through ten), a script could be written to move the files very quickly.

With scripting, there are multiple shells which one can use. Just if you didn't know, a shell is the thing that gives you your prompt (the thing that might say “lab46:~$”)

Each of these different shells will give the use a little bit different of an experience. In this case, we will be experimenting with bash scripting.

This is how you write a bash script.

First off, create a file in which you will create your script using touch or vim. Since there is no such thing as a file extension in UNIX/Linux, an extension is not needed. However, if you want to, you can put a .bash at the end for conventional purposes.

The first line of the bash script should be the following:

#!/bin/bash

This line is otherwise known as “sh-bang”. This indicates that this is indeed a bash script.

After this, it really all depends on what you want to do, so, here are some general ideas of bash scripting.

The pound or sharp symbol is used for comments. So, if you wanted to say something at the beginning of your script, you could perform the following.

(I don't know why the formatting of the bash scripting is like this; if anyone knows, please let me know)

#!/bin/bash # this is the BEST SCRIPT EVER!

All of the characters after the pound/sharp symbol will be ignored (an exception being the first line).

Initializing variables is quite easy, since bash is such a high level language. In order to initialize a variable, simply perform the following.

#!/bin/bash a=1

That is all there is to it! The variable with the name “a” has now been set equal to the number one! This is also true of strings and booleans!

#!/bin/bash a=1 b=“unix is awesome” c=false

Now, in order to get this information back, we need to do something a little different from what we would usually do. For instance, if we had a variable called “pizza” which was set equal to seven, we could do the following.

#!/bin/bash pizza=7 echo “I have $pizza pizzas!”

This script would print out onto the terminal: “I have 7 pizzas!”

So, in order to grab the value of a certain variable, a dollar sign must be used. Another example, which also includes getting input from the user, is as follows.

#!/bin/bash name=“” echo -n “What is your name? ” read name echo “Hello $name!”

Now, something you may wonder is “Well, if you use a dollar sign to get a value from a variable, what if you want to represent a literal dollar sign?”. The answer is simple, use a double dollar sign. For example…

#!/bin/bash echo “You have $$10”

This would print out: “You have $10”

Within bash scripting, we can also write if statements. An example of an if statement would be the following.

#!/bin/bash a=1 if [ “$a” -eq “5” ];then

  echo "a is equal to 5!"

else

  echo "a is not equal to 5!"

fi

For loops can also be written. An example of a for loop is as follows…

for1);do

  echo "$i"

done

This will echo all of the numbers from one through nine.

Within UNIX/Linux systems, there are also wildcards. Wildcards allow users to go through a range of different options. Here is a list of wildcards.

Here is an example of where wildcards would be useful.

If you wanted to find all files with three characters in it, you could perform the following command.

ls ???

March 9, 2015 | Week Six

Well, for this week, class was technically cancelled! At 3pm, all classes and activities were cancelled, and our class started at 2:40pm! However, we still stayed in class because, UNIX/Linux is just awesome!

So, during this time in which class was “cancelled”, we learned about the dd command. The dd command is used to convert and copy a file. For example, if there were a file in which you only needed the first 5 bits, this would be possible with the dd command.

One of the arguments for the dd command is if=FILE. This stands for input file. This is the file which the bytes will be read from. The FILE part of this argument should be replaced with the path to the file. This path can either be relative or absolute. If a path is not specified, then stdout is assumed by default. For example:

Another one of the arguments for the dd command is of=FILE. This stands for output file. This is the file in which the bytes which are read will be written to (these bytes coming from the input file specified). This file can either be existent or non-existent. If non-existent, the file will be created. Here is an example of using this argument with the dd command:

All of these commands will simply copy all of the contents of the input file to the output file. This can be used as a method of copying a file, but there is already a command which specializes in that (cp).

Now, in some instances, the whole file will not want to be copied. For example, if you only want to copy the first 50 bytes. Counting the first 50 bytes isn't very efficient! We need to be more lazy! Productive laziness! This can be done with the dd command. The set of arguments which will make this possible are bs=BYTES and count=BLOCKS. Now, let's back up a bit. What in the world are BLOCKS???

Blocks are the unit of measurement when it comes to the dd command. One block is X bytes. The amount of bytes in a single block of data can be set by the user! This is where the bs=BYTES argument comes into handy! For example, if you want a block to be 5 bytes: bs=5

The block size by default (I believe!) is 1 byte. After you have set the amount of bytes in a single block, the amount of blocks which will be used can be modified with count=BLOCKS. For example, if you want to copy 2 blocks of 16 bytes: dd if=random.txt of=random.txt.copy bs=16 count=2

This will effectively copy a total of 32 bytes (16 times 2; block size times amount of blocks).

Now, another area of dd which is really cool is that you can set the offset! What's an offset? I'll explain it!

The offset is how far from the beginning of the file you want to go. For example, if you want to start 2 bytes after the start, this would effectively be the third byte. If starting from zero, this would be identified as byte 02. So, you would set the offset as 2 bytes.

For the dd command, there are two different offsets which can be set. One for the input file, and one for the output file. For example, if you wanted to read in 32 bytes from a file starting at the fifth byte:

This will effectively read 32 bytes starting from the fifth byte (4 bytes away from the beginning) and write it to the output file.

If you want to set an offset for the output file in a similar scenario as above, the following can be done:

So, the arguments for offsetting both the output file or the input file is different, so make sure not to get them mixed up!

There are a lot more arguments which come with the dd command, but this is mainly what I have done with the dd command so far.

Something cool which can actually be done with the dd command is wiping out a storage device! Quick formatting!

This will basically wipe out the file known as sda, which represents a storage device of some sort. This should also be a means of a warning. Be careful when using the dd command, and make sure to revise what you are about to run (and make sure to make copies of the original file, just in case!).

March 30, 2015 | Week Nine

So, I'm going to be honest here. I don't really want to write this up right now because I have a little bit of a headache, but whatever. Let's do this.

Not really sure what happened in UNIX/Linux for week nine, so let me go check…

Whelp… in my notes directory for week 9, I have nothing…

So, I'm just going to talk about this lab that Matt just extended until after break.

This lab is called udr2, the next installment of the famous “Unix Data Recovery” series of projects. So far, I haven't really had a huge issue with these projects. Maybe an email to Matt every once in a while. Nothing major. This udr2, however, I'm not sure about at this moment. The main focus of the lab is to get the packets from raw data which Matt so gracefully provided us. These packets are information from an EKG device. Matt decided that he wanted to play around with a new sleep schedule, and decided to monitor his brain-waves whilst doing this.

So, with this raw data and information on how to identify a packet and whatnot, we basically have to answer a bunch of questions about these packets. For example, how many packets are there in each session file, how much time elapsed in each session, stuff like that. So far, I haven't really had an issue with the questions. On one or two of them, I did get a little bit stuck, but all I did was think about it in a different way and I was able to easily come up with the solution. Or, at least, I hope I came up with the solution. It would really suck if I got it wrong after a long time of thinking.

Along with this lab, Matt was so kind to give us a tool which would help us dissect the packets and answer the questions properly. This tool is similar to grep, but grep only works with ASCII text, and the session files we were provided with are binary data. Therefore, we can't use grep. Matt did say that grep can be messed around with to make it work with binary data, but that I guess he didn't want us to mess around with that stuff. In fact, he was contemplating having us figure out how to use grep with binary data, but decided that he didn't want that.

Instead, he created a new tool. The next big thing. This next big thing is called bgrep. bgrep stands for binary grep. You guessed it. bgrep is basically grep, but it works with binary data. So, now, we can find certain patterns within binary data without having to mess around with grep a lot. bgrep saved us a lot of time.

The way you would use bgrep is actually quite simple. Let's say, for instance, there is a raw session file of binary data called cat.data. Within this file, you want to find out how many packets are within this session file. Each packets starts with 0x76, followed by a 0x41. In order to find out how many packets are within this session, we can use bgrep. Use bgrep to find out the starting point of each packet, and then use wc -l to count the amount of lines. This will be the number of packets within the session file. So, this is what the command would look like.

cat cat.data | bgrep '76 41' | wc -l

This command will print out how many packets are within that session file.

Not only can you look up absolute patterns, but also, relative patterns. For instance, if you wanted to view the first byte after the beginning of each packets, simply perform the following command.

cat cat.data | bgrep '76 41 ..'

That “..” indicates that anything can go there. So, this command will print out all the patterns in the file which starts with 76 41, and then any other byte, which can be anything in the whole world.

Isn't that neat? I think so. That's basically all there is to running bgrep. The only downside is that I think bgrep can only be used on lab46. If you have your own linux/unix machine, you wouldn't be able to use it by default. You would have to get the source code of the project from Matt and compile it and set everything up yourself.

May 11, 2015 | Week Ten

#!/bin/bash
 
#a for loop which will loop through all bytes
for((i=51912;i>=0;i--));do
        dd if=gizmoRev of=file.temp bs=1 count=1 skip=$i # extracts one byte at a time, starting from the last byte
        cat file.temp >> gizmo # appends to the result file
done
 
exit 0

This is a script I wrote for a data recovery project. Basically this will reverse the bit order of the entire file.

This script was required in order to complete the project properly. I needed to reverse a file to enable a gizmo, which allowed me to run another program which would allow me to practice chmod.

At first my script did not work. But, upon further inspection, I realized I had an off by one error. I was able to use my problem solving skills to solve the problem.

Another project which we did which was a series were the puzzlebox projects.

These projects required us to use the file command a lot. Basically, we would start with one file. This could be some compressed data or something. We would use the file command on this one file in order to find out what exactly it is. After we find out what file it is, we would use different tools found on a UNIX system to de-crypt the file or decompress the file. This would lead to more files being revealed, and therefore, more uses of the file command.

This project wasn't insanely confusing. It was a good way to show the usefulness of the file command and since then, I've used it quite often. For example, if I am ever modifying the byte order of a file or modifying bytes of a file in general, I always use the file command on the file to make sure that the file is the correct type. Otherwise, everything gets screwed up.

May 11, 2015 | Week Eleven

This week, we learned about process IDs.

A process is essentially a program in action. It runs in the background (normally) and these processes can be modified at any point in time. This is similar to the task manager on windows systems.

In order to view the processes which are running under your account, simply run the command ps. This will list all of the processes running off of your account. If you are interested in listing all running processes, simply run the command ps aux. This will list every single process running on the current machine.

If you are finding that a certain program is not running properly, and you want to kill it, that it simple to do. Simply use the kill command.

Use the command ps or ps aux. Find the process ID of the program that you wish to kill. It should be fairly simple to locate the process ID.

Then simply run the command kill <ID>. It's that simple. That should kill the process which is running.

April 20, 2015 | Week Twelve

We did nothing in UNIX/Linux except take a survey.

April 26, 2015 | Week Thirteen

We didn't have class.

1)
i=1;i<10;i++