I am trying to run this piece of vulnerable C code:
#include <stdio.h>
#include <stdlib.h>
int add(int x, int y)
{
int z =10;
z = x + y;
return z;
}
main(int argc, char **argv)
{
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int c;
char buffer[100];
gets(buffer);
puts(buffer);
c = add(a,b);
printf("Sum of %d+%d = %d\n",a, b, c);
exit(0);
}
I am trying to get past the segmentation fault so that I can input the integers but the segmentation fault prevents that. In the terminal I have tried:
gcc -ggdb -fno-stack-protector -U_FORTIFY_SOURCE -Wa,--execstack -o SimpleDemo SimpleDemo.c
I still get a segmentation fault. I am lost as to what to try next. As you can probably tell, I am an ubuntu newb. The bash code I am using comes from here:
http://www.evanjones.ca/buffer-overflow-101.html
I have been at this for while so would really appreciate some help
Cheers
Just explaining what hmjd is asking you to do.
Run (binary) 10 20
Related
I am using the GMP. My program can build successfully, But run failed. The following is error things:
a=1231231231231231
res^n != a
Segment fault
All codes in my program is:
#include <gmp.h>
#include <stdio.h>
int main()
{
mpz_t a,res;
unsigned long int n = 123;
char str1[] = "1231231231231231";
mpz_init_set_str(a, str1, 10);
gmp_printf("a=%Zd\n",a);
mpz_init(res);
if(mpz_root(res, a, n)){
printf("res^n == a\n");
}
else{
printf("res^n != a\n");
}
mpz_clears(a,res);
return 0;
}
You have to call mpz_clears() like:
mpz_clears(a,res, NULL);
Here's what the documentation says:
Function: void mpz_clears (mpz_t x, ...)
Free the space occupied by a NULL-terminated list of mpz_t variables.
I have a little C program.
#include <stdlib.h>
#include <stdio.h>
void counter(int n);
int main(int argc, char *argv[])
{
int n = atoi(argv[1]);
counter(n);
return 0;
}
void counter(int n)
{
printf("%d\n", n);
if(n == 0) {
return;
}
counter(n - 1);
}
When I enter 200000 as the command line argument it works well. But with 300000 it says: 17623 segmentation fault (core dumped).
With gdb, the error is: Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a6f198 in _IO_new_file_write (f=0x7ffff7dd0760 <_IO_2_1_stdout_>, data=0x555555756260, n=6) at fileops.c:1196 1196 fileops.c: No such file or directory.
I don't know what is wrong. Should I allocate memory or something like that?
So I'm getting to know valgrind a bit and I've come across an example that has me confused.
I was shown this code causes a Invalid Write in valgrind:
#include <stdlib.h>
#include <stdlib.h>
int main(int argc, const char* argv[])
{
int index = atoi(argv[1]);
char *x = malloc(10);
x[index] = ‘f’;
return 0;
}
while this code causes a segfault in valgrind:
int foo(y)
{
char str[10];
str[11] = ‘f’;
return 0
}
So do these examples have anything to do with the fact that example 1 is something thats done on the heap while the 2nd example is done on the stack?
Am I missing something bigger?
Thanks
I'm getting a segmentation error (core dump) when I try to run this. It compiles perfectly but I get the error, and I don't know why. There must be a problem with a file writing because without this works good. Any help would be great. Thanks!
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <crypt.h>
#include <string.h>
int
main(void)
{
FILE *f=fopen("shadow1.txt","w");
if (f=NULL)
{
printf("ERROR");
}
unsigned long seed[2];
char salt[] = "$1$........";
const char *const seedchars =
"./0123456789ABCDEFGHIJKLMNOPQRST"
"UVWXYZabcdefghijklmnopqrstuvwxyz";
char *password;
int i;
/* Generate a (not very) random seed.
You should do it better than this... */
seed[0] = time(NULL);
seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
/* Turn it into printable characters from ‘seedchars’. */
for (i = 0; i < 8; i++)
salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
/* Read in the user’s password and encrypt it. */
password = crypt(getpass("Password:"), salt);
/* Print the results. */
//fprintf(f,"%s $ %s",password);
printf("Success Registration to file !");
fclose(f);
return 0;
}
if (f=NULL)
{
printf("ERROR");
}
was the problem...
void Register(char u,char p) {
you probably want these to be char * because of the fprintf that treats them as strings:
fprintf(f,"%s $ %s",u,p);
and since you pass char *s in:
char *password,*username;
//...
Register(username,password);
This would most likely have been caught by compiler warnings. It is a lot faster to get your answer from the compiler than from here.
If you can't figure out why your program isn't working, you can enable all the warnings you should need with -Wall -Wextra and turn warnings into errors with -Werror.
You are not allocating space to hold username so it will segfault on the scanf.
I am a bit new to pointers. I am receiving this error while running the code below specified using gcc compiler in ububtu.
Error:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7de7c13 in _dl_fini () at dl-fini.c:235
235 dl-fini.c: No such file or directory
code
.
#include <stdio.h>
#include <cmath>
#include<stdlib.h>
void update(int *a,int *b) {
int *c;
*c=*a;
*c=abs(*a+*b);
*b=abs(*a-*b);
*a=*c;
}
int main() {
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
please post any suggestions/solutions regarding this.
I suspect this is what you were trying to do.
void update(int *a,int *b) {
int c;
c=*a;
c=abs(*a+*b);
*b=abs(*a-*b);
*a=c;
}
The problem is that you're never allocating memory for that pointer c declared in your update() function:
int *c; // <<< not initialized
You must allocate memory to use that pointer:
int *c = new int();
and free it when leaving your function:
delete c;
The easier way of course is just to have a variable
int c = *a;
on the functions local stack, and omit the dereferencing:
c=abs(*a+*b);
// ^ Note the * was removed
Due to the fact your tagged your question with c++ and your code has nothing to do with c++ here is a real c++ code:
#include <iostream>
#include <cmath>
void update(int &a,int &b) {
int c;
c=a;
c=abs(a+b);
b=abs(a-b);
a=c;
}
int main() {
int a, b;
std::cin >> a;
std::cin >> b;
update(a, b);
std::cout << a << std::endl << b << std::endl;
}
I strongly recommend you use some compiler flags which would have already warn you while the compilation and even the code would not have been generated.
For g++ my suggested options are:
-std=c++11 -Wall -Werror -Wextra