好吧,我正在對緩衝區溢出進行一些研究。我有一個C程序,容易受到我試圖轉換爲java的變量攻擊的影響。有人認爲他們可以幫助我嗎?到目前爲止,我仍然無法獲得Java代碼進行編譯。JAVA中的緩衝區溢出
C代碼
#include <stdio.h>
#include <string.h>
/*
A routine that checks whether the password is correct or not
Standard library call "gets()" does not check for buffer overflow
*/
int checkPassword(){
char passwordFlag = 'F';
char inputPwd[10];
memset(inputPwd, 0, 10);
gets(inputPwd);
if (!strcmp(inputPwd, "goodpass")){
passwordFlag = 'T';
}
if (passwordFlag == 'T'){
return 1;
}
else{
return 0;
}
}
int main()
{
printf("Please enter a password\n");
if (checkPassword() == 1)
{
printf("Successful\n");
return 0;
}
else{
printf("Access Denied.\n");
return -1;
}
}
的Java代碼(不是當前編譯)
import java.io.*;
class Numbers {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a password");
if (checkPassword() == 1)
{
System.out.println("Successful");
System.exit(1); //you wouldn't exit here but its not like i'm doing anything important
}
else{
System.out.println("Access Denied.");
System.exit(1);
}
}
public static Integer checkPassword(){
char passwordFlag = 'F';
char inputPwd[10];
memset(inputPwd, 0, 10);
gets(inputPwd);
if (!strcmp(inputPwd, "goodpass")){
passwordFlag = 'T';
}
if (passwordFlag == 'T'){
return 1;
}
else{
return 0;
}
}
}
在Java中,memset的,獲取並STRCMP是不確定的。 – Alanmars 2012-02-27 03:13:00
關於你的Java代碼有很多很多東西,它們不是Java。 – 2012-02-27 03:14:00
不要只取C庫函數名稱並在Java中使用它們。你需要學習Java方法來完成這些任務。 – Nayuki 2012-02-27 03:20:04