//performs fixed xor on two Hex strings of equal length string fixedXor(string &a,string &b) { string foo = ""; string bar = ""; for(int i = 0; i < a.size(); i++) { foo += hextobin(a[i]); } for(int i = 0; i < b.size(); i++) { bar += hextobin(b[i]); } string output = X_OR_byte(foo,bar); int i = 0; string result = ""; while(i < output.size()) { string temp = ""; temp = output[i]; temp += output[i+1]; temp += output[i+2]; temp += output[i+3]; result += bintohex(temp); i = i + 4; } return result; }
//prints all possible XOR results int sByteXor(string &input) { ofstream output("plaintexts.txt"); string temp = ""; //convert input argument from hex to it's binary equivalent, store in string temp for(int i = 0; i < input.size(); i++) { temp += hextobin(input[i]); } //cout << "temp = " << temp << "\n"; //perform all possible xor operations, store result in xor_results vector <string> xor_results = X_OR(temp); //this for loop converts each xor result from binary to ascii and stores it in ascii_results for(int i = 0; i < xor_results.size(); i++) { string bar = ""; for(int j = 0; j < xor_results[i].size(); j = j + 8) { string foo = ""; foo += xor_results[i][j]; foo += xor_results[i][j+1]; foo += xor_results[i][j+2]; foo += xor_results[i][j+3]; foo += xor_results[i][j+4]; foo += xor_results[i][j+5]; foo += xor_results[i][j+6]; foo += xor_results[i][j+7]; bar += bintoc(foo); } output << bar << "\n"; } output.close(); return 0; }
int keyset_parse_key(const char* text, unsigned int textlen, unsigned char* key, unsigned int size, int* valid) { unsigned int i, j; unsigned int hexcount = 0; if (valid) *valid = 0; for(i=0; i<textlen; i++) { if (ishex(text[i])) hexcount++; } if (hexcount != size*2) { fprintf(stdout, "Error, expected %d hex characters when parsing text \"", size*2); for(i=0; i<textlen; i++) fprintf(stdout, "%c", text[i]); fprintf(stdout, "\"\n"); return KEY_ERR_LEN_MISMATCH; } for(i=0, j=0; i<textlen; i++) { if (ishex(text[i])) { if ( (j&1) == 0 ) key[j/2] = hextobin(text[i])<<4; else key[j/2] |= hextobin(text[i]); j++; } } if (valid) *valid = 1; return KEY_OK; }
/* converts a hex string `src' of `size' characters to binary and copies the the result into `dst' */ static int parse_hex(uint8_t *dst, uint32_t size, const char *src) { int l, h; check(dst); check(src); check(2 * size == strlen(src)); while (size) { h = hextobin(tolower(*src++)); l = hextobin(tolower(*src++)); check(l >= 0); check(h >= 0); *dst++ = (h << 4) | l; --size; } return 0; }
int break_Rkey_Xor() { string inputFile; cout << "Enter the name of an existing text file in the current directory: "; cin >> inputFile; //open input file for reading ifstream input(inputFile); string bin_text = ""; //this while loop converts the input to binary form while(input.good()) { char c = input.get(); //if input is still valid if(input.good()) { bin_text += hextobin(c); } } input.close(); int pos[3]; find_shortest_distances(bin_text,pos); vector <string> keyOneBlocks = breakIntoBlocks(pos[0],bin_text); vector <string> keyTwoBlocks = breakIntoBlocks(pos[1],bin_text); vector <string> keyThreeBlocks = breakIntoBlocks(pos[2],bin_text); vector <string> keyOneTranspose = transpose(keyOneBlocks); vector <string> keyTwoTranspose = transpose(keyTwoBlocks); vector <string> keyThreeTranspose = transpose(keyThreeBlocks); ofstream output("keyOneoutput.txt"); for(int i = 0; i < keyOneTranspose.size(); i++) { sByteXor(keyOneTranspose[i],output); } output.close(); ofstream output2("keyTwooutput.txt"); for(int i = 0; i < keyTwoTranspose.size(); i++) { sByteXor(keyTwoTranspose[i],output2); } output2.close(); ofstream output3("keyThreeoutput.txt"); for(int i = 0; i < keyThreeTranspose.size(); i++) { sByteXor(keyThreeTranspose[i],output3); } output3.close(); return 0; }
static int fips_ic_set(char *str) { int i; if (strlen(str) != 2 * SHA1_DIGEST_SIZE) { printk(KERN_ERR "FIPS: invalid integrity check HMAC parameter %s" " (must be %d characters long)\n", str, 2 * SHA1_DIGEST_SIZE); memset(fips_ic, 0, SHA1_DIGEST_SIZE); } else { hextobin(str, fips_ic, SHA1_DIGEST_SIZE); printk(KERN_INFO "FIPS: FIPS expected integrity check HMAC = "); for (i = 0; i < SHA1_DIGEST_SIZE; i++) { printk(KERN_CONT "%02x", fips_ic[i]); } } return 1; }
int detectXor() { string inputfile; //get name of input file with hex encoded single byte XOR encrypted ciphertexts. cout << "Enter the name of an existing text file in the current directory: "; cin >> inputfile; ifstream input(inputfile); //open output file ofstream output("plaintexts.txt"); string bar = ""; while(input.good()) { string temp = ""; string baz = ""; //get line (ciphertext) from input, store in baz getline(input,baz); //compute all possible plaintexts, write store them in ascii_results vector if(input.good()) { //convert hex to binary, store in temp for(int i = 0; i < baz.size(); i++) { temp += hextobin(baz[i]); } //cout << "temp = " << temp << "\n"; //perform xor, all possible xor results are stored in the vector vector <string> xor_results = X_OR(temp); vector <string> ascii_results; //convert every xor result from binary to the corresponding ascii characters for(int i = 0; i < xor_results.size(); i++) { string bar = ""; for(int j = 0; j < xor_results[i].size(); j = j + 8) { string foo = ""; foo += xor_results[i][j]; foo += xor_results[i][j+1]; foo += xor_results[i][j+2]; foo += xor_results[i][j+3]; foo += xor_results[i][j+4]; foo += xor_results[i][j+5]; foo += xor_results[i][j+6]; foo += xor_results[i][j+7]; bar += bintoc(foo); } //store ascii characters in ascii_results vector ascii_results.push_back(bar); } //print all ascii_results for(int i = 0; i < ascii_results.size(); i++) { bar = ascii_results[i] + "\n"; output << bar; } } } //close input and output files input.close(); output.close(); return 0; }
int main(int argc, char *argv[]) { char macAddress[18]; char ifName[64]; const int on = 1; short int cont; int skt_target; unsigned char ma[6]; unsigned char packet[102]; char buff[1024]; struct sockaddr_in sindst; struct ifreq *ifr = NULL; struct ifconf ifc; int i, if_found = 0; int iRet = 0; if (argc != 3) { print_usage(argv[0]); exit(-1); } snprintf(ifName, 64, argv[1]); snprintf(macAddress, 18, argv[2]); // Test length of argument if (strlen(macAddress) != 17) { fprintf(stderr, "Mac address incorrect: use 12:34:56:78:90:ab or any other separator\n"); exit(-2); } // Test and convert hex string to binary ma[0]=0; ma[1]=0; ma[2]=0; ma[3]=0; ma[4]=0; ma[5]=0; for (cont=0;cont<18;cont++) { if (cont%3 != 2) { if ((macAddress[cont] < '0' || macAddress[cont] > '9') && (macAddress[cont] < 'A' || macAddress[cont] > 'F') && (macAddress[cont] < 'a' || macAddress[cont] > 'f')) { fprintf(stderr, "Mac address incorrect: use 12:34:56:78:90:ab or any other separator\n"); exit(-3); } else { ma[cont/3]=ma[cont/3]*16+hextobin(macAddress[cont]); } } } // Create broadcast magic packet // 6*FF + 16*MAC_ADDRESS for(cont=0;cont<=5;cont++) packet[cont]='\xFF'; for(cont=0;cont<=15;cont++) { packet[6+cont*6] = ma[0]; packet[7+cont*6] = ma[1]; packet[8+cont*6] = ma[2]; packet[9+cont*6] = ma[3]; packet[10+cont*6] = ma[4]; packet[11+cont*6] = ma[5]; } // Open Socket skt_target = socket( AF_INET, SOCK_DGRAM, 0); if ( skt_target < 0 ) { fprintf( stderr, "Cannot open SOCKET.\n"); exit(-4); } // Prepare dest address memset (&sindst,0,sizeof (sindst)); sindst.sin_family = AF_INET; sindst.sin_port = htons (PORT); // Retrieve interfaces informations ifc.ifc_len = sizeof(buff); ifc.ifc_buf = buff; if (ioctl(skt_target, SIOCGIFCONF, &ifc) < 0) { perror("ioctl SIOCGIFCONF"); return(-5); } ifr = ifc.ifc_req; // Retrieve the interface broadcast address for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) { if (strncmp(ifName, ifr->ifr_name, 3) == 0) { if_found = 1; if (ioctl(skt_target, SIOCGIFBRDADDR, ifr) < 0) { perror("ioctl SIOCGIFBRDADDR"); return(-6); } // printf(" Broadcast: %s\n", inet_ntoa(((struct sockaddr_in *) // &(ifr->ifr_broadaddr))->sin_addr)); sindst.sin_addr = ((struct sockaddr_in *) &(ifr->ifr_broadaddr))->sin_addr; break; } } if (if_found == 0) { return(-7); } // Set up Broadcast iRet = setsockopt (skt_target, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); if (iRet != 0) { return(-8); } // Send UDP packet iRet = (int) sendto( skt_target, packet, 102, 0, (struct sockaddr*)&sindst, sizeof(sindst)); if (iRet != 102) { return(-9); } // Close socket and exit close (skt_target); printf("Packet sent successfully\n"); return 0; }
/* * smb_pwd_fgetent * * Parse the buffer in the passed pwbuf and fill in the * smb password structure to point to the parsed information. * The entry format is: * * <user-name>:<user-id>:<LM hash>:<NTLM hash> * * Returns a pointer to the passed pwbuf structure on success, * otherwise returns NULL. */ static smb_pwbuf_t * smb_pwd_fgetent(FILE *fp, smb_pwbuf_t *pwbuf, uint32_t flags) { char *argv[SMB_PWD_NARG]; char *pwentry; smb_passwd_t *pw; smb_pwdarg_t i; int lm_len, nt_len; pwentry = pwbuf->pw_buf; if (fgets(pwentry, SMB_PWD_BUFSIZE, fp) == NULL) return (NULL); (void) trim_whitespace(pwentry); for (i = 0; i < SMB_PWD_NARG; ++i) { if ((argv[i] = strsep((char **)&pwentry, ":")) == NULL) return (NULL); } if ((*argv[SMB_PWD_NAME] == '\0') || (*argv[SMB_PWD_UID] == '\0')) return (NULL); pw = pwbuf->pw_pwd; bzero(pw, sizeof (smb_passwd_t)); pw->pw_uid = strtoul(argv[SMB_PWD_UID], 0, 10); (void) strlcpy(pw->pw_name, argv[SMB_PWD_NAME], sizeof (pw->pw_name)); if (strcmp(argv[SMB_PWD_LMHASH], SMB_PWD_DISABLE) == 0) { pw->pw_flags |= SMB_PWF_DISABLE; if (flags != SMB_PWD_GETF_NOPWD) { (void) strcpy((char *)pw->pw_lmhash, SMB_PWD_DISABLE); (void) strcpy((char *)pw->pw_nthash, SMB_PWD_DISABLE); } return (pwbuf); } if (flags == SMB_PWD_GETF_NOPWD) return (pwbuf); lm_len = strlen(argv[SMB_PWD_LMHASH]); if (lm_len == SMBAUTH_HEXHASH_SZ) { (void) hextobin(argv[SMB_PWD_LMHASH], SMBAUTH_HEXHASH_SZ, (char *)pw->pw_lmhash, SMBAUTH_HASH_SZ); pw->pw_flags |= SMB_PWF_LM; } else if (lm_len != 0) { return (NULL); } nt_len = strlen(argv[SMB_PWD_NTHASH]); if (nt_len == SMBAUTH_HEXHASH_SZ) { (void) hextobin(argv[SMB_PWD_NTHASH], SMBAUTH_HEXHASH_SZ, (char *)pw->pw_nthash, SMBAUTH_HASH_SZ); pw->pw_flags |= SMB_PWF_NT; } else if (nt_len != 0) { return (NULL); } return (pwbuf); }
void decode() { flag3=0; hextobin(instruction[0]); strcpy(cond,arr); hextobin(instruction[1]); F[0]=arr[0]; F[1]=arr[1]; I=arr[2]; opcode[0]=arr[3]; b_opcode[0]=arr[2]; b_opcode[1]=arr[3]; hextobin(instruction[2]); opcode[1]=arr[0]; opcode[2]=arr[1]; opcode[3]=arr[2]; b_offset[0]=arr[0]; b_offset[1]=arr[1]; b_offset[2]=arr[2]; S=arr[3]; b_offset[3]=arr[3]; hextobin(instruction[3]); strcpy(Rn,arr); b_offset[4]=arr[0]; b_offset[5]=arr[1]; b_offset[6]=arr[2]; b_offset[7]=arr[3]; hextobin(instruction[4]); strcpy(Rd,arr); b_offset[8]=arr[0]; b_offset[9]=arr[1]; b_offset[10]=arr[2]; b_offset[11]=arr[3]; hextobin(instruction[5]); offset[0]=arr[0]; offset[1]=arr[1]; offset[2]=arr[2]; offset[3]=arr[3]; b_offset[12]=arr[0]; b_offset[13]=arr[1]; b_offset[14]=arr[2]; b_offset[15]=arr[3]; hextobin(instruction[6]); Imm[0]=arr[0]; Imm[1]=arr[1]; Imm[2]=arr[2]; Imm[3]=arr[3]; offset[4]=arr[0]; offset[5]=arr[1]; offset[6]=arr[2]; offset[7]=arr[3]; b_offset[16]=arr[0]; b_offset[17]=arr[1]; b_offset[18]=arr[2]; b_offset[19]=arr[3]; hextobin(instruction[7]); strcpy(Rm,arr); offset[8]=arr[0]; offset[9]=arr[1]; offset[10]=arr[2]; offset[11]=arr[3]; b_offset[20]=arr[0]; b_offset[21]=arr[1]; b_offset[22]=arr[2]; b_offset[23]=arr[3]; Imm[4]=arr[0]; Imm[5]=arr[1]; Imm[6]=arr[2]; Imm[7]=arr[3]; sum=0; bintodec(Rn); n=sum; sum=0; bintodec(Rm); mr=sum; sum=0; bintodec(Imm); mi=sum; sum=0; bintodec(Rd); d=sum; sum=0; bintodec(offset); off=sum; if(instruction[2]=='F') { flag3=1; if(instruction[7]=='E') b_off=-1; if(instruction[7]=='D') b_off=-2; if(instruction[7]=='C') b_off=-3; if(instruction[7]=='B') b_off=-4; if(instruction[7]=='A') b_off=-5; if(instruction[7]=='9') b_off=-6; if(instruction[7]=='8') b_off=-7; if(instruction[7]=='7') b_off=-8; if(instruction[7]=='6') b_off=-9; if(instruction[7]=='5') b_off=-10; if(instruction[7]=='4') b_off=-11; if(instruction[7]=='3') b_off=-12; if(instruction[7]=='2') b_off=-13; if(instruction[7]=='1') b_off=-14; if(instruction[7]=='0') b_off=-15; } else {sum=0; bintodec(b_offset); b_off=sum; } //printf("\n f= %s b_opcode= %s, b_off = %d \n",F,b_opcode,b_off); if((strcmp(F,"00")==0)&&(strcmp(opcode,"1101")==0) && I=='1') { printf(" Operation is MOV , immediate value is %d , Destination register is R%d \n",mi,d); control=1; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"1101")==0) && I=='0') { printf(" Operation is MOV , register is R%d , Destination register is R%d \n",mr,d); control=0; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='0')) { printf(" Operation is ADD , first operand is R%d ,Second operand is R%d , Destination register is R%d \n",n,mr,d); control=2; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='1')) { //printreg();/////////////////////////// //u++; //////////////////////// printf(" Operation is ADD , first operand is R%d ,immediate value is %d , Destination register is R%d \n",n,mi,d); control=5; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0010")==0 )&&(I=='0')) { printf(" Operation is SUB , first operand is R%d ,Second operand is R%d , Destination register is R%d \n",n,mr,d); control=6; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0010")==0 )&&(I=='1')) { printf(" Operation is SUB , first operand is R%d ,immediate value is %d , Destination register is R%d \n",n,mi,d); control=7; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"1010")==0 )&&(I=='0')) { printf(" Operation is CMP , first operand is R%d ,Second operand is R%d \n",n,mr); printf("\n R[4]== %d\n",R[4]); control=9; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"1010")==0 )&&(I=='1')) { printf(" Operation is CMP , first operand is R%d ,immediate value is %d \n",n,mi); printf("\n R[4]== %d\n",R[4]); control=11; if(z==0) {w=mi;z=1;} } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='0')) { printf(" Operation is AND , first operand is R%d ,Second operand is R%d , Destination register is R%d \n",n,mr,d); control=20; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='1')) { printf(" Operation is AND , first operand is R%d ,immediate value is %d , Destination register is R%d \n",n,mi,d); control=21; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='0')) { printf(" Operation is OR , first operand is R%d ,Second operand is R%d , Destination register is R%d \n",n,mr,d); control=22; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='1')) { printf(" Operation is OR , first operand is R%d ,immediate value is %d , Destination register is R%d \n",n,mi,d); control=23; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='0')) { printf(" Operation is EOR , first operand is R%d ,Second operand is R%d , Destination register is R%d \n",n,mr,d); control=24; } else if((strcmp(F,"00")==0)&&(strcmp(opcode,"0100")==0 )&&(I=='1')) { printf(" Operation is EOR , first operand is R%d ,immediate value is %d , Destination register is R%d \n",n,mi,d); control=25; } if((strcmp(F,"01")==0)&&(I=='0')&&(strcmp(opcode,"1100")==0)) { printf(" Operation is LDR , base address is %d , offset is %d , Destination register is R%d \n",n,off,d); control=3; } /*else if((strcmp(F,"01")==0)&&(I=='0')&&(strcmp(opcode,"1100")==0)) //$$$$$$$$ for register offsets { printf(" Operation is LDR , base address is %d , offset is %d , Destination register is R%d \n",n,off,d); control=26; } */ else if((strcmp(F,"01")==0)&&(I=='0')&&(strcmp(opcode,"1100")==0)) { printf(" Operation is STR , base address is %d , offset is %d , register whose value is to be stored is R%d \n",n,off,d); control=4; } else if((strcmp(F,"01")==0)&&(I=='1')&&(strcmp(opcode,"1100")==0)) //$$$$$$$$$$ for register offsets { printf(" Operation is STR , base address is stored in R%d , offset register is R%d , register whose value is to be stored is R%d \n",n,mr,d); control=27; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"0000")==0)) { printf(" Operation is Branch EQ , branch offset is %d\n",b_off); control=8; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"0001")==0)) { printf(" Operation is Branch NE , branch offset is %d\n",b_off); control=12; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"1011")==0)) { printf(" Operation is Branch LT , branch offset is %d\n",b_off); control=13; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"1101")==0)) { printf(" Operation is Branch LE , branch offset is %d\n",b_off); control=14; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"1100")==0)) { printf(" Operation is Branch GT , branch offset is %d\n",b_off); control=15; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"1010")==0)) { printf(" Operation is Branch GE , branch offset is %d\n",b_off); control=16; } else if((strcmp(F,"10")==0)&&(strcmp(b_opcode,"10")==0)&&(strcmp(cond,"1110")==0)) { //return; ///////////////////////////////////////////////////////////////////////// printf(" Operation is Branch ALL , branch offset is %d\n",b_off); control=17; } else if(strcmp(opcode,"1000")==0) { printf(" EXIT \n"); control=10; } /*else if(instruction[4]=='5') R[5]= */ }
/* * Print "standard" escape characters */ static int print_escape(const char *str) { const char *start = str; int value; int c; str++; switch (*str) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': for (c = 3, value = 0; c-- && isodigit(*str); str++) { value <<= 3; value += octtobin(*str); } putchar(value); return str - start - 1; /* NOTREACHED */ case 'x': str++; for (value = 0; isxdigit(*str); str++) { value <<= 4; value += hextobin(*str); } if (value > UCHAR_MAX) { warnx ("escape sequence out of range for character"); rval = 1; } putchar (value); return str - start - 1; /* NOTREACHED */ case '\\': /* backslash */ putchar('\\'); break; case '\'': /* single quote */ putchar('\''); break; case '"': /* double quote */ putchar('"'); break; case 'a': /* alert */ putchar('\a'); break; case 'b': /* backspace */ putchar('\b'); break; case 'e': /* escape */ #ifdef __GNUC__ putchar('\e'); #else putchar(033); #endif break; case 'f': /* form-feed */ putchar('\f'); break; case 'n': /* newline */ putchar('\n'); break; case 'r': /* carriage-return */ putchar('\r'); break; case 't': /* tab */ putchar('\t'); break; case 'v': /* vertical-tab */ putchar('\v'); break; default: putchar(*str); warnx("unknown escape sequence `\\%c'", *str); rval = 1; } return 1; }