Introduction to C++ to implement hacking system (preparation work)

Analyze project requirements

After hackers open this attack system, the first thing they see is a "function menu".

In order to allow hackers to choose the required functions.

Suppose the requirements are as follows:

1. Website 404 attack

2. Website tampering attack

3. Website attack records

4. DNS attack

5. Server restart attack

project realization

Create an empty project CP1

Add file admin.c

#include <iostream>
#include <Windows.h>

/*1.网站404攻击
2.网站篡改攻击
3.网站攻击记录
4.DNS攻击
5.服务器重启攻击*/

int main(void) 
{
    std::cout << "1.网站404攻击" << std::endl;
    std::cout << "2.网站篡改攻击" << std::endl;
    std::cout << "3.网站攻击记录" << std::endl;
    std::cout << "4.DNS攻击" << std::endl;
    std::cout << "5.服务器重启攻击" << std::endl;
    system("pause");return 0;
}

operation result:

Execution method 1 (used during development)

Execute in debug mode: click the button again

Execution method 2

Directly double-click to run the executable file in debug mode:

Execution method 3

Use release mode:

 Finally run directly:

Execution method 4

Run the executable compiled in release mode directly:

The difference between release mode and debug mode:

In debug mode, a lot of debugging information will be generated, which is convenient for troubleshooting, but has an impact on execution efficiency. After confirming that the program is correct, use release mode to build an executable.

The following basic grammar for beginners C++:

The original rule:

  1. Each "grammatical unit" (component) is separated by any number (at least 1) of separators. Delimiters are: space, or tab key, or newline.

         int main(void)

        int

        main(void)

        (Both of the above are legal)

        intmain(void)

        (is illegal)

       2. You must use the English input method to input (only the "string" enclosed in double quotation marks can use Chinese)

       3. Used after each code; it means the end of this instruction code.

       4. For preprocessing instructions such as #include, one instruction must occupy one line

        Other rules do not need to be memorized and will be mastered in later project practice.

Use of header files:

       There are many elements in the program (std::cout, system), all of which are actors. But they don't know each other, but they have to cooperate together and compile compulsively, which will lead to errors! You have to introduce them in advance and know their respective names and usages

solution:

Include the corresponding header file! (The header file contains the introduction of related elements)

std::cout header: iostream

system header file: Windows.h

#include <Windows.h> means to copy (copy) all the content in the file Windows.h to "here".

Header file search path

#include    <Windows.h>

<> means to find the file stdio.h from the default path of the compiler

This default path depends on the compiler. The paths of different compilers under different platforms are different. Under this default path, all the header files required by the c standard library have been included.

Use this method with the header files of the C++ Standard Library.

#include    “mytest.h”

"" means to find the file mytest.h from the current directory

If it cannot be found in the current directory, it will be searched from the default path of the compiler.

Use this method with a user-defined header file.

header file location

Requirements are placed at the top of the file.

The function of #include is to copy related declarations into this file, so it is customary to put #include at the front of the file.

If it is placed later, when related functions appear before include, there will be problems, for example

 Introduction to the main function:

 Each software is also different and contains different functions, but they all start from the main function:

 Requirements for the main function:

  1. return type
  2. parameter
  3. return value

The role of the main function:

The main function is the only entry point to the program.

That is to say, when the program is running, it starts to execute from the main function first.

A program must have a main function, and there can only be one main function.

The format of the main function:

Format 1:

int    main(void) {

}

Format 2:

The specific usage will be explained in detail in the function part later

int    main(int argc ,   char* argv) {

}

The return value of the main function
The main function should use return to return an int type data, that is, it must return an integer. General usage:
if the program ends successfully, the main function returns 0
; if there is an exception in the program, it returns an integer greater than 0.
 

Introduction to string constants:

String constant: A string whose content never changes.

String constant: enclose it in half-width (English input method) double quotation marks "".

What is the difference between constants and variables?

Constant: The value does not change in any way during the entire run of the program. Variables: Values ​​that can change at any time during the entire runtime of the program.

The output statement in c language/c++ uses:

C++-style printing:

Example:

std::cout << “hello!     world” ;

std::cout << hello!      world.” << std::endl;

Requirements:

Need to include the header file #include <iostream>

Note that it is not #include <iostream.h>

std is a namespace.

cout is an "object" within the std namespace.

endl is also an object in the std namespace, which is used to represent the "carriage return" (change to the front of the next line)

Object: first simply understand it as a specific entity.

std::cout << is equivalent to calling a special function (but not a function) to print data.

std::cout, can output any number of data continuously, separated by <<.

C-style printing:

Example:

printf("Salary: %d", 30000);

printf("Salary: %d Annual Leave: %d", 30000, 12);

printf(“%f”, 3.1415);

printf("pi: %f", 3.1415);

illustrate:

  1. Use of placeholders
  2. %d integer, %f floating point (data with fractional part)

        Requirements:

        Need to include the header file #include <stdio.h>

pay attention:

There are many usages of std::cout and printf, and now you only need to master the most common usages above, and you don’t need to pay attention to other usages now.

When to use the print statement:

        'Print' often refers to: output information to the "standard output device" (the standard output device is the "terminal" in the display).

  1. Output from console application
  2. An important means of program debugging Bug

C++ avoids name conflicts: use namespaces

        Use namespaces.

Usage 1:

#include <iostream> #include <string>

namespace China {

float population = 14.1; 

std::string capital = " Beijing " ;

}

namespace Japan {

float population = 1.27;  

std::string capital = " Tokyo " ;

}

using namespace Japan; int main(void) {

std::cout << " :" << capital << std::endl; std::cout << "人口:" << population << std::endl;

std::cout << " :" << China::capital << std::endl; std::cout << "人口:" << China::population << std::endl;

system("pause"); return 0;

}

Usage 2:

#include <iostream> #include <string>

namespace China {

float population = 14.1;   // Unit : 100 million

std::string capital = " Beijing " ;

}

namespace Japan {

float population = 1.27;   // unit : 100 million

std::string capital = " Tokyo " ;

}

// Note : no namespace

// Directly specify the identifier in the namespace instead of the entire domain name using China::capital;

using Japan::population;

int main(void) {

        std::cout << " :" << capital << std::endl;

        std::cout << "人口:" << population << std::endl;

        system("pause");

        return 0;

}

Usage 3:

#include <iostream> #include <string>

namespace China {

float population = 14.1;   //单位: 亿
std::string capital = "北京";

}

namespace Japan {

float population = 1.27;   //单位: 亿

std::string capital = "东京";

}

using namespace China; using Japan::population;

int main(void) {

std::cout << "首都:" << capital << std::endl;
std::cout << "人口:" << population << std::endl; //出错!

system("pause"); 
return 0;
}

Error message:

population": ambiguous symbol

wrong reason:

solution:

Specify the full domain name (Japan::population ) to represent.

......

int main(void) {

std::cout << "首都:" << capital << std::endl;

        std::cout << "人口:" << Japan::population << std::endl; //出错!

        system("pause");

        return 0;

}

c++的编译过程:

 程序注释:

注释的目的:

        为了让程序更方便阅读(“可读”),以便于维护。

需要注释的内容:

  1. 重要变量名的作用(用来表示什么)
  2. 比较复杂的算法思想
  3. 程序中的关键步骤

注释的方式

     有两种注释方式

  1. 单行注释  //

        在行尾,或代码的上一行

        在代码的下一行也可以,但很少有人这么做。

        2.多行注释  /*    */不支持嵌套。

        3.不要为了注释而注释!

        过多的注释,反而有害!会把真正需要注意的东西淹没。

         注释的风格:

  1. 在行尾注释 使用//注释
  2. 在代码的上一行使用//注释
  3. 多方内容的注释,使用 /*    */
  4. 函数的注释

常见错误总结

错误 1:VS 的中文乱码问题

VS 最知名的错误(被程序员吐槽最多的 BUG)。

VS 支持多种中文编码,但是使用不当时,常常导致中文乱码,而且难以解决。

中文乱码的原因:

  1. 中文在不同编码格式下, 存储的方式不一样.
  2. 如果程序是A 编码方式编译运行的,但是控制台却是以B 编码方式来显示, 就会出现乱码.
  3. vs 的控制台默认编码是 GB2312,编号号是 836

注意:GBK 编码是兼容 GB2312 的,一般描述 GBK 常常就是指 BG2312

如果源代码文件的编码如果是其他编码格式, 就会导致中文乱码.

正常场景:

在 vs 中新建文件时,该文件默认都是 GB2312 编码.

因为控制台默认也是 GB2312 编码,所以一般情况下,都不会出现中文乱码.

错误场景:

  1. 直接在项目中导入了其他已经创建好的源代码文件,

如果该文件不是 BG2312 编码, 而且含有中文的话, 就必定会出现中文乱码.

  1. 从其他文件中复制代码到 vs 的文件中, 也可能导致编码错乱.
  2. 网络编程中, 和服务器交互通信, 两端的编码很可能不同.

我们的”黑客攻击系统”的服务端木马, 就是 UTF-8 编码格式的.

解决方案一:   修改文件的编码

修改源代码文件的”编码格式”, 使其和控制台的编码格式保持一致.

代码文件的编码格式,可以通过 vs 很方便的修改: 先用 vs 打开对应的文件,  然后如下操作:

 

 

解决方案二:   强制指定文件执行

#pragma execution_character_set("gbk")

不修改文件的编码, 而是直接指定程序执行时使用的编码, 使其和运行程序的控制台的编码一致.

解决方案三:   修改控制台的编码格式

修改 vs 控制台的编码, 使其和源代码的编码保持一致. 修改注册表, 可以修改控制台的编码格式.

注意:

不建议使用该方式.

因为, 把控制台的默认编码改为其他编码后, 在该控制台输入中文, 很可能导致输入的中文

无法识别.

在 vs2010 中存在该问题.

解决方案四:   对数据进行编码转换

适用于: 服务器端和客户端, 或多个客户端之间的编码不一致时.

  1. 收到对方的其他编码数据时, 先使用特定的接口来进行编码转换.
  2. 发送本地数据给对方之前, 先使用特定的接口来进行编码转换.

错误 2:360 报告病毒

项目执行时,360 安全卫士报告病毒,程序被拦截,提示如下:

 

解决方法:

方法 1. 关闭 360

方式 2. 在 360 中添加白名单。

错误 3:代码正确,但是无法通过编译

代码看不出错误,而且报错信息莫名其妙,则很可能是因为代码使用全角字符方式输入的。   特别是各种符号,比如双引号,圆括号,分号等。

其他错误:

LINK : fatal error LNK1561: 必须定义入口点

原因:没有 main 函数,或者 main 函数的 main 写错了

下篇介绍:主要该程序的主要编写

Guess you like

Origin blog.csdn.net/m0_65635427/article/details/124896263