1

We used to edit a payload script via the terminal on an old server.

To ease up and avoid typos I'd like to design a php script that generates the payload, save it to a file and use the built-in FTP functionality in PHP to upload the file to the FTP server and chmod the script to 755.

But my generated payload is not particularly liked by the server. It doesn't run.

#./payload.sh 
-sh: ./payload.sh: not found
#

However after doing a simple dos2unix payload.sh, everything runs fine.

#dos2unix payload.sh    
#./payload.sh 
[OK] running payload....
#

How can I automatically sculpt my file to avoid needing to dos2unix it every time? I've tried the obvious KISS method:

$contents = str_replace("\r","",$contents);
$contents = trim($contents);

No luck. Script still doesn't run.

So I took a peek into the dos2unix.c and dos2unix.h and found out it was seemingly not all that simple as I thought. There was 800-odd lines of code.

Is there any more simple stuff I can do in PHP? FWIW, I have to admit that for the time being, I've been using manual typed files written in Sublime Text 2 on my Mac. I did try to "Save with Encoding" / "UTF-8" and did select "Unix" as "Line Endings".

EDP
  • 239
  • 6
  • 18
  • 1
    `system("dos2unix $script")` job done. Though admittedly it doesn't help you understand why. Can we see the first few lines of the shell script? – Rob G Dec 07 '15 at 08:39
  • 3
    One way of converting line endings to unix style is to use ascii transfer mode in ftp connection. `ftp_put($conn, $target, $source, FTP_ASCII);` – Kadir Dec 07 '15 at 08:52
  • @Kadir just the opposite has helped me. I'm using FTP_BINARY now instead of FTP_ASCII. Works now. Thanks for pushing me in the right direction! – EDP Dec 07 '15 at 08:59
  • @EDP You are welcome. The purpose of ASCII type is to ensure that line endings are properly changed to what is right on the target platform. I suppose that in your case you have already changed line endings to unix style so there is no need to do conversion. – Kadir Dec 07 '15 at 09:08

1 Answers1

0

I'm using FTP_BINARY now instead of FTP_ASCII. Works now. – EDP

Armali
  • 14,228
  • 13
  • 47
  • 141