Copyright Notice:

This account published articles are from the public account, Chengxiang Moying (cxmyDev), chengxiang Moying all rights reserved.

Shall not be reproduced without permission.

One, foreword

When we develop some apps that require the security of the content displayed on the screen, if malicious programs are running in the background to take screenshots of these sensitive pages, the information may be leaked.

A common scenario is the qr code of wechat or Alipay. If a malicious program takes screenshots at this time and sends them out, it may lead to economic losses for users.

So this article on how to prevent the screenshots in some pages, for a detailed discussion.

Second, the idea of preventing screenshots

First of all, we need to make it clear that the function of preventing screenshots is generally applied to a single page in the App. It is not necessary to disable it globally, and it may not be conducive to users’ sharing and spreading. Usually, we only need to restrict sensitive pages.

These sensitive information, on the one hand, comes from the application based on user information, automatically generated, such as the previous example of payment QR code. On the other hand, active input from users, such as user input account number, password (some in order to be afraid of user input error, there is a switch to provide display password).

So we will discuss these two ideas separately.

1. Information automatically generated by the program (TWO-DIMENSIONAL code)

For auto-generated information, from the developer’s point of view, the only thing you need to do is make sure the screen capture is dirty. That is to say, the data saved by the screensaver is actually invalid or expired.

But for Android, screenshots don’t provide a broadcast or other callback to listen for it, so we have to solve it in other ways.

Now some mainstream ideas:

  1. In activity.onPause (), refresh the data.
  2. Use FileObserver to listen for changes in screenshot files in the screenshot directory.
  3. Use ContentObserver to monitor changes to all resources.

These ideas are dependent on each other to use the screenshot provided by the system itself, which may not be able to cover in detail. For example, the first method, which refreshes data in onPause(), takes advantage of the fact that the device is taking a screenshot, so that the user will be aware that the screenshot has been taken, so that the Activity will enter the onPause() life cycle. However, some software screenshots will not have this animation, so it will completely lead to monitoring and lead to information leakage.

However, this idea is not the focus of this article, the focus of this article is mainly the second way, here is just a brief introduction, later there is time to do a detailed explanation.

2. User input information

Generally speaking, even if we can modify the information that users input actively through the first way, it will be very troublesome. The most direct way is to see whether the screen capture function can be directly disabled.

In fact, Android provides an Api that is very easy to use. Just take the Window object and add a FLAG_SECURE flag to it.

The complete code is as follows:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

The window object can be obtained from activity.getwindow () or dialog.getwindow (). There are many ways to obtain the window. However, the Window object obtained by different objects will have different areas for disabling screenshots.

This approach is recommended in most cases and disables the system screenshots once and for all.

FLAG_SECURE is WindowManager LayoutParams a property, you can see from the document, it marks the current window is safe, not allowed to have some safe operation, such as the screenshot function.

Is FLAG_SECURE reliable?

FLAG_SECURE is an official recommended Flag, but we need to test it out.

Test device: Letv X600 Android 6.0

First, Android Monitor is used to take screenshots.




Later, I checked some data and found some different performance on some devices. For example, I tested on Samsung mobile phone and found that a picture file was output, but the content was pixelated, as shown in the figure below.



FLAG_SECURE
FLAG_SECURE

Since FLAG_SECURE is applied to the Window object, it is not protected by the Activity’s Window object if there are popovers or highlighted UI elements in the Activity. For example, if a Dialog box pops up in an Activity, and the Activity is set to FLAG_SECURE, if there is a Dialog popup, FLAG_SECURE needs to be set separately for the content of the popup.

Where screenshots could leak include:

  • AutoCompleteTextView
  • Spinner(in drop-down menu or dialog mode)
  • PopupWindow
  • ShareActionProvider
  • Toast

These are just some of the places where screenshots can leak.

A GIF found on the @commonsware blog below clearly shows that it is still possible to capture content in these situations, so it is important to be careful not to expose important information in these situations when there is something important to hide.

As you can see, on a page set to FLAG_SECURE, some devices still capture black images, and the points that caused the leak are still available.

@commonsware blog:

Commonsware.com/blog/2016/0…

Four,

FLAG_SECURE is a FLAG_SECURE flag that protects your screen from taking screenshots, but raises some interesting questions.