dqybjj3497 2010-11-06 20:31
浏览 74
已采纳

从PHP开始,编译,运行和返回C ++程序的输出

I am making a program that will run a persons C++ code from PHP. How can I compile, run, return the output of a C++ program securly? By securly I mean how can I do it without making my system vulnerable to hackers? I guess I would just use the exec comman to GCC to compile the program then run the program using exec. But how can I do this securly?

  • 写回答

4条回答 默认 最新

  • dougu5847 2010-11-06 20:42
    关注

    This is something just a little bit more advanced that something you would be able to do with a non-custom server. Most likely. Because most server providers won't let you execute processes, and even if, they SURELY won't let you install GCC there and run potentially unsafe code on their machine (executing processes, remember?)...

    I thought of doing something like this (online development tools with multi-language compilers, sandboxes, SVC...) someday, but just because I have a lot of space as well as a custom server right next to my normal computer.

    IF you've got a custom server (supposing it's Linux, and most likely LAMP):

    • have gcc or even better g++ installed
    • have a Virtual Machine (such as Bochs) installed, along with a basic install of any Linux (other platform) - this is just for C and D steps
    • put the files on some temporary place
    • use proc_open to start the g++ with the files listed, but make sure you run this as a dedicated user - for example gccuser - a user with NO permissions but to read from the directory where the files are stored
    • A) read out the output (success or the errors / warnings) from g++
    • B) let the user download the result file
    • C) put the result file through a linker, and then write that file on the VM's HDD - THEN run it, and you can show the output to user
    • D) (most advanced) - make a GUI kinda thing (terminal) in JavaScript / AS to talk to the running file in real-time - add a time limit... this however also means you need more than one Bochs running at once (more users) - that means you need to copy a Bochs to a temp directory, together with an empty HDD (with only OS), run it on THAT one, when finished - delete

    proc_open - http://php.net/manual/en/function.proc-open.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止