简体   繁体   English

限制仅在服务器上执行Python文件(防止从浏览器访问)

[英]Restrict execution of Python files only to server (prevent access from browser)

I have two Python files that I want to prevent anyone from executing unless it's from the server itself. 我有两个Python文件,除非文件来自服务器本身,否则我想阻止任何人执行。 The files implement a function that increases an amount of money to a user. 这些文件实现了增加用户金钱的功能。 What I need is to make this file not public to the web, so that if someone tries to call this file, the file would refuse this request, unless the call is from the server. 我需要使此文件对网络不公开,以便如果有人尝试调用此文件,则该文件将拒绝该请求,除非调用来自服务器。

Does anyone know how I can do this? 有人知道我该怎么做吗? My first idea was to check for the IP address but a lot of people can spoof their IP. 我的第一个想法是检查IP地址,但是很多人可以欺骗他们的IP。

Example

Let's say I have this file: function.py , a function in this file will accept a new amount of money and increase the appropriate balance in the database. 假设我有一个文件: function.py ,此文件中的一个函数将接受新的金额并增加数据库中的适当余额。

When someone tries to post data to this file, and this person is outside the server (lets say from 244.23.23.0 ) the file will be in-accessible. 当某人试图将数据发布到该文件中时,并且该人不在服务器外部(例如,从244.23.23.0 ),将无法访问该文件。 Whereas, calling the function from the server itself will be accepted. 而从服务器本身调用该函数将被接受。

So files can access other files on the server, but external users cannot, with the result that no one can execute this file unless it's called from the server. 因此,文件可以访问服务器上的其他文件,但外部用户无法访问,结果除非从服务器调用此文件,否则没人可以执行此文件。

This is really important to me, because it's related to real money. 这对我来说真的很重要,因为它与真钱有关。 Also, the money will come from PayPal IPN. 另外,这笔钱将来自PayPal IPN。 And actually, if there was a way to prevent access unless it was coming from PayPal, that would be an amazing way to secure the app. 实际上,如果有一种阻止访问的方法(除非它来自PayPal),那将是保护应用程序安全的好方法。

OK, as far as what I have tried: OK,就我所尝试的而言:

  1. Put the database in a cloud SQL using Google [https://developers.google.com/cloud-sql/] 使用Google [https://developers.google.com/cloud-sql/]将数据库放入云SQL中
  2. Try to check the IP of the incoming request, in the file 尝试在文件中检查传入请求的IP

Thanks for any and all help. 感谢您提供的所有帮助。

If you use Apache, you could use .htaccess to limit access to your files: 如果使用Apache,则可以使用.htaccess来限制对文件的访问:

http://httpd.apache.org/docs/current/howto/htaccess.html http://httpd.apache.org/docs/current/howto/htaccess.html

.htaccess, chmod or you could use a key defined by yourself... You have several possibilies. .htaccess,chmod或您可以使用自己定义的密钥...您有几种可能。

Edit: Anyway, if the file only contains a function. 编辑:无论如何,如果文件仅包含一个函数。 Nobody can use it from an external http request, unless you actually call it in this file: function(); 除非您实际上在以下文件中调用它,否则任何人都不能从外部http请求中使用它: function();

In regards to PayPal.. 关于贝宝..

I recommend taking a look at this example from paypal, they're part of x.com it's legit. 我建议您从paypal看一下这个示例 ,这是x.com的一部分,这是合法的。

The important part is this: 重要的部分是:

// STEP 2: Post IPN data back to paypal to validate

$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

// In wamp like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
    // error_log("Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;
}
curl_close($ch);


// STEP 3: Inspect IPN validation result and act accordingly

if (strcmp ($res, "VERIFIED") == 0) {
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment

    // assign posted variables to local variables
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];
} else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
}

What happens here is the request posted to your ipn script is sent back to paypal for validation, paypal checks the details and sends back a "valid" response, at this point you know the data is good and has been sent by paypal, you are then at a point where you can act on the data and update databases or whatever action you need to do. 这里发生的是发布到您的ipn脚本的请求被发送回Paypal进行验证,paypal检查详细信息并发送回“有效”响应,此时您知道数据是正确的并且已经由paypal发送,您然后您就可以对数据进行操作并更新数据库或您需要执行的任何操作。

you can try the following: 您可以尝试以下操作:

move it out of the public_html folder. 将其移出public_html文件夹。

if (php_sapi_name() == 'cli')
{
   //code to be executed
}
else if (php_sapi_name() != 'cli')
{
   die();
}

This way the file will only be executed from command line and that too from your sever itself because you will have to authenticate server details. 这样,将仅从命令行执行文件,而也将从服务器本身执行文件,因为您将必须验证服务器详细信息。 The file cannot be executed form web. 无法从Web执行该文件。

Another not very elegant way to do it could be defining a needed and secret parameter to be able to execute the code. 另一种不是很优雅的方法是定义一个必需的秘密参数以执行代码。

For example: 例如:

www.myweb.com/myfunction.php?pass=secretPassword

Then you could just check if the password is the one you expect with something like: 然后,您可以检查密码是否是您期望的密码,例如:

//hardcoded hashed pass with sha1, for example.
$myHashedPass = '40bd001563085fc35165329ea1ff5c5ecbdbbeef';

if(sha1($_GET['pass']) != $myHashedPass){
    die();
}

Might not be the best solution but combined with some others can be useful. 可能不是最佳解决方案,但与其他一些解决方案结合使用可能会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM