简体   繁体   English

Android Volley POST请求在PHP文件上的安全性

[英]Security on android volley POST Request at php file

I have an app dealing with money transactions so it's very important to have security. 我有一个处理货币交易的应用程序,因此拥有安全性非常重要。 I use passwords and some other tricks when the user sends requests to my php file, but I want to know if it is possible to somehow let the php file verify that the POST method sends Only from my app and Volley? 当用户向我的php文件发送请求时,我会使用密码和其他技巧,但是我想知道是否有可能让php文件验证POST方法仅从我的应用程序和Volley发送?

I don't want to accept requests from web pages or anything else; 我不想接受来自网页或其他任何内容的请求; only my request With Android Volley Proceeded. 只有我的要求才能进行Android Volley。

PS : Send a Values from POST method and check on PHP to identify is Not a Safe Method, and easily can hacked. PS:从POST方法发送一个值,然后在PHP上进行检查,以确认这不是安全方法,并且很容易被黑客入侵。

When you are sending your request from android, encrypt your payload using some encryption, probably RSA, and then decrypt that request on your server side, if decrypted successfully, you can be sure that the request is genuine and is not altered. 当您从android发送请求时,请使用某种加密(可能是RSA)对有效负载进行加密,然后在服务器端对该请求进行解密,如果解密成功,则可以确保该请求是真实的且未更改。

Generate a private key file in PHP 在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');

Generate a public .der-file from the private key file with OpenSSL 使用OpenSSL从私钥文件生成公共.der文件

openssl rsa -in private.pem -pubout -outform DER -out public.der

Import and use the public key in Java (Android side): 导入并使用Java(Android端)中的公钥:

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);

Encode your payload in Android (get bytes according to your requirement) 在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

Decrypt your payload in PHP: 用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 repo for demo: https://github.com/tejashwikalptaru/encrypted-communication 演示的Git回购: https : //github.com/tejashwikalptaru/encrypted-communication

Generally, no. 通常,不会。 Can't be done. 不能做 Whatever your app does, someone can do the exact same thing without using your app. 无论您的应用程序执行什么操作,某人都可以执行完全相同的操作而无需使用您的应用程序。

You can make it hard to replicate what your app does, though. 但是,您可能很难复制您的应用程序执行的操作。 Using encryption, as already suggested, accomplishes this: attacker would have to extract the key from your app and replicate your encryption logic. 正如已经建议的那样,使用加密可以完成此任务:攻击者必须从您的应用程序中提取密钥并复制您的加密逻辑。

In similar vein, you can set custom headers, you can check other headers match what your app would send, you can check details of the payload to see if they match what the app would send, and so on. 同样,您可以设置自定义标头,可以检查其他标头是否与应用程序发送的内容匹配,可以检查有效负载的详细信息以查看它们是否与应用程序发送的内容匹配,依此类推。 Any of these make it harder to construct a request that would pass as legitimate, but none would prevent it. 这些中的任何一个都使得构造一个可以合法通过的请求变得更加困难,但是没有一个阻止它。

Some attack vectors can be prevented, though: for example, if you specifically want to prevent requests from unmodified web browsers, you can check headers that can't be set from Javascript: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name 但是,可以防止某些攻击媒介:例如,如果您特别想阻止未经修改的Web浏览器发出的请求,则可以检查无法通过Javascript设置的标头: https : //developer.mozilla.org/zh-CN /文档/词汇/ Forbidden_​​header_name

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

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