11 ms·
Detecting the use of “curl – bash” server side (2016)
- jjgreen 6y agoAnyone dare to incur the wrath of the Rust contingent by linking to the elephant in the room? OK then, I will https://www.rust-lang.org/tools/install https://www.rust-lang.org/tools/install
- RMPR 6y agoIn the meantime all the distributions (at least the major one) have Rust in their official repositories.
- est31 6y agoUnless you are on a rolling release distro, you can run into problems with the rust compiler package being out of date, as the rust ecosystem quickly starts requiring newer versions. 500 crates for a medium sized project are not unheard of, and if one of the maintainers decides to adopt a new feature, you run into issues. That's why in order to write Rust, you'll likely need rustup, which is packaged on way less distros than the rust compiler is. Debian for example doesn't have an official rustup package.
- jjgreen 6y ago> That's why in order to write Rust, you'll likely need rustup It seems that way, which is a big disincentive to use Rust at all.
- lock-free 6y agoThe same issues exist for other languages and runtimes so I'm not sure why Rust having a standard solution is a disincentive. How many ways are there to ruin a python environment?
- avhon1 6y agoThe issue is worse with Rust, which is still a young and rapidly-changing language. Debian stable currently packages rustc 1.41.1 (February 2020), which is 11 releases behind the most recent 1.48.0 release. The older compiler's differences after 10 months include: * doesn't have subslice patterns * has `Error::description`, which has since been deprecated * doesn't have x86 CPU feature detection * lacks fix for unsound typecasts between integers and floats * lacks fix for incomplete constant propagation * doesn't let you use conditionals, loops, match, $$, or || in constant fns, or cast arrays to slices in constant fns * has less-helpful error messages and backtraces * doesn't let you implement traits on arrays with lengths >32 * has worse type inference, this (valid since 1.43.0) code doesn't compile because 0.0 and &0.0 are f64s: let n: f32 = 0.0 + &0.0; * requires you to import standard libraries to use predefined numeric constants (like MAXINT or NaN) * doesn't support Control Flow Guard on Windows * doesn't generate documentation links from classpaths (you have to write relative file paths instead) and numerous libraries being pre-stable. For code that you're writing yourself, not having these features is mostly just a quality-of-life issue: you'll have to do things that you wouldn't have to do if you had the latest compiler. For code that you're collaborating on, having an 10-months-older compiler can be a showstopper: the code might just not compile at all, and very likely the only fix will be to uninstall your system-packaged rustc and install the version from rustup. Contrast this with C/C++ development, where your system's pre-packaged compilers are much more likely to be sufficiently up-to-date. These languages are more mature than Rust: they change slower, and new features are adopted by programmers more gradually. For example, the Linux kernel only requires GCC 4.9 [1], which was released in 2013. Debian stable supplies GCC 8.3, which will probably be able to compile unmodified kernel source code for many years without being updated. [1] https://www.kernel.org/doc/html/latest/process/changes.html https://www.kernel.org/doc/html/latest/process/changes.html
- lock-free 6y agoI can contrast with C/C++ development, where C++17/20 are not available by default on many distros because package managers lag so far behind the curve and new projects get tied to outdated versions. I just don't really see this as a problem - Rust really isn't that much of a moving target, and the benefit is there is one solution to the problem: use Rustup to manage your Rust installation. That is significantly more robust than "use your package manager."
- richardwhiuk 6y agoOr avoid distributions like the former CentOS.
- gspr 6y agoYikes.
- kzrdude 6y agoWhat, specifically is wrong with it? The article writes this is obviously bad, but I think it needs to be expanded upon, it's not obvious.
- beojan 6y ago`curl | bash` downloads an installation script and runs it as it's being downloaded, with no opportunity to read the script and ensure it does nothing malicious, and has no inadvertent bugs that could cause damage.
- taejo 6y agoJust like if I'd done `curl > install.sh; bash install.sh` without reading install.sh or installed a binary package, both of which are done every day by people with work to do and without the time, skill and inclination to audit millions of lines of source code.
- ciceryadam 6y agoor curl --output instead of using stdout
- willeh 6y agoAs opposed to downloading a binary and running it? We don't verify what the binaries we download contain, why do it for a bash script?
- dastx 6y agoI suppose the difference is iff people do very script before they run, this could bypass it. But I think, those people would definitely download it, inspect it, then run the downloaded script.
- VBprogrammer 6y agoI think it would take next to no effort to hide something in a bash script in such a way that it passed casual inspection even by an expert. See code obfuscation competitions for examples.
- est31 6y agoThe suggestion of the website is certainly not great, but note that: * It has been improved a little by the https and tls 1.2 parameters compared to earlier versions that didn't have these params * You are not required to install rustup that way. There are many rustup packages available in distros today. Arch has one, nix OS has one. Debian is dragging their feet [0] but they aren't entirely opposed to the idea, so maybe something will change about this in the future. * Even if the download process is secured, rustup doesn't really verify any signatures, it just downloads what's inside the manifest. So you can download potentially malicious code if the S3 bucket is hacked that rustup downloads the compiler from. * Even if the entire process of obtaining rustc is secured, the standard way of using rust + cargo basically gives maintainers access to your local computer through build.rs and other methods. You can sandbox the cargo invocation, but who does that? [0]: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=955208 https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=955208
- deleted 6y ago[deleted]
- maple3142 6y agoSo it is safer to use `sh -c "$(curl https://whatever.com/install.sh https://whatever.com/install.sh)"`?
- tobyhinloopen 6y agoNo. It’s safer to download the file, inspect it manually, and then run it.
- hvdijk 6y agoOnly mildly so. This hides from the server the fact that you are not checking what is received, so prevents the server from adapting its results based on that, but still allows a server to just unconditionally serve a malicious script. The safe way is to save to a file, actually inspect the file to make sure there is nothing malicious in there, then run that file.
- dang 6y agoIf curious see also 2018 https://news.ycombinator.com/item?id=17636032 https://news.ycombinator.com/item?id=17636032 Discussed at the time: https://news.ycombinator.com/item?id=11532599 https://news.ycombinator.com/item?id=11532599
- adriancr 6y agoThat's probably why you should: - download the bash script - inspect the script / add a 'set -x' for tracing - then run (the downloaded script) with log output somewhere.
- dastx 6y agoset -x won't do much though. All it will tell you is what it did, but by the time you realise, the damage could already be done.
- adriancr 6y agothat's why you download and inspect the script first. and you run the downloaded script, never curl|bash
- richardwhiuk 6y agoAnd then just leave it to install whatever binary it did.
- mehrdadn 6y agoIf you want to enable tracing you can just bash -x
- londons_explore 6y agoThe difference between a malicious and a legit bash script can be just a couple of characters. Eg. the script can have a deliberate injection vulnerability in a variable expansion... If people commonly inspected bash scripts they downloaded, attackers would just write the script more carefully to make sure 15 minutes reading the script wouldn't find anything bad going on.
- prepend 6y agoFortunately it’s not common, so they don’t. If it became common and they did, I could inspect the script using automated tools that check for obfuscation, etc. I think it’s not about eliminating, but making a little better. With lots of this stuff, it’s just about being faster than the other guys that the bear is chasing. It’s kind of moot because all this is solved by “don’t download and run scripts from strangers.” Then you just have to worry about someone rooting homebrew. I don’t like this line of thought because it ultimately gets us to a Microsoft world where everything is signed and there’s still tons of crap getting through. I’d rather have it where scripts can be individually assessed and run without lots of expensive stuff in the middle. Even if that means some risk.
- jcynix 6y agoCool concept. But "curl | less" will display those null chars, so I'd use many shell comment lines instead to fill tcp buffers. Which again will fill the reader's page and make the pager stop for me to read, which might take more than 10 seconds. Seems it not _that_ easy to be nasty?
- RMPR 6y agoI already knew it was dangerous but TIL. Iirc there is an online service (I can't remember the name) detecting if a script is malicious does it work in this case?
- csunbird 6y agoThat was my first thought too. Someone can return legit script when it is not "curl | bash" and return a malicious one otherwise.
- steerablesafe 6y agoI'm in no way advocating for curl | bash, but one way to defeat the detection would be to use "sponge" from moreutils[1]. curl "https://example.com/install" | sponge | bash [1] https://manpages.debian.org/testing/moreutils/sponge.1.en.html https://manpages.debian.org/testing/moreutils/sponge.1.en.ht...
- m463 6y agoit's like half of tee
- steerablesafe 6y agoI don't think tee buffers all its input before writing to its outputs.
- lqet 6y ago> This allows constructing pipelines that read from and write to the same file. TIL, how did I not know about sponge before?
- OskarS 6y agosponge rules, but you need to be careful. Lets say you wanted to search/replace a file with sed and sponge, then this does not work: cat file.txt | sed "s/a/b/g" | sponge > file.txt Because bash opens file.txt for writing and truncates it immediately, so "cat file.txt" wont output anything. You have to do this for it to work: cat file.txt | sed "s/a/b/g" | sponge file.txt sponge wont open the file for writing until it's sponged everything up. If you remember this, sponge is an awesome tool in the UNIX toolbox.
- bonoboTP 6y agoOf course with sed you'd use the -i flag.
- jniedrauer 6y ago
- est 6y agoAre there any good http client classifier tools for servers?
- thombles 6y agoThose advocating we inspect the downloaded script surely wouldn't rely on plain old "cat" would they? :) https://www.infosecmatter.com/terminal-escape-injection/ https://www.infosecmatter.com/terminal-escape-injection/
- VBprogrammer 6y agoPfft, that would be a Useless Use of Cat[1]. [1]http://porkmail.org/era/unix/award.html#cat http://porkmail.org/era/unix/award.html#cat
- ImaCake 6y agoDoes it matter if it's bad form if every terminal noob does it anyway? An attacker using this vector certainly wouldn't care - so long as the attack works. There's definitely a place for poking fun at Unix noobs - I have learnt better tools than cat because of it - but it is not terribly relevant here.
- VBprogrammer 6y agoIt's actually one of the few cases where there is a functional difference between using cat and the alternative. Using less instead wouldn't execute the escape sequences.
- turminal 6y agoUnless the user has less aliased to `less - r` which many users do. I wouldn't be surprised if there existed some obscure distro that ships this alias in the default bashrc
- oblio 6y agoIt's not even a noob thing. I've been using using *NIX for 15+ years and I still don't subscribe to this "useless use of cat" pedantry. Without cat I need to use redirections or find the parameter for each command I use, in order to get things from a file. cat bla | command1 | command2 is much nicer to write and read, much more logical and much easier to modify if needed. "Useless use of cat" is a micro-optimization. Use if you want or need, don't turn it into dogma or even better, newbie shaming (yay for gatekeepers!).
- rnijveld 6y agoI wonder why there is such a focus on this `curl|bash` pattern. Meanwhile most of us are downloading hundreds of thousands of lines of code using all kinds of package managers and I don't see many inspecting all those downloaded files, especially not manually. I don't think anyone would ever get to doing anything other than checking if you really want to verify everything. I'm not saying that downloading something from your official OS package repositories is the same as downloading a random URL from the internet. The thing I'm more thinking about is language specific package managers such as NPM, Composer and Cargo. Or user repositories, things like AUR, PPAs and non-official apt repositories, where any random person can put something up. The thing for those is that they almost look like they are something official and something to be trusted. Often times they are displayed on an official site, you download them from a trusted URL and they look like they are really secure, even with hashing and things like that built-in. Lots of package managers don't support any way of verifying the identity of the one uploading the files, and even if they do we often import signing keys into our chain of trust without a moment of thought or we don't use the signing mechanism at all. And with something like NPM packages you are likely to download another few dozen of other packages which you didn't even intent on downloading. You will probably run a lot of code there that could be doing all kinds of horrible things. At least with `curl|bash` I get some feedback of where the code is originating from, what URL will I be downloading something from and is that some place that I can trust. At least I get somewhat of an identity verification (albeit very very weak) as long as I trust the owner of the site to protect it adequately from preventing unauthorized uploads.
- blueblisters 6y ago> And with something like NPM packages you are likely to download another few dozen of other packages which you didn't even intent on downloading. You will probably run a lot of code there that could be doing all kinds of horrible things. This got me thinking - how would easy would it be to orchestrate a dependency based attack that would cripple a large number of applications - for example with the help of a maintainer of a popular open-source project gone rogue? Do large tech companies frequently audit the 3rd party code that goes into their applications or is it largely based on trusting the open-source maintainer?
- unglaublich 6y agoYou could as well send a different response the second time some IP+user-agent visits the resource. That would be almost as effective. Just download the script, inspect the code, run the code.
- mschuster91 6y agoOne can also run `curl | pv > tmp.sh` to spot this pattern.
- FrozenVoid 6y agoi don't install anything that comes in shellscript download, this speaks volumes on security of the 'software' itself.
- ciceryadam 6y agoSlightly related, fun with copy and pasting into terminal: copy&paste&getpwned - https://web.archive.org/web/20170421210256/http://hysteria.cz/niekt0/ohm/cpagp.pdf https://web.archive.org/web/20170421210256/http://hysteria.c...
- theandrewbailey 6y agoSounds like you could have bash sleep long enough for your server to time out and close the connection before the rest of the script gets downloaded.
- tgbugs 6y agoI have spent a stupid amount of time trying to come up with a somewhat more secure approach and wound up with `scurl` which requires a cypher, a checksum, a url that points to an immutable file, a target path, always writes the output to disk, and will not write to the target path if the checksum does not match [0]. For an extra level of paranoia add a check to ensure that the target directory exists and is only writeable by the user currently running the command. As far as I can tell there isn't a simpler way to do this that doesn't have some hidden pitfall, and I'm sure even this way has pitfalls, because I only just realized that I had not accounted for the race condition on the target path. There are variants of this which can stream, and there was even some work to try to integrate this functionality into curl itself [1], but they are 1) not portable 2) more complex, longer, and harder to audit and 3) don't protect you anyway. Why not? Because you can't know that a file doesn't match until you read the last byte, at which point all of those bytes will already have been piped into bash. Oops! I have also spend a stupid amount of time trying to figure out a reasonable workflow that streamlines auditing and update of the immutable links used in this pattern [2]. The same issue is faced by anyone who pins their dependencies. Auditing dependencies is a huge bottleneck, to the point where it is often neglected entirely and/or has the effect of preventing updating dependencies altogether. Immutability of the data behind the link is critical to prevent checksum failures due to legitimate changes to the underlying data, but also means that you can't take advantage of being able to point users to a single unchanging url. For one type of security the name has to change with the content. For usability and another type of security it is vital to be able to quickly push updates. If you have users with an immutable url pointing to a bad version the only thing you can do is take down that version, but you can only do that if you control the server. If you point to a raw github url that bad version will never go away. Thus it is critical to have tooling in place that makes it easier for users (aka other developers) to find and update those immutable urls in a way that minimized the audit load. The sandbox wont save you this time! 0. https://github.com/tgbugs/orgstrap/blob/master/get-emacs.org#posix-bootstrap https://github.com/tgbugs/orgstrap/blob/master/get-emacs.org... 1. https://github.com/curl/curl/issues/1399 https://github.com/curl/curl/issues/1399 2. https://github.com/tgbugs/orgstrap/blob/master/reval.org https://github.com/tgbugs/orgstrap/blob/master/reval.org 3. https://blogs.sciencemag.org/pipeline/archives/2008/02/26/sand_wont_save_you_this_time https://blogs.sciencemag.org/pipeline/archives/2008/02/26/sa...