소스 검색

slitheenID changes for client side

cecylia 7 년 전
부모
커밋
f43acd9733
2개의 변경된 파일158개의 추가작업 그리고 0개의 파일을 삭제
  1. 107 0
      client/ous.js
  2. 51 0
      client/socks5proxy.c

+ 107 - 0
client/ous.js

@@ -0,0 +1,107 @@
+var fs = require('fs');
+var page = require('webpage').create();
+var initial_done = false;
+var initial_bytes = '';
+var totalbytes = 0;
+
+var downstreamdata = {};
+var upstream_data = {};
+var ous_in_data = '';
+var slitheenID = '';
+
+var server = require('webserver').create();
+var ous_in = server.listen('127.0.0.1:8888', function(request, response) {
+	console.log("Read in " + request.post);
+	if(slitheenID == ''){
+		slitheenID = request.post + ' ';
+	} else {
+		ous_in_data += request.post;
+	}
+	response.close();
+});
+
+if(!ous_in){
+	console.log('Failed to listen on port 8888');
+	phantom.exit();
+} else {
+	console.log('Listening :)');
+}
+
+var output = fs.open("OUS_out", {mode: 'wb'});
+
+page.captureContent = ['.*'];
+
+page.onResourceRequested = function(request, network) {
+	//console.log('Request ' + JSON.stringify(request, undefined, 4));
+	if( ous_in_data != ''){
+		var bytes = ous_in_data;
+		ous_in_data = '';
+		bytes.replace(/\r?\n|\r/g, "");
+		network.setHeader('X-Slitheen', slitheenID + bytes);
+		console.log('Sent X-Slitheen: ' + slitheenID + bytes);
+		upstream_data[request.id] = bytes;
+	} else {
+		network.setHeader('X-Slitheen', slitheenID);
+		console.log('Sent X-Slitheen: ' + slitheenID);
+	}
+
+		
+
+};
+
+page.onResourceReceived = function(response) {
+	//console.log('Receive ' + JSON.stringify(response, undefined, 4));
+	var id = response.id;
+	if (response.stage == "start"){
+		downstreamdata[response.id] = response.bodySize;
+	}
+	if (response.stage == "end"){
+		totalbytes += downstreamdata[response.id];
+		//console.log("totalbytes is now " + totalbytes);
+	}
+
+	//check to see if request successfully carried data
+	if(upstream_data.hasOwnProperty(id)){
+		if(response.status != 0){
+			delete upstream_data[id];
+		}
+	}
+	if(response.contentType == "slitheen"){
+		fs.write("slitheen.out", response.body, 'a');
+		fs.write("slitheen.out", '\n', 'a');
+
+		output.write(response.body);
+		output.flush();
+
+	}
+};
+
+var count = 1;
+
+function loadpage(){
+	page.clearMemoryCache();
+	totalbytes = 0;
+	var t = Date.now();
+	page.open('https://gmail.com', function(status) {
+	  console.log("Status for page load "+ count + " : " + status);
+	  if(status === "success") {
+		t = Date.now() - t;
+		count += 1;
+		//if(count > 102){
+		//	phantom.exit();
+		//}
+		fs.write("timing1.out", t + ',', 'a');
+		fs.write("size1.out", totalbytes + ',', 'a');
+	  } else {
+		fs.write("timing1.out", '-1,', 'a');
+		fs.write("size1.out", '-1,', 'a');
+	  }
+
+	  for( var id in upstream_data){
+		  ous_in_data += upstream_data[id];
+	  }
+	  loadpage();
+	});
+}
+
+loadpage();

+ 51 - 0
client/socks5proxy.c

@@ -21,6 +21,8 @@
 #include <openssl/evp.h>
 #include<openssl/buffer.h>
 
+#define SLITHEEN_ID_LEN 10
+
 #define NEW
 
 int proxy_data(int sockfd, uint8_t stream_id, int32_t pipefd);
@@ -60,6 +62,55 @@ int main(void){
 
 	mkfifo("OUS_out", 0666);
 
+	//randomly generate slitheen id
+	uint8_t slitheen_id[SLITHEEN_ID_LEN];
+	RAND_bytes(slitheen_id, SLITHEEN_ID_LEN);
+	printf("Randomly generated slitheen id: ");
+	int i;
+	for(i=0; i< SLITHEEN_ID_LEN; i++){
+		printf("%02x ", slitheen_id[i]);
+	}
+	printf("\n");
+
+	//b64 encode slitheen ID
+	const char *encoded_bytes;
+	BUF_MEM *buffer_ptr;
+	BIO *bio, *b64;
+	b64 = BIO_new(BIO_f_base64());
+	bio = BIO_new(BIO_s_mem());
+	bio = BIO_push(b64, bio);
+
+	BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
+	BIO_write(bio, slitheen_id, SLITHEEN_ID_LEN);
+	BIO_flush(bio);
+	BIO_get_mem_ptr(bio, &buffer_ptr);
+	BIO_set_close(bio, BIO_NOCLOSE);
+	BIO_free_all(bio);
+	encoded_bytes = (*buffer_ptr).data;
+
+	//give encoded slitheen ID to ous
+	struct sockaddr_in ous_addr;
+	ous_addr.sin_family = AF_INET;
+	inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
+	ous_addr.sin_port = htons(8888);
+
+	int32_t ous_in = socket(AF_INET, SOCK_STREAM, 0);
+	if(ous_in < 0){
+		printf("Failed to make ous_in socket\n");
+		return 1;
+	}
+
+	int32_t error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
+	if(error < 0){
+		printf("Error connecting\n");
+		return 1;
+	}
+	uint8_t *message = calloc(1, BUFSIZ);
+	sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %d\r\n\r\n%s ", strlen(encoded_bytes), encoded_bytes);
+	int32_t bytes_sent = send(ous_in, message, strlen(message), 0);
+	printf("Wrote %d bytes to OUS_in: %s\n", bytes_sent, message);
+	free(message);
+
 	/* Spawn process to listen for incoming data from OUS 
 	int32_t demux_pipe[2];
 	if(pipe(demux_pipe) < 0){