0

So I want to delete all files in my current directory using the following pipeline:

ls | sed -rn 's/(.*)\.jpg$/mv -n & \1.jpeg/pi' | sh

How to create a file, named so that running the above pipeline deletes all files in the current directory. Thus through code injection.

Please, give the command used to create that file.

  • What..? What is the sh for? – DisplayName Dec 14 '14 at 22:08
2

The command you have above will (somewhat clumsily) rename all files in the current directly from *.jpg to *.jpeg, it could be modified to delete all files but is hardly appropriate to the task.

However, it sounds like you are trying to craft a suitable filename such that when the above command encounters it, it will delete everything in the current directory instead. This is certainly achievable (although it makes one question your motives...).

DISCLAIMER: You are probably going to do damage with this. That's all on you, test first so you don't ruin something to care about, and expect legal action against you from your target if you are caught maliciously doing this on their system.

Now that's out of the way, you want to create a file that matches the sed input but creates a suitable output to still do what you want.

Create a file called ;rm *;.jpg (or ;rm -rf *;.jpg for a more aggressive removal). You can run this through just the sed part to see what it will do via:

  • echo ";rm * ; .jpg" | sed -rn 's/(.*)\.jpg$/mv -n & \1.jpeg/pi'

EDIT: As a side note, that command you've listed can be made to execute (almost) anything you like as the user running it, a delete of all files is a rather unimaginative (and destructive) use of this weakness. Maybe consider something more colourful (and easier to reverse)?

  • no, it is for educational purposes or in other words exercise – Highlights Factory Dec 14 '14 at 22:48
  • 1
    Fair enough then :) I certainly agree that the best way to defend against a black hat is to think like one. – Smiling Dragon Dec 14 '14 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.