Brush 28 interview questions: how to write secure java code?

Protective masks

Attacks on jdk, jvm, java applications varied? Well, from the perspective of java programmer, how to write secure code?

I was Li Fuchun, I prepare for the interview, today's topic is: how to write secure java code?

A: This function needs from design to implementation details into account, the so-called unsafe, is exploited by attackers jdk, jvm, blemishes java application, or architecture design flaws attack.

To dos attack as an example, to analyze how to write secure java code.

dos is when the attacker sends a request using a large number of machines, the target java application bandwidth or other resources (cpu, memory, disk) is depleted, resulting in normal users can not normally access. Attacks example as follows:

1, the early versions of jdk, you can use to set thread priority, thread attacker to make a lot of consumption of resources to do something, this can be solved by upgrading jdk version.

2, some sites cpu-intensive user interface does not limit the use of these interfaces attacker abuse depletion cpu resources; Solution: Restrict users cpu-intensive interface frequency or time.

3, file upload no restrictions, the attacker will run out of memory and external memory system; Solution: Restrict users to upload a file number and frequency of

4, the consumption of other resources, such as file descriptors, database connections, re-entry lock is not released in time, the solution is: show free up resources, try-finally;

Security Code

Specify the canonical security code during the development phase, java specification can refer to the manual ali. Here are a few examples to illustrate what appears scene security issues.

Numerical prevent overflow type

// a, b, c都是int类型的数值
if (a + b < c) {
// …
}

Improve

if (a < c – b)

Avoid exposure of sensitive information

try {
// 业务代码
} catch (Exception e) {
throw new RuntimeException(hostname + port + “ doesn’t response”);

Improvement: sensitive information is not output to the exception;

Serialization

Serialization general appearance scene information exchange between systems, mainly to avoid leakage of sensitive data.

1, when sending out, to increase the field sensitive field transiant protected against being serialized;

2, when receiving information, in the same readObject implemented method of Chinese medicine and data security check;

After the development is completed, you can use the associated plug-in code scanning, such as findbugs, the first time to detect possible security holes, fix the code segment unsafe.

Specifications and tools can also be combined team of security sector put code quality scanning systems, such as sonar;

After deploying java program, to be used for jdk version, the version of jdk timely updates, version upgrades automatically fixes many loopholes for the java;

summary

Benpian answered the main reason for the attack generated java, then dos attack power, citing the case of several attacks.

Combined with software development life cycle, it gives some security tools and advice to write java code java application security assurance at different stages.

Summary Brain Mapping

The original is not easy, thumbs concern support about it! Please indicate the source, let us complementarity and common progress, welcomed the communication.
I will continue to share Java programming knowledge and software programmers to develop career path, welcome attention, I put together a variety of resources all these years to learn programming, public concern number 'Li Fuchun continuous output' Send 'learning materials' share to you!

Guess you like

Origin www.cnblogs.com/snidget/p/12627368.html