Question

In: Computer Science

Shown here is the sort.c program which takes data.txt and sorts it. If I have found...

Shown here is the sort.c program which takes data.txt and sorts it. If I have found the addresses for system(), exit(), and “/bin/sh”, how would I do the following by ONLY changing the contents of data.txt?: 1) open the shell code 2) exit cleanly fron the shell code all while 3) not receiving a seg fault.

I beleive the correct answer is to have about 18-19 rows of aaaaaaaa followed by the three addresses so that when the stack starts to pop off one of the new addresses will be in the olace of the sort return address.

Here is the code in plain text:

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

/*
* a toy program for learning stack buffer
* overflow exploiting
* It reads a list of hex data from the
* specified file, and performs bubble sorting
*/

#define SORT_ME 1 //Comment this out and recompile if you do not want the function to sort your values.

long n = 0, c = 0, d = 0, v = 0;

FILE *fp = NULL;

void SortData()
{
long swap = 0;
long array[17];

// loading data to array
printf("Source list:\n");
char line[sizeof(long) * 2 + 1] = {0};
while(fgets(line, sizeof(line), fp)) {
if (strlen((char *)line) > 1) {
sscanf(line, "%lx", &(array[n]));
printf("0x%lx\n", array[n]);
++n;
}
}
fclose(fp);

#ifdef SORT_ME   
for (c = 0; c < (n - 1); c++)
{
v = c;
for (d = ( c + 1 ); d < n; d++)
{
if (array[d] < array[v])
{
// Swap the found minimum element with the first element
swap = array[d];
array[d] = array[v];
array[v] = swap;
}
}
}
#endif

// output sorting result
printf("\nSorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%lx\n", array[c]);

}

int main(int argc, char **argv)
{
if(argc!=2)
{
printf("Usage: ./sort file_name\n");
return -1;
}

// From http://stackoverflow.com/questions/5141960/get-the-current-time-in-c
time_t rawtime;
struct tm * timeinfo;

time(&rawtime);
timeinfo = localtime(&rawtime);
printf("Current local time and date: %s\n\n", asctime(timeinfo));
  
fp = fopen(argv[1], "rb");
SortData();

return 0;
}

Solutions

Expert Solution

code incorrect

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

/*
* a toy program for learning stack buffer
* overflow exploiting
* It reads a list of hex data from the
* specified file, and performs bubble sorting
*/

#define SORT_ME 1 //Comment this out and recompile if you do not want the function to sort your values.

long n = 0, c = 0, d = 0, v = 0;

FILE *fp = NULL;

void SortData()
{
long swap = 0;
long array[17];

// loading data to array
printf("Source list:\n");
char line[sizeof(long) * 2 + 1] = {0};
while(fgets(line, sizeof(line), fp)) {
if (strlen((char *)line) > 1) {
sscanf(line, "%lx", &(array[n]));
printf("0x%lx\n", array[n]);
++n;
}
}
fclose(fp);

#ifdef SORT_ME   
for (c = 0; c < (n - 1); c++)
{
v = c;
for (d = ( c + 1 ); d < n; d++)
{
if (array[d] < array[v])
{
// Swap the found minimum element with the first element
swap = array[d];
array[d] = array[v];
array[v] = swap;
}
}
}
#endif

// output sorting result
printf("\nSorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%lx\n", array[c]);

}

int main(int argc, char **argv)
{
if(argc!=2)
{
printf("Usage: ./sort file_name\n");
return -1;
}

// From http://stackoverflow.com/questions/5141960/get-the-current-time-in-c
time_t rawtime;
struct tm * timeinfo;

time(&rawtime);
timeinfo = localtime(&rawtime);
printf("Current local time and date: %s\n\n", asctime(timeinfo));
  
fp = fopen(argv[1], "rb");
SortData();

return 0;
}


Related Solutions

I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the bubble sort algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here       ...
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts it by the amount of "reviews" that a property has in increasing order. So the most reviews first. So each listing in the array would contain a different number of reviews, and they should be sorted based on that value. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to...
Create program which sorts letters of a string based on ASCII value. The program will then...
Create program which sorts letters of a string based on ASCII value. The program will then print the sorted string to stdout. Use C programming language. - Only use stdio.h - Input prompt should say "Enter string of your choice: " - Remove any newline \n from input string - Implement sorting operation as a function. Should use selection sort algorithm, but you may use a different algorithm - Output should print sorted string on new line Example:     Enter...
I have this program in C that takes three char arrays that each have a first...
I have this program in C that takes three char arrays that each have a first and last name. I have two functions that reverese the name and change it to all upper case. I have the program completeed but need to change both functions to use pointers instead of arrays. I will bold the functions I need to use pointers. #include <stdio.h> void upper_string(char []); int main() { char name1[100]="John Smith"; char name2[100]="Mary Cohen"; char name3[100]="Carl Williams"; upper_string(name1);// calling...
Python 3 Function which takes the head Node of a linked list and sorts the list...
Python 3 Function which takes the head Node of a linked list and sorts the list into non-descending order. PARAM: head_node The head of the linked list RETURNS: The node at the head of the sorted linked list. ''' def sort(head_node): #Code goes here ''' Test code goes here '' ' if __name__ == '__main__':
program runs but does not compute the average. Here is what I have. Thank you #include...
program runs but does not compute the average. Here is what I have. Thank you #include <iostream> #include<iomanip> #include"Average.h" using namespace std; int main() {    cout<<"Enter 3 integers, seperated by spaces, on a single line: ";    int number1, number2, number3;    cin>>number1>>number2>>number3;    int average;    double avg; //   average = computeAverage(number1, number2, number3);    cout<<"Average of "<<number1<<" and "<<number2 <<" and "<<number3 <<" is "    <<average<<endl;          cout<<"Enter 2 integers, seperated by a space,...
I need someone to create a program for me: Create a program that takes as input...
I need someone to create a program for me: Create a program that takes as input an employee's salary and a rating of the employee's performance and computes the raise for the employee. The performance rating here is being entered as a String — the three possible ratings are "Outstanding", "Acceptable", and " Needs Improvement ". An employee who is rated outstanding will receive a 10.2 % raise, one rated acceptable will receive a 7 % raise, and one rated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT