perl command not found - c

I have a c code and I want take my code a perl command as argument like this:
./code ‫'‪'perl -e 'print "A"x202;'"cat file‬‬
"cat file": command not found
I have cat command in my machine.
could you possibly tell me what the problem is?
thank you in advance

I'm not exactly sure how you want to use the cat command in your case, however, as #ikegami said, you are nesting quotes. In your perl part, use q{} or qq[] to get single or double quotes, and circumvent actually typing them out -- what you use as a delimiter is free with q and qq
use double quotes for your perl script if you can, so what you get is
./code 'perl -e " use q<> or qq++ here "'

It could be that your path is not correctly set inside your code.
I would advice to pass the full cat command path.
You can get the full path of the cat command by typing:
which cat
In my machine the cat command is located in /bin directory, therefore I would give:
./code ‫'‪'perl -e 'print "A"x202;'"/bin/cat file‬‬

Related

Dash and C: eval "$(<cmdfile)" and system("eval \"\$(<cmdfile)\"") giving different results

I would like to use the system() function in C to evaluate an expression within a file cmdfile but I'm not getting the same results as when I do so on the command line directly. The content of cmdfile is the following:
$ cat cmdfile
echo hello
and when I evaluate its content on the command line directly, it works:
$ eval "$(<cmdfile)"
hello
To do the same in C, I'm using system(). This is my code:
$ cat systest.c
#include <stdio.h>
#include <string.h>
int main (int argc, char* argv[])
{
char* cmd = argv[1];
printf("%s\n", cmd);
system(cmd);
return 0;
}
The trouble is that I don't see any output when using the above code:
$ ./systest "eval \"\$(<cmdfile)\""
eval "$(<cmdfile)"
There should be hello printed right after the printf output but it doesn't work. Still, I know that system() is definitely doing something, because if I give it a non-existing filename, dash complains:
$ ./systest "eval \"\$(<cmdfileFF)\""
eval "$(<cmdfileFF)"
sh: 1: cannot open cmdfileFF: No such file
and if I just evaluate echo hello without involving cmdfile, it works too:
$ ./systest "eval \"echo hello\""
eval "echo hello"
hello
I'd like to know what is causing this difference in behaviour. Is there any other way of executing the content of cmdfile in dash? I'm restricted to only using the built-in commands of dash on the command line, so options such as ./systest "eval \"\$(cat cmdfile)\"" are not possible. Further, the expansion of "$(<cmdfile)" should only happen within system(), not before (thus ./systest "eval \"$(<cmdfile)\"" won't work.
I tested this with dash 0.5.10.2-6 and dash 0.5.8-2.1ubuntu2.
Thank you for any insight!
Edit
Thanks to Jonathan Leffler's comment, I now realise that dash doesn't understand the $(<file) syntax. So what would be a dash-compatible equivalent?
Wrap-up
So my confusion was due to the fact that system(...) always uses /bin/sh, but when testing my expressions on the command line, I was accidentally invoking bash instead of dash. Hence the results were different.
$(< …) substitution isn’t POSIX-sh-compatible, but your sh is restricted to about that. A general alternative is to replace < cmdfile with cat cmdfile:
./systest "eval \"\$(cat cmdfile)\""
but I think dot-sourcing is equivalent in this case:
./systest '. ./cmdfile'
The proper fix is to put a shebang line in the script and mark it as executable.
#!/bin/sh
echo "hello"
The shebang needs to be the absolutely first line of the file (its first two bytes should be # and !). The quoting around the argument to echo is not strictly necessary here, but good practice. (See also When to wrap quotes around a shell variable?)
Changing the permissions only needs to be done once, when you have just created the file:
chmod +x ./cmdfile
Now, you can simply use
system("./cmdfile")

How to convert 'ls' command to 'cat' command?

I am trying to solve this problem, I am only allowed to run ls command and my objective is to read the flag. There is a vulnerable C code which has setuid.
-rwsr-sr-x 1 lameprogrammer lameprogrammer 8579 Sep 15 07:21 vul_c
-rw-r----- 1 lameprogrammer lameprogrammer 154 Sep 15 07:40 flag
I am user attacker and I have to read this flag file. The given C code is
#include <stdlib.h>
#include <stdio.h>
#define FILENAME "/var/challenges/attacker/challenge1/flag"
int main(void)
{
int vert;
vert = system("ls " FILENAME);
if(!vert)
puts("Flag is at " FILENAME " :P ");
else
puts("Sorry! no file is there");
}
I was trying to convert ls into cat so that if that runs then it will read the flag file. To do that I copied all the bin folder into my local space and then I replaced ls with cat and then exported the new PATH. Technically it should replace and my ls command should work like cat but it is not working. The following are my command :
cp -r /bin /home/attacker
cd /home/attacker/bin
rm ls
cp cat ls
export PATH=/home/attacker/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/usr/games
The ls command is not working as cat and output is:
ls: unrecognized option '--color=auto'
Try 'ls --help' for more information.
When I am trying to run ./vul_c it says permission denied and then it is printing Sorry! no file is there.
Any help onto this would be great.
You are in the right direction but you are just putting too much effort for this small thing. Instead of creating a program why don't you create a symlink and then point this to cat.
Try this step:
ln -s /bin/cat ls
Finally, export this and I hope you will be done with your solution. In this, you don't even have to worry about permission. Let me know if it works.
Does the program work? The problem as I see it is that if you use ls in the shell, is that it has an alias that will enable the colouring, i.e. something like
alias ls='ls --color=auto'
Now this is something that cat wouldn't understand. But it would be only for your shell, not the ls command run by that script, because it wouldn't use the aliases. Perhaps something like unalias ls would help in the shell.
Now, the system function cannot run your ls because it doesn't have proper rights - you forgot to chmod +x ls.
Try the following command:
unalias ls

Run C program from shell script [duplicate]

I have a script in unix that looks like this:
#!/bin/bash
gcc -osign sign.c
./sign < /usr/share/dict/words | sort | squash > out
Whenever I try to run this script it gives me an error saying that squash is not a valid command. squash is a shell script stored in the same directory as this script and looks like this:
#!/bin/bash
awk -f squash.awk
I have execute permissions set correctly but for some reason it doesn't run. Is there something else I have to do to make it able to run like shown? I am rather new to scripting so any help would be greatly appreciated!
As mentioned in #Biffen's comment, unless . is in your $PATH variable, you need to specify ./squash for the same reason you need to specify ./sign.
When parsing a bare word on the command line, bash checks all the directories listed in $PATH to see if said word is an executable file living inside any of them. Unless . is in $PATH, bash won't find squash.
To avoid this problem, you can tell bash not to go looking for squash by giving bash the complete path to it, namely ./squash.

How to split lines in a file, and have the output names be based on those lines

I am using CentOS. I have a file that contains information like:
100000,UniqueName1
100000,UniqueName2
100000,UniqueName4
100000,SoloName9
I want to split this out into files, one for each line, each named:
[secondvalue]_file.txt
For an example:
SoloName9_file.txt
Is it possible to split the file in this fashion using a command, or will I need to write a shell script? If the former, what's the command?
Thank you!
Here's one approach. Use the sed command to turn this file into a valid shell script that you can then execute.
sed -e 's/^/echo /g' -e 's/,/ >/g' -e 's/$/_file.txt/g' <your.textfile >your.sh
chmod +x your.sh
./your.sh
Note that trailing whitespace in the file would take some additional work.
Writing it into a shell script file gives you a chance to review it, but you can also execute it as a single line.
sed -e 's/^/echo /g' -e 's/,/ >/g' -e 's/$/_file.txt/g' <your.textfile | sh

sed command works fine under shell terminal, but fails in 'system()' call under C code

I'm trying to delete some special lines in a log file, so I use sed of busybox on an embeded linux system.
# sed
BusyBox v1.18.4 (2013-01-16 16:00:18 CST) multi-call binary.
Usage: sed [-efinr] SED_CMD [FILE]...
Options:
-e CMD Add CMD to sed commands to be executed
-f FILE Add FILE contents to sed commands to be executed
-i Edit files in-place (else sends result to stdout)
-n Suppress automatic printing of pattern space
-r Use extended regex syntax
If no -e or -f, the first non-option argument is the sed command string.
Remaining arguments are input files (stdin if none).
execute the following command under shell and everything works fine:
export MODULE=sshd
sed "/$MODULE\[/d" logfile
but if I try to use the following C code to accomplish this:
char logfile[] = "logfile";
char module_str[] = "sshd";
char env_str[64] = {0};
int offset = 0;
strcpy(env_str, "MODULE=");
offset += strlen("MODULE=");
strcpy(env_str + offset, module_str);
putenv(env_str);
system("sed \"/$MODULE\[/d\" logfile");
when executing the a.out, I got the error message:
sed: unmatched '/'
what's wrong with my 'system()' call? I'm totally a newbie in text processing, so anybody can give me some clue? Thanks.
Best regards,
dejunl
straight off I can see that the \ before the [ is going to be swallowed by 'C'
so you'll need to double it,
system("sed \"/$MODULE\\[/d\" logfile");
But the shell might want to swallow the one that's left swallow that one so double it again
system("sed \"/$MODULE\\\\[/d\" logfile");
of course system("sed \"/$MODULE\\[/d\" logfile"); can't be sure I'm reading the question you posed. try it with echo instead of sed and adjust it until the string comes out as you want sed to see it.

Resources