关于软件设计中安全性的理解

在学习《软件构造》这门课之前,我其实就对软件的安全性有一定的理解。因为大一年度项目我们组就是完成的一个软件创新创业项目,所以我对于软件的安全性有一定的了解,但只能说浅尝辄止。在学习完《软件构造》这门课之后,我对于软件的安全性有了更好更深入的理解,从原理到实践,系统地学习了有关软件安全性的知识,接下来我会通过文字+代码的形式复习一下学习到的软件安全性的知识。
在很早的时候,大概是lab2的时候就已经开始有了对于软件安全性的要求,也学习了关于软件安全性的知识。还记得在lab1的时候基本都要参考学长学姐的代码,里面的参数有些是public类型的,有的是private类型的,有同学在上课的时候就询问了老师关于public和private的问题,老师说lab1暂时不提,等到lab2的时候就会有所要求。
在lab2中,我们需要实践学习过的关于ADT的知识,其中十分重要的一环就是对安全性的考虑。其中我们在写spec、AF、RI等信息的时候就需要写出对于安全性的考虑和应对措施。在lab2中最常见的就是对数据的类型要求以及防拷贝等方法。
首先是对数据类型的要求。在lab1中,我写的代码中的参数是这样的

public class FriendshipGraph {
	public final static int maxNum = 1000;
	public ArrayList<Person> people = new ArrayList<Person>();
	public int[][] edge = new int[maxNum][maxNum];
	public HashMap<Person, Integer> hm = new HashMap<Person, Integer>();
	boolean visited[] = new boolean[maxNum];
}

可以看到这些数据全部都是public类型的,而在lab2中有对于lab1中的P3有重新实现的惭怍,而这一次的参数变成了这样

public class FriendshipGraph {
	private final Graph<Person> graph = Graph.empty();
	
	// Abstraction function:
    //   TODO
	// 该图由之前的ConcreEdgesGraph来实现
	// 由于ConcreEdgesGraph创造的图是有向带权值的图,所以要求加边时要双向加边并将权值统一
	//
	
    // Representation invariant:
    //   TODO
	// graph的映射不能为空
	
    // Safety from rep exposure:
    //   TODO
    // graph为private类型
}

可以看到为了防止ADT中的数据产生泄露的可能性,我将参数全部改为了private类型,并且在很多场合使用了immutable类型

public class ChessGame implements Game {
	
	// TODO fields
	private ChessBoard board = new ChessBoard();
	private String name1;
	private String name2;
	private ChessPlayer player1;
	private ChessPlayer player2;
	private final static String white = "white";
	private final static String black = "black";
}

第二个很重要的技术就是防拷贝式编程,比如防止用户使用Object类中的toString()方法直接拷贝ADT中的重要信息,以及防止内部参数被外界直接调用导致内部信息被恶意篡改等。

// TODO toString()
		@Override
		public String toString() {
			return "This is a friednship graph!";
		}

例如在很多ADT中我都使用了防拷贝式编程。

@Override
	public Set<L> vertices() {
		// throw new RuntimeException("not implemented");
		Set<L> set = new HashSet<L>();
		for (int i = 0; i < vertices.size(); i++) {
			set.add(vertices.get(i).getLabel());
		}
		return set;
	}

例如给用户提供了调用内部可变数据的方法,但并不能直接将内部可变数据传输给用户,而是应该传输给用户和内部数据内容相同但是并不是内部的可变数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
{************************************************************** 浅谈软件安全设计(一) code by 黑夜彩虹 & vxin with almost pure delphi 网站:http://soft.eastrise.net 2007-03-07 --- 转载时请保留作者信息。 **************************************************************} 此CM的设计模式: 1、插入一些花指令 2、写了一些代码迷惑Cracker 3、有简单的Anti_DEDE 和检测调试器 话不多说,请看以下代码: unit main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,strutils; Const C1= 17856; C2= 23589; type TForm1 = class(TForm) Image1: TImage; Edit1: TEdit; Label1: TLabel; Label2: TLabel; Edit2: TEdit; Button1: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} Procedure Anti_DeDe();//检测DEDE反编译器 var DeDeHandle:THandle; i:integer; begin DeDeHandle:=FindWindow(nil,chr($64)+chr($65)+chr($64)+chr($65)); if DeDeHandle0 then begin For i:=1 to 4500 do SendMessage(DeDeHandle,WM_CLOSE,0,0); end; end; Function ABC42():Boolean; //检测调试器; var YInt,NInt:Integer; begin asm mov eax,fs:[30h] movzx eax,byte ptr[eax+2h] or al,al jz @No jnz @Yes @No: mov NInt,1 @Yes: Mov YInt,1 end; if YInt=1 then Result:=True; if NInt=1 then Result:=False; end; function EncryptModule(SourceStr:String;Key:Word;N:Integer):String; var //加密函数 I:Integer; begin SetLength(Result,Length(SourceStr));//利用SetLength函数指定密文长度 //对每一个索引元素进行变换 for I:=1 to Length(SourceStr) do begin Result[I]:=Char(byte(SourceStr[I]) xor (Key Shr N)); Key:= (byte(Result[I]) + Key)*C1+C2; end; end; //==========以下是549的函数,据说没有暴破点,顺便试一试 //========函数作用:动态改变程序运行罗辑 function GetEIP: Integer;//自动生成address的方法 asm mov eax, [esp]; sub eax, 5; //call GetEIP占用5字节 end; function PatchOneItem(PatchItem: String): Boolean; var PatchAddress: Integer; PatchLength: DWord; PatchData: Pointer; PatchDataStr: String; i: Integer; PatchByte: Byte; PID, PHandle: THandle; WriteCount: DWord; begin Result := False; if Length(PatchItem) < 11 then Exit; PatchAddress := StrToInt(\'0x\' + LeftStr(PatchItem, 8)); for i := 1 to Length(PatchItem) do begin if PatchItem[i] \' \' then PatchDataStr := PatchDataStr + PatchItem[i]; end; PatchLength := (Length(PatchDataStr) - 9) div 2; GetMem(PatchData, PatchLength); try for i := 0 to PatchLength - 1 do begin PatchByte := StrToInt(\'0x\'+PatchDataStr[10 + i * 2] + PatchDataStr[10 + i * 2 + 1]); Byte(Pointer(Integer(PatchData) + i)^) := PatchByte; end; GetWindowThreadProcessId(Application.Handle, PID); PHandle := OpenProcess(PROCESS_ALL_ACCESS, False, PID); WriteProcessMemory(PHandle, Pointer(PatchAddress), PatchData, PatchLength, WriteCount); CloseHandle(PHandle); finally FreeMem(PatchData, PatchLength); end; Result := PatchLength = WriteCount; end; procedure Patch(PatchFile: String); var PatchItems: TStrings; PatchIndex: Integer; begin if not FileExists(PatchFile) then Exit; PatchItems := TStringList.Create; try PatchItems.LoadFromFile(PatchFile); for PatchIndex := 0 to PatchItems.Count - 1 do begin PatchOneItem(PatchItems[PatchIndex]); end; finally PatchItems.Free; end; end; procedure TForm1.FormCreate(Sender: TObject); begin Anti_DeDe; //检测DEDE,检测到关闭它。 if ABC42 then ExitProcess(0); //检测调试器,终止。 end; procedure TForm1.Button1Click(Sender: TObject); //注册按纽,开始检测 begin //========在这里插入一些花指令 asm jz @Start jnz @Start db 0E8h, 24h, 0, 0 ; db 0, 8Bh, 44h, 24h db 4, 8Bh, 0, 3Dh db 4, 0, 0, 80h db 75h, 8, 8Bh, 64h db 24h, 8, 0EBh, 4 db 58h, 0EBh, 0Ch, 0E9h db 64h, 8Fh, 5, 0 db 0, 0, 0, 74h db 0F3h, 75h, 0F1h, 0EBh db 24h, 64h, 0FFh, 35h db 0, 0, 0, 0 db 0EBh, 12h, 0FFh, 9Ch db 74h, 3, 75h, 1 db 0E9h, 81h, 0Ch, 24h db 0, 1, 0, 0 db 9Dh, 90h, 0EBh, 0F4h db 64h, 89h, 25h, 0 db 0, 0, 0, 0EBh db 0E6h db 0EBh, 1, 0Fh, 31h ; db 0F0h, 0EBh, 0Ch, 33h db 0C8h, 0EBh, 3, 0EBh db 9, 0Fh, 59h, 74h db 5, 75h, 0F8h, 51h db 0EBh, 0F1h db 0B9h, 4, 0, 0 ; @Start: end; if length(edit2.Text)>3 then //比较大于3位 begin //============再写一些骗Cracker if edit2.Text=EncryptModule(Edit1.Text,12345,10) then begin showmessage(\'请重启本软件。\'); //=======还可以再写一些记号,这里我就不写了 end; PatchOneItem(edit2.Text); //真正的比较 showmessage(\'ok\'); //弹出OK对话框 end; end; end.  //====附件为CM,会破解的兄弟可以试试其强度....

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值