by 1vuio0pswjnm7 · 3y ago · 0 comments
echo https://example.com|Connection=keep-alive yy025|nc -vvn 127.127 80
yy025 sets the HTTP Connection header according to the value of the corresponding environmental variable. If I want to send a POST request I set the variable httpMethod to POST. The address 127.0.0.127 is a TLS forward proxy. Original netcat allows one to type 127.127 without the zeros.Using djb's envdir utility, one can put a selection of different HTTP headers into a directory so that there's no need to type them on the command line.
cd /path/to/dir
echo close > connection
echo aplication/x-www-form-urlencoded > content-type
echo id=123\;key=xyz > cookie
echo 19 > content-length
echo POST > httpMethod
echo /api/whatever > path
echo "{ \"key\": \"value\" }" > post-data
cd
echo https://example.com \
|if cd /path/to/dir;envdir . yy025;then
cat post-data;fi \
|nc -vvn 127.127 80
It's strange to me to see this author use printenv instead of set (a built-in) to display Bourne shell environmental variables. As someone who uses Bourne shell every day and on a variety of computers, I never use printenv. On the smaller systems I make, I do not include printenv in the userland.The use of fgrep to match a single word in all caps is also strange.
Maybe I am just missing some UNIX wisdom here. If so, please disregard this comment.
From the article
$ export FRED=value
$ printenv | fgrep FRED
FRED=value
$ FRED=
$ printenv | fgrep FRED
FRED=
$ unset FRED
$ printenv | fgrep FRED
$ # ie, no output from printenv
Without using printenv or grep -F (in shell script /bin/fgrep) $ export FRED=value
$ set|grep FRED
FRED='value'
FRED=
$ set|grep FRED
FRED=''
$ unset FRED
$ set|grep FRED
$ # ie, no output from set(I am the author of the linked-to entry.)
FRED=barney; set | grep FRED
Also there is no need for grep, nevermind grep -F (fgrep), if using printenv. FRED=barney printenv FRED
Without semicolon sh -c 'FRED=barney set|grep FRED'
echo 'FRED=barney set|grep ^FRED'|shIt's not missing. PP is making a point about "single-command variables."
$ FOO=BAR env | grep FOO # Exists as an environment variable passed to env
FOO=BAR
$ env | grep FOO # But only for that command
$ FOO=BAR # Creates a variable (not an environment variable)
$ env | grep FOO # As shown by not being passed down
$ echo $FOO # But still accessible in this context
BAR
$ export FOO=BAR # Promotes it to an environment variable (the "=BAR" is optional since it's already defined)
$ env | grep FOO # And as such is now visibly passed down
FOO=BAR
(Yes, bash does mix these together so it's easy to accidentally replace something you don't intend to)If you add a semicolon then FRED is no longer a single command variable (it persists)
> on NetBSD, one can do this
Linux also does the expected thing:
$ FRED=barney printenv FRED
barney
Without using printenv or grep -F (in shell script /bin/fgrep)
$ export FRED=value
$ set|grep FRED
FRED='value'
$ FRED=
$ set|grep FRED
FRED=''
$ unset FRED
$ set|grep FRED
$ # ie, no output from set
(Missed a dollar sign.)Another thing is that printenv does not make it easy to see spaces or other nonprinting characters in the values of environmental variables. For example PS="# " displays as PS=#. TAB=$(printf '\11') displays as TAB=. For me, that's not really helpful. Whereas the builtin set command displays the values in single quotes so one can easily the presence of spaces and other non-printing characters.
A workaround is to use printenv with sed, something like
printenv|sed -n l
If I was going to investigate "single command environmental variables" I might just use getenv and printf. That way I can see the non-printable characters. But not being an IT person or a programmer, maybe I am missing the reason why printenv, written in 1979, would be preferable to getenv. (That, IMO, would be an interesting blog post.) For example, int printf(const char *__restrict, ...);
char *getenv(const char *);
int main()
{
if(getenv("FRED"))
printf("%s=%s\n","FRED",getenv("FRED"));
return 0;
}
usage: FRED= a.outSo it's just not immediately obvious to many why a simple extra space is a problem at all, and why it's this specific error. It can also just generally be hard to see past what you intended to write to what you actually did and how that will be parsed.
But otherwise yeah, I also don't see this as obscure at all. Was expecting something confusing when, say, fiddling with arrays or something.