[英]Restrict execution of Python files only to server (prevent access from browser)
我有兩個Python文件,除非文件來自服務器本身,否則我想阻止任何人執行。 這些文件實現了增加用戶金錢的功能。 我需要使此文件對網絡不公開,以便如果有人嘗試調用此文件,則該文件將拒絕該請求,除非調用來自服務器。
有人知道我該怎么做嗎? 我的第一個想法是檢查IP地址,但是很多人可以欺騙他們的IP。
假設我有一個文件: function.py
,此文件中的一個函數將接受新的金額並增加數據庫中的適當余額。
當某人試圖將數據發布到該文件中時,並且該人不在服務器外部(例如,從244.23.23.0
),將無法訪問該文件。 而從服務器本身調用該函數將被接受。
因此,文件可以訪問服務器上的其他文件,但外部用戶無法訪問,結果除非從服務器調用此文件,否則沒人可以執行此文件。
這對我來說真的很重要,因為它與真錢有關。 另外,這筆錢將來自PayPal IPN。 實際上,如果有一種阻止訪問的方法(除非它來自PayPal),那將是保護應用程序安全的好方法。
OK,就我所嘗試的而言:
感謝您提供的所有幫助。
如果使用Apache,則可以使用.htaccess來限制對文件的訪問:
.htaccess,chmod或您可以使用自己定義的密鑰...您有幾種可能。
編輯:無論如何,如果文件僅包含一個函數。 除非您實際上在以下文件中調用它,否則任何人都不能從外部http請求中使用它: function();
關於貝寶..
我建議您從paypal看一下這個示例 ,這是x.com的一部分,這是合法的。
重要的部分是:
// 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
}
這里發生的是發布到您的ipn腳本的請求被發送回Paypal進行驗證,paypal檢查詳細信息並發送回“有效”響應,此時您知道數據是正確的並且已經由paypal發送,您然后您就可以對數據進行操作並更新數據庫或您需要執行的任何操作。
您可以嘗試以下操作:
將其移出public_html
文件夾。
if (php_sapi_name() == 'cli')
{
//code to be executed
}
else if (php_sapi_name() != 'cli')
{
die();
}
這樣,將僅從命令行執行文件,而也將從服務器本身執行文件,因為您將必須驗證服務器詳細信息。 無法從Web執行該文件。
另一種不是很優雅的方法是定義一個必需的秘密參數以執行代碼。
例如:
www.myweb.com/myfunction.php?pass=secretPassword
然后,您可以檢查密碼是否是您期望的密碼,例如:
//hardcoded hashed pass with sha1, for example.
$myHashedPass = '40bd001563085fc35165329ea1ff5c5ecbdbbeef';
if(sha1($_GET['pass']) != $myHashedPass){
die();
}
可能不是最佳解決方案,但與其他一些解決方案結合使用可能會很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.