diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..70e34ecb3dcc4c74a1f8635625e280b492849b5e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.errorSquiggles": "disabled" +} \ No newline at end of file diff --git a/a.out b/a.out index 6077ca89373cb8457763dcffb1492b09a6f5d2d1..c1322169f1ed84f5f1b6ee06571f89c6eefbff13 100755 Binary files a/a.out and b/a.out differ diff --git a/bool.h b/bool.h new file mode 100644 index 0000000000000000000000000000000000000000..a30b9c2ca67d1a74a47e7f34cae669ffbeb6924e --- /dev/null +++ b/bool.h @@ -0,0 +1 @@ +typedef enum {false, true} bool; diff --git a/charptr.c b/charptr.c new file mode 100644 index 0000000000000000000000000000000000000000..02384d3af4c9316071cebe0e82fa9c6cf0537c2f --- /dev/null +++ b/charptr.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <string.h> + +void replace(char *subject, char search, char replace){ + for(int i=0;;i++){ + // if(*(subject+i) == search){ + // *(subject+i) = replace; + // } + // if(*(subject+i) == '\0'){ + // break; + // } + + if(subject[i] == search){ + subject[i] = replace; + } + if(subject[i] == '\0'){ + break; + } + } +} + +int main(){ + //All arrays are pointers, all pointers are arrays + + // char a[5]; + // scanf("%s", a); + + // int b; + // scanf("%d", &b); + + char subject[] = "Hello World"; + printf("Addr of index 1: %p\n", &subject[0]); + printf("Value of %p \n", subject); + + // for(int i=0; i<strlen(subject); i++){ + // printf("Value of subject[%d] = %c \n", i, subject[i]); + // printf("Address of subject[%d] = %p\n", i, &subject[i]); + // } + // printf("-------------------------------------------\n"); + // for(int i=0; i<strlen(subject); i++){ + // printf("Value of subject[%d] = %c \n", i, *(subject + i)); + // printf("Address of subject[%d] = %p\n", i, subject + i); + // } + + replace(subject, 'o', 'i'); + + printf("After replacing: %s\n", subject); +} \ No newline at end of file diff --git a/constch.c b/constch.c new file mode 100644 index 0000000000000000000000000000000000000000..311aea9b93f8c70c1b184c43a05085a697c8c182 --- /dev/null +++ b/constch.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +int main() { + const int x = 10; // x is a const variable + int *ptr = &x; // cast away the const-ness + + *ptr = 20; // modify the value of x through ptr + + printf("x = %d\n", x); // The value of x is now modified + + return 0; +} diff --git a/consts.c b/consts.c new file mode 100644 index 0000000000000000000000000000000000000000..c602a8f33b6039cd4ba220a4801096d6645f44be --- /dev/null +++ b/consts.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +int main(){ + int i = 10; //variable + const int j = 20; + + int *danger = (int *)(&j); //cast-away the constness + *danger = 50; + *danger = 100; + + printf("const j = %d", j); +} \ No newline at end of file diff --git a/helloworld b/helloworld new file mode 100755 index 0000000000000000000000000000000000000000..c9def79d1d3e1fd140cfd078c3cdbb140f8f8b10 Binary files /dev/null and b/helloworld differ diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..e653957c09371f587dd842fb78cd0f5e617b92ca --- /dev/null +++ b/main.c @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <unistd.h> +#include "bool.h" +#include "weekday.h" +//int, char, float - primitive +//bool - 0 - false, 1 - true +bool is_holiday(weekday today); + +int main() { + if(is_holiday(SUN)){ + printf("Holiday \n"); + } else { + printf("Not Holiday \n"); + } + +} + +bool is_holiday(today) weekday today=1; { + switch(today) { + case SUN: + return true; + break; + case SAT: + return true; + break; + default: + return false; + } +} \ No newline at end of file diff --git a/memhook b/memhook new file mode 100755 index 0000000000000000000000000000000000000000..6d05044353ea5ece365463cc7d2568cc9f9d36e3 Binary files /dev/null and b/memhook differ diff --git a/memhook.c b/memhook.c new file mode 100644 index 0000000000000000000000000000000000000000..2c59772e436586731801a3c89d2d8b462030dc4b --- /dev/null +++ b/memhook.c @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/uio.h> +#include <unistd.h> +#include <errno.h> + +// Function to write data to another process' memory +void write_data_to_address(pid_t pid, void *remote_addr, const void *data, size_t size) { + struct iovec local[1]; + struct iovec remote[1]; + + local[0].iov_base = (void*) data; + local[0].iov_len = size; + remote[0].iov_base = remote_addr; + remote[0].iov_len = size; + + ssize_t bytes_written = process_vm_writev(pid, local, 1, remote, 1, 0); + if (bytes_written == -1) { + perror("process_vm_writev"); + exit(1); + } else if (bytes_written != size) { + fprintf(stderr, "Partial write: %ld of %zu bytes\n", bytes_written, size); + exit(1); + } +} + +// Function to get the base address of the writable segment from /proc/<pid>/maps +void* get_base_address(pid_t pid) { + char filename[64]; + FILE *f; + void* base_address = NULL; + char line[256]; + + snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); + f = fopen(filename, "r"); + if (f == NULL) { + perror("fopen"); + exit(1); + } + + while (fgets(line, sizeof(line), f)) { + if (strstr(line, "rw-p") && strstr(line, "/var/labsstorage/home/sibidharan/c-for-hackers/helloworld")) { + sscanf(line, "%p-%*p", &base_address); + break; + } + } + + fclose(f); + return base_address; +} + +int main(int argc, char *argv[]) { + if (argc != 3) { + fprintf(stderr, "Usage: %s <pid> <offset>\n", argv[0]); + return 1; + } + + pid_t pid = atoi(argv[1]); + size_t offset = strtoull(argv[2], NULL, 0); // Target offset in the remote process + char *new_string = "Sibid"; + + // Get the base address of the writable segment + void *base_address = get_base_address(pid); + if (base_address == NULL) { + fprintf(stderr, "Could not find base address\n"); + return 1; + } + + // Calculate the real address + void *real_address = (void*)((char*)base_address + offset); + + // Write new string to the target process' memory + write_data_to_address(pid, real_address, new_string, strlen(new_string) + 1); + + printf("Successfully wrote '%s' to address %p in process %d\n", new_string, real_address, pid); + return 0; +} diff --git a/nonop.c b/nonop.c new file mode 100644 index 0000000000000000000000000000000000000000..4ba2dee4d1f23c51cb7314f6dcdf9f9d432601ee --- /dev/null +++ b/nonop.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <unistd.h> + + +int is_holiday(int t){ + switch(t) { + case 0: //SUNDAY + return 1; + break; + case 6: // SATURDAY + return 1; + break; + default: + return 0; + } +} + +int main() { + if(is_holiday(2)){ + printf("Holiday \n"); + } else { + printf("Not Holiday \n"); + } + +} \ No newline at end of file diff --git a/pointer.c b/pointer.c new file mode 100644 index 0000000000000000000000000000000000000000..24e4ff6766f056233d103052ecf2ca614e9f28c3 --- /dev/null +++ b/pointer.c @@ -0,0 +1,50 @@ +#include <stdio.h> + +int increment(int i){ //the data is duplicated into i for processing + i = i + 1; //11 + return i; +} + +void decrement(int *i){ // no new data is created instead just the pointer is stored + *i = *i - 1; +} + +int main() { + int a = 10 b = 15; + int *p = &a; //initialization synatx + int *c = p; // *(&p) + + a = increment(a); // 2 variables are used + + printf("After increment: %d\n", a); // 10 + printf("Value of a: %d\n", a); // 11 + + decrement(&a); //or &a only one variable is used + + printf("After decrement: %d\n", a); + printf("Value of a: %d\n", a); // 11 + + // printf("Value of p = %p \n", p); + // printf("Address of p = %p \n", &p); + + // printf("Value of a = %d \n", a); + // printf("Address of a = %p \n", &a); + + // printf("Value of p = %p \n", p); + // printf("Address of p = %p \n", &p); //this is useless + + // printf("Pointed value of p = %d \n", *p); + + // p = &a; //i change the address stored in p + // *p = 20; //change the value in address stored in p + + // printf("Value of p = %p \n", p); + // printf("Address of p = %p \n", &p); + + // printf("Value of a = %d \n", a); + + // printf("Challenge: %d", *(&a)); + + // printf("%d", *c); + +} \ No newline at end of file diff --git a/weekday.h b/weekday.h new file mode 100644 index 0000000000000000000000000000000000000000..35c07a0f6efca81b1279dde4918d3cff8eeb3420 --- /dev/null +++ b/weekday.h @@ -0,0 +1 @@ +typedef enum day {SUN, MON, TUE, WED, THU, FRI, SAT} weekday; //a set of named integer constants