Skip to main content
added 23 characters in body
Source Link
Petr Skocik
  • 29.7k
  • 18
  • 90
  • 154

Your approach is perfectly fine:.

Here's a C example that embeds catcat, copiesand upon execution, writes it to a tempfile, and marks it executable:

//$ xxd --include /bin/cat
//^ create _bin_cat and _bin_cat_len
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
int main(){
  //TODO: error checking everywhere
  char tmp[] = "/tmp/cat-XXXXXX"; 
  mkstemp(tmp);
  FILE* f = fopen(tmp, "w");
  fwrite(_bin_cat, _bin_cat_len, 1, f);
  fchmod(fileno(f), 0700);
  puts(tmp); //print the name of where the embeded cat got copied to
  return 0;
}

Works fine for me.

Your approach is perfectly fine:

Here's a C example that embeds cat, copies it to a tempfile and marks it executable:

//$ xxd --include /bin/cat
//^ create _bin_cat and _bin_cat_len
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
int main(){
  //TODO: error checking everywhere
  char tmp[] = "/tmp/cat-XXXXXX"; 
  mkstemp(tmp);
  FILE* f = fopen(tmp, "w");
  fwrite(_bin_cat, _bin_cat_len, 1, f);
  fchmod(fileno(f), 0700);
  puts(tmp);
  return 0;
}

Works fine for me.

Your approach is perfectly fine.

Here's a C example that embeds cat, and upon execution, writes it to a tempfile, and marks it executable:

//$ xxd --include /bin/cat
//^ create _bin_cat and _bin_cat_len
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
int main(){
  //TODO: error checking everywhere
  char tmp[] = "/tmp/cat-XXXXXX"; 
  mkstemp(tmp);
  FILE* f = fopen(tmp, "w");
  fwrite(_bin_cat, _bin_cat_len, 1, f);
  fchmod(fileno(f), 0700);
  puts(tmp); //print the name of where the embeded cat got copied to
  return 0;
}

Works fine for me.

Source Link
Petr Skocik
  • 29.7k
  • 18
  • 90
  • 154

Your approach is perfectly fine:

Here's a C example that embeds cat, copies it to a tempfile and marks it executable:

//$ xxd --include /bin/cat
//^ create _bin_cat and _bin_cat_len
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
int main(){
  //TODO: error checking everywhere
  char tmp[] = "/tmp/cat-XXXXXX"; 
  mkstemp(tmp);
  FILE* f = fopen(tmp, "w");
  fwrite(_bin_cat, _bin_cat_len, 1, f);
  fchmod(fileno(f), 0700);
  puts(tmp);
  return 0;
}

Works fine for me.