android volley POST 请求 php 文件的安全性
Security on android volley POST Request at php file
我有一个处理货币交易的应用程序,因此确保安全性非常重要。当用户向我的 php 文件发送请求时,我使用密码和其他一些技巧,但我想知道是否有可能以某种方式让 php 文件验证 POST 方法发送仅来自我的应用程序和 Volley?
我不想接受来自网页或其他任何东西的请求;仅我的请求 Android Volley Proceeded.
P.S : 从 POST 方法发送值并检查 PHP 以识别不是安全方法,并且很容易被黑客攻击。
当您从 android 发送请求时,使用某种加密方法(可能是 RSA)加密您的负载,然后在您的服务器端解密该请求,如果解密成功,您可以确定该请求是正版未篡改。
在PHP
中生成私钥文件
$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$keys = openssl_pkey_new($config);
$priv = openssl_pkey_get_private($keys);
openssl_pkey_export_to_file($priv, 'private.pem');
使用 OpenSSL 从私钥文件生成 public.der 文件
openssl rsa -in private.pem -pubout -outform DER -out public.der
在Java(Android端)导入并使用public键:
File pubKeyFile = new File("public.der");
DataInputStream dis = new DataInputStream(new FileInputStream(pubKeyFile));
byte[] keyBytes = new byte[(int) pubKeyFile.length()];
dis.readFully(keyBytes);
dis.close();
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKey publicKey = (RSAPublicKey)keyFactory.generatePublic(keySpec);
在 Android 中编码您的有效负载(根据您的要求获取字节)
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
String payload = "tejashwi kalp taru";
byte[] encryptedBytes = Base64.getEncoder().encode(cipher.doFinal(payload.getBytes()));
String encryptedData = new String(encryptedBytes));
//send encryptedData to server for decryption
在PHP中解密您的有效载荷:
$fp = fopen("private.pem", "r");
$privateKey = fread($fp, 8192);
fclose($fp);
$res = openssl_get_privatekey($privateKey);
$cipher = base64_decode($cipher);
openssl_private_decrypt( $cipher, $decrypted, $res, OPENSSL_PKCS1_OAEP_PADDING );
// $decrypted is the result
Git 演示回购:https://github.com/tejashwikalptaru/encrypted-communication
一般不会。做不到。无论您的应用做什么,其他人都可以在不使用您的应用的情况下做完全相同的事情。
不过,您可能很难复制您的应用程序的功能。正如已经建议的那样,使用加密可以实现这一点:攻击者必须从您的应用程序中提取密钥并复制您的加密逻辑。
类似地,您可以设置自定义 headers,您可以检查其他 headers 匹配您的应用发送的内容,您可以检查负载的详细信息以查看它们是否与应用匹配会发送,等等。这些中的任何一个都会使构建合法通过的请求变得更加困难,但 none 会阻止它。
有些攻击向量是可以阻止的,但是:例如,如果您特别想阻止来自未修改的网络浏览器的请求,您可以检查无法从 Javascript 设置的 headers: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name
我有一个处理货币交易的应用程序,因此确保安全性非常重要。当用户向我的 php 文件发送请求时,我使用密码和其他一些技巧,但我想知道是否有可能以某种方式让 php 文件验证 POST 方法发送仅来自我的应用程序和 Volley?
我不想接受来自网页或其他任何东西的请求;仅我的请求 Android Volley Proceeded.
P.S : 从 POST 方法发送值并检查 PHP 以识别不是安全方法,并且很容易被黑客攻击。
当您从 android 发送请求时,使用某种加密方法(可能是 RSA)加密您的负载,然后在您的服务器端解密该请求,如果解密成功,您可以确定该请求是正版未篡改。
在PHP
中生成私钥文件$config = array(
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$keys = openssl_pkey_new($config);
$priv = openssl_pkey_get_private($keys);
openssl_pkey_export_to_file($priv, 'private.pem');
使用 OpenSSL 从私钥文件生成 public.der 文件
openssl rsa -in private.pem -pubout -outform DER -out public.der
在Java(Android端)导入并使用public键:
File pubKeyFile = new File("public.der");
DataInputStream dis = new DataInputStream(new FileInputStream(pubKeyFile));
byte[] keyBytes = new byte[(int) pubKeyFile.length()];
dis.readFully(keyBytes);
dis.close();
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKey publicKey = (RSAPublicKey)keyFactory.generatePublic(keySpec);
在 Android 中编码您的有效负载(根据您的要求获取字节)
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
String payload = "tejashwi kalp taru";
byte[] encryptedBytes = Base64.getEncoder().encode(cipher.doFinal(payload.getBytes()));
String encryptedData = new String(encryptedBytes));
//send encryptedData to server for decryption
在PHP中解密您的有效载荷:
$fp = fopen("private.pem", "r");
$privateKey = fread($fp, 8192);
fclose($fp);
$res = openssl_get_privatekey($privateKey);
$cipher = base64_decode($cipher);
openssl_private_decrypt( $cipher, $decrypted, $res, OPENSSL_PKCS1_OAEP_PADDING );
// $decrypted is the result
Git 演示回购:https://github.com/tejashwikalptaru/encrypted-communication
一般不会。做不到。无论您的应用做什么,其他人都可以在不使用您的应用的情况下做完全相同的事情。
不过,您可能很难复制您的应用程序的功能。正如已经建议的那样,使用加密可以实现这一点:攻击者必须从您的应用程序中提取密钥并复制您的加密逻辑。
类似地,您可以设置自定义 headers,您可以检查其他 headers 匹配您的应用发送的内容,您可以检查负载的详细信息以查看它们是否与应用匹配会发送,等等。这些中的任何一个都会使构建合法通过的请求变得更加困难,但 none 会阻止它。
有些攻击向量是可以阻止的,但是:例如,如果您特别想阻止来自未修改的网络浏览器的请求,您可以检查无法从 Javascript 设置的 headers: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name