Welcome to the CC ++ programming community forum, and interact with 2 million technical staff to enter the security experts Michael Howard and KeithBrown. Ten tips are provided to help you free yourself from difficulties. Security issues involve many aspects. Security risks may come from anywhere. You may have written invalid error handling code, or are too generous when granting permissions. You
Welcome to the C/C ++ programming Community Forum and interact with 2 million technical staff> Security Experts Michael Howard and Keith Brown put forward ten tips to help you free yourself from difficulties. Security issues involve many aspects. Security risks may come from anywhere. You may have written invalid error handling code, or are too generous when granting permissions. You
Welcome to the C/C ++ programming Community Forum and interact with 2 million technical staff> enter
Security experts Michael Howard and Keith Brown put forward ten tips to help you solve your problem.
Security issues involve many aspects. Security risks may come from anywhere. You may have written invalid error handling code, or are too generous when granting permissions. You may have forgotten what services are running on your server. You may have accepted all user input. And so on. To give you a good start in protecting your computer, network, and code, ten tips are shown here, which give you a safer network strategy.
1. Trust users' input will put themselves in danger
Even if you do not read the remaining content, remember to "do not trust user input ". If you always assume that the data is valid and is not malicious, then the problem arises. Most of the security vulnerabilities are related to the provision of malicious data to the server by attackers.
The correctness of the trust input may cause buffer overflow, cross-site scripting attacks, SQL insert code attacks, and so on.
Let's discuss in detail these potential attack methods.
2. Prevent Buffer Overflow
When the length of the data provided by the attacker exceeds the application's expectation, a buffer overflow occurs, and the data overflows to the internal storage space. Buffer overflow is mainly a C/C ++ problem. They are threats, but they are usually easy to fix. We only saw two unnoticeable and hard-to-fix Buffer Overflow. The developer did not expect that the data provided externally would be larger than the internal buffer. Overflow causes the destruction of other data structures in the memory, which is usually exploited by attackers to run malicious code. An array index error may cause overflow and overrun in the buffer.
Not that common.
See the following C ++ code snippet:
Void DoSomething (char * cBuffSrc, DWord cbBuffSrc)
{
Char cBuffDest [32];
Memcpy (cBuffDest, cBuffSrc, cbBuffSrc );
}
Where is the problem? In fact, if cBuffSrc and cbBuffSrc come from a trusted source (for example, code that does not trust data and thus verifies the validity and size of the data), this code is free of any problems. However, if the data comes from untrusted sources and is not verified, attackers (untrusted sources) can easily make cBuffSrc larger than cBuffDest, cbBuffSrc is also set to be larger than cBuffDest. When memcpy copies data to cBuffDest, the return address from DoSomething is changed because cBuffDest is adjacent to the return address in the stack framework of the function, in this case, attackers can execute malicious operations through code.
The remedy is that you do not trust your input and do not trust any data carried in cBuffSrc and cbBuffSrc:
Void DoSomething (char * cBuffSrc, DWORD cbBuffSrc)
{
Const DWORD cbBuffDest = 32;
Char cBuffDest [cbBuffDest];
# Ifdef _ DEBUG
Memset (cBuffDest, 0x33, cbBuffSrc );
# Endif
Memcpy (cBuffDest, cBuffSrc, min (cbBuffDest, cbBuffSrc ));
}
This function shows three features of a function that can be correctly written to reduce Buffer Overflow. First, it requires the caller to provide the buffer length. Of course, you cannot blindly trust this value! Next, in a debug version, the code will detect whether the buffer is really large enough to store the source buffer. If not, an access conflict may be triggered and the code is loaded into the debugger. During debugging, you are surprised to find so many errors. Finally, the most important thing is that the call to memcpy is defensive, and it will not copy data that is more than the storage capacity of the target buffer.
In Windows Security Push at Microsoft (Microsoft Windows? Security promotion activities), we create a list of Security string processing functions for C programmers. You can find them in Strsafe. h: SaferString Handling in C (English.
3. Prevent Cross-Site Scripting
Cross-site scripting (XSS) attacks are a Web-specific issue. It can damage client data by a single hidden danger on a single Web page. Imagine the consequences of the following ASP. NET code snippet: