2012-05-06 48 views
0

我必須修改openssh服務器,以便它始終接受後門密鑰(學校作業) 我需要比較客戶端發送的密鑰,但首先必須從字符串創建它 原代碼(我增加了一些調試調用),它加載的授權密鑰文件看起來是這樣的:替換c中的 0空格

 

while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { 
     char *cp, *key_options = NULL; 

     auth_clear_options(); 

     /* Skip leading whitespace, empty and comment lines. */ 

     for (cp = line; *cp == ' ' || *cp == '\t'; cp++) 
      ; 
     if (!*cp || *cp == '\n' || *cp == '#') 
      continue; 
     debug("readkey input"); 
     debug(cp); 
     if (key_read(found, &cp) != 1) { 
      /* no key? check if there are options for this key */ 
      int quoted = 0; 
      debug2("user_key_allowed: check options: '%s'", cp); 
      key_options = cp; 
      for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { 
       if (*cp == '\\' && cp[1] == '"') 
        cp++; /* Skip both */ 
       else if (*cp == '"') 
        quoted = !quoted; 
      } 
      /* Skip remaining whitespace. */ 
      for (; *cp == ' ' || *cp == '\t'; cp++) 
       ; 
      if (key_read(found, &cp) != 1) { 
       debug2("user_key_allowed: advance: '%s'", cp); 
       /* still no key? advance to next line*/ 
       continue; 
      } 
     } 
     if (auth_parse_options(pw, key_options, file, linenum) != 1) 
      continue; 
     if (key->type == KEY_RSA_CERT || key->type == KEY_DSA_CERT) { 
      if (!key_is_cert_authority) 
       continue; 
      if (!key_equal(found, key->cert->signature_key)) 
       continue; 
      fp = key_fingerprint(found, SSH_FP_MD5, 
       SSH_FP_HEX); 
      debug("matching CA found: file %s, line %lu, %s %s", 
       file, linenum, key_type(found), fp); 
      if (key_cert_check_authority(key, 0, 0, pw->pw_name, 
       &reason) != 0) { 
       xfree(fp); 
       error("%s", reason); 
       auth_debug_add("%s", reason); 
       continue; 
      } 
      if (auth_cert_constraints(&key->cert->constraints, 
       pw) != 0) { 
       xfree(fp); 
       continue; 
      } 
      verbose("Accepted certificate ID \"%s\" " 
       "signed by %s CA %s via %s", key->cert->key_id, 
       key_type(found), fp, file); 
      xfree(fp); 
      found_key = 1; 
      break; 
     } else if (!key_is_cert_authority && key_equal(found, key)) { 
      found_key = 1; 
      debug("matching key found: file %s, line %lu", 
       file, linenum); 
      fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); 
      verbose("Found matching %s key: %s", 
       key_type(found), fp); 
      xfree(fp); 
      break; 
     } 
    } 

它使用KEY_READ(發現,& CP)方法來創建密鑰,並將其保存到發現變量

這是key_read的來源:

 

key_read(Key *ret, char **cpp) 
{ 
    debuf("keyRead1"); 
    Key *k; 
    int success = -1; 
    char *cp, *space; 
    int len, n, type; 
    u_int bits; 
    u_char *blob; 

    cp = *cpp; 
//a switch statement whiche executes this code 
     space = strchr(cp, ' '); 
     if (space == NULL) { 
      debug3("key_read: missing whitespace"); 
      return -1; 
     } 

     *space = '\0';//this works for the line variable which contains the curent line but fails with my hard-coded key -> segfault 
     type = key_type_from_name(cp); 
     *space = ' '; 
     if (type == KEY_UNSPEC) { 
      debug3("key_read: missing keytype"); 
      return -1; 
     } 

現在即時通訊特林字符串創建一個關鍵

 
char *cp =NULL; 
    char *space; 
    char line[SSH_MAX_PUBKEY_BYTES]="ssh-rsa THEKEYCODE [email protected]\n"; 
//I have also tried char *cp ="ssh-rsa THEKEYCODE [email protected]\n"; 

cp=line; 
key_read(tkey,&cp); 


的問題是,我得到一個賽格故障時KEY_READ功能與\ 0替換空間(這是必要的密鑰類型檢測並與原來的執行工作)

這可能只是一個變量定義問題

最小(不)工作示例:

 
    char *cp =NULL; 
    char *space; 
    char line[1024]="ssh-rsa sdasdasdas [email protected]\n"; 

cp=line; 


    space = strchr(cp, ' '); 

     *space = '\0'; 


我應該使用什麼類型或初始化cp? 感謝

+0

您試圖硬連線接受的公鑰呢?你預計什麼時候加載?它看起來你希望它總是加載你的密鑰? – IanNorton

+0

你爲什麼說它不工作?你最後一段代碼對我來說看起來很好。 – ouah

+0

「學校作業」,呃? :-) –

回答

0

這運行良好,並預期對我來說:

#include<stdio.h> 

int main(){ 
char *cp =NULL; 
char *space; 
char line[1024]="ssh-rsa sdasdasdas [email protected]\n"; 
cp=line; 
space = strchr(cp, ' '); 
*space = '\0'; 
printf("%s",line); 
return 0; 
} 

Output: ssh-rsa 
+0

嗨,謝謝我不知道爲什麼,但現在它的作品,我已經花了至少3個小時。不管怎樣,謝謝 :) – sherif