permissions := strconv.Atoi(os.Getenv("filePermissions"));
fMode := os.FileMode(permissions)
os.chmod(filePath, fMode);
...
String permissionMask = System.getProperty("defaultFileMask");
Path filePath = userFile.toPath();
...
Set<PosixFilePermission> perms = PosixFilePermissions.fromString(permissionMask);
Files.setPosixFilePermissions(filePath, perms);
...
$rName = $_GET['publicReport'];
chmod("/home/". authenticateUser . "/public_html/" . rName,"0755");
...
publicReport
,如「../../localuser/public_html/.htpasswd
」,應用程式會指定攻擊者可讀取那個檔案。
...
$mask = $CONFIG_TXT['perms'];
chmod($filename,$mask);
...
permissions = os.getenv("filePermissions");
os.chmod(filePath, permissions);
...
...
rName = req['publicReport']
File.chmod("/home/#{authenticatedUser}/public_html/#{rName}", "0755")
...
publicReport
,如「../../localuser/public_html/.htpasswd
」,應用程式會指定攻擊者可讀取那個檔案。
...
mask = config_params['perms']
File.chmod(filename, mask)
...
services-config.xml
描述符號檔案會指定「記錄」XML 元素以描述記錄的不同內容。內容將與下列相似:
<logging>
<target class="flex.messaging.log.ConsoleTarget" level="Debug">
<properties>
<prefix>[BlazeDS]</prefix>
<includeDate>false</includeDate>
<includeTime>false</includeTime>
<includeLevel>false</includeLevel>
<includeCategory>false</includeCategory>
</properties>
<filters>
<pattern>Endpoint.*</pattern>
<pattern>Service.*</pattern>
<pattern>Configuration</pattern>
</filters>
</target>
</logging>
target
標籤有一個名為 level
的選擇性屬性,由它來指示記錄層級。如果除錯層級設定的層級過於詳細,應用程式可能會將敏感資料寫入記錄檔案。sprintf()
、FormatMessageW()
或 syslog()
。snprintf()
將指令行引數複製到緩衝區內。
int main(int argc, char **argv){
char buf[128];
...
snprintf(buf,128,argv[1]);
}
%x
) 從堆疊讀取,然後函數會取得即將格式化的引數。(在此範例中,函數沒有取得即將格式化的引數。)透過使用 %n
格式化指令,攻擊者可能寫入堆疊,導致 snprintf()
將輸出的位元組數寫入到指定的引數 (而不是如預期從引數讀取值)。這種攻擊的一種繁瑣複雜的形式會使用四條交錯的輸入來完全控制堆疊中一個指標的數值。
printf("%d %d %1$d %1$d\n", 5, 9);
5 9 5 5
Example 1
中所提到的一樣。syslog()
函數有時候以如下形式使用:
...
syslog(LOG_ERR, cmdBuf);
...
syslog()
的第二個參數是個 Format String,所以任何包含在 cmdBuf
內的格式化指令都會被解譯,如 Example 1
中所述。syslog()
的正確使用方式:
...
syslog(LOG_ERR, "%s", cmdBuf);
...
sprintf()
、FormatMessageW()
、syslog()
、NSLog
或NSString.stringWithFormat
範例 1:以下程式碼在 NSString.stringWithFormat:
中使用指令行引數作為格式字串。
int main(int argc, char **argv){
char buf[128];
...
[NSString stringWithFormat:argv[1], argv[2] ];
}
%x
) 從堆疊讀取,然後函數會取得即將格式化的引數。(在此範例中,函數沒有取得即將格式化的引數。)
printf("%d %d %1$d %1$d\n", 5, 9);
5 9 5 5
Example 1
中所提到的一樣。syslog()
函數有時候以如下形式使用:
...
syslog(LOG_ERR, cmdBuf);
...
syslog()
的第二個參數是個 Format String,所以任何包含在 cmdBuf
內的格式化指令都會被解譯,如 Example 1
中所述。syslog()
的正確使用方式:範例 4:Apple 核心類別留下了危險的途徑,可藉此利用 Format String 弱點。
...
syslog(LOG_ERR, "%s", cmdBuf);
...
String.stringByAppendingFormat()
函數有時候以如下形式使用:
...
NSString test = @"Sample Text.";
test = [test stringByAppendingFormat:[MyClass
formatInput:inputControl.text]];
...
stringByAppendingFormat()
的正確使用方式:
...
NSString test = @"Sample Text.";
test = [test stringByAppendingFormat:@"%@", [MyClass
formatInput:inputControl.text]];
...
strncpy()
的範圍函數,使用不正確時也會引起弱點。大多數 Buffer overflow 弱點的根本原因,都是緩衝區的處理,加上對資料的大小或組成假設錯誤。
void wrongNumberArgs(char *s, float f, int d) {
char buf[1024];
sprintf(buf, "Wrong number of %.512s");
}
strncpy()
的範圍函數,使用不正確時也會引起弱點。大多數 Buffer overflow 弱點的根本原因,都是緩衝區的處理,加上對資料的大小或組成假設錯誤。%d
格式規範從浮點數轉換 f
。
void ArgTypeMismatch(float f, int d, char *s, wchar *ws) {
char buf[1024];
sprintf(buf, "Wrong type of %d", f);
...
}
script
標籤。
<script src="http://www.example.com/js/fancyWidget.js"></script>
www.example.com
以外的網站上,則該網站依賴 www.example.com
提供正確且無惡意的程式碼。如果攻擊者能危害 www.example.com
,他們就能修改 fancyWidget.js
的內容來破壞網站的安全性。例如,他們可以新增程式碼至 fancyWidget.js
來竊取使用者的機密資料。allowBackup
屬性設定為 true
(預設值) 並定義 <application>
標籤上的 backupAgent
屬性。Environment.getExternalStorageDirectory()
會將參照傳回 Android 裝置的外部儲存裝置。private void WriteToFile(String what_to_write) {
try{
File root = Environment.getExternalStorageDirectory();
if(root.canWrite()) {
File dir = new File(root + "write_to_the_SDcard");
File datafile = new File(dir, number + ".extension");
FileWriter datawriter = new FileWriter(datafile);
BufferedWriter out = new BufferedWriter(datawriter);
out.write(what_to_write);
out.close();
}
}
}
kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
:kSecAttrAccessibleAlways
:kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
:kSecAttrAccessibleAlwaysThisDeviceOnly
:kSecAttrAccessibleWhenUnlocked
:kSecAttrAccessibleWhenUnlockedThisDeviceOnly
:ThisDeviceOnly
的可存取性層級仍會備份至 iCloud 和 iTunes。這可能會導致隱私問題,具體視所儲存資料的敏感和隱私程度而定。
...
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSData *token = [@"secret" dataUsingEncoding:NSUTF8StringEncoding];
// Configure KeyChain Item
[dict setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id) kSecClass];
[dict setObject:token forKey:(__bridge id)kSecValueData];
...
[dict setObject:(__bridge id)kSecAttrAccessibleWhenUnlocked forKey:(__bridge id) kSecAttrAccessible];
OSStatus error = SecItemAdd((__bridge CFDictionaryRef)dict, NULL);
...
kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
:kSecAttrAccessibleAlways
:kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
:kSecAttrAccessibleAlwaysThisDeviceOnly
:kSecAttrAccessibleWhenUnlocked
:kSecAttrAccessibleWhenUnlockedThisDeviceOnly
:ThisDeviceOnly
的可存取性層級仍會備份至 iCloud 和 iTunes。這可能會導致隱私問題,具體視所儲存資料的敏感和隱私程度而定。
...
// Configure KeyChain Item
let token = "secret"
var query = [String : AnyObject]()
query[kSecClass as String] = kSecClassGenericPassword
query[kSecValueData as String] = token as AnyObject?
...
query[kSecAttrAccessible as String] = kSecAttrAccessibleWhenUnlocked
SecItemAdd(query as CFDictionary, nil)
...
protected void onCreate(Bundle savedInstanceState) {
...
try {
File httpCacheDir = new File(context.getExternalCacheDir(), "http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);
} catch (IOException e) {
Log.i(TAG, "HTTP response cache installation failed:" + e);
}
}
protected void onStop() {
...
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
}
{app ID}/Library/Caches/com.mycompany.myapp/Cache.db*
檔案中。{app ID}/Library/Caches/com.mycompany.myapp/Cache.db*
檔案中。{app ID}/Library/Caches/com.mycompany.myapp/Cache.db*
檔案中。 URLCache
類別的 diskCapacity
或 memoryCapacity
屬性設定為 0,可有效地停用 HTTP(S) 回應快取系統。但 NSURLCache
說明文件指出,僅當裝置的記憶體或裝置空間均不足時,磁碟上和記憶體內的快取才會被截斷成配置的大小。系統應該使用這兩項設定來釋放系統資源並提高效能,而非將其作為安全性控制。{app ID}/Library/Caches/com.mycompany.myapp/Cache.db*
檔案中。 URLCache
類別的 diskCapacity
或 memoryCapacity
屬性設定為 0,可有效地停用 HTTP(S) 回應快取系統。但 NSURLCache
說明文件指出,僅當裝置的記憶體或裝置空間均不足時,磁碟上和記憶體內的快取才會被截斷成配置的大小。系統應該使用這兩項設定來釋放系統資源並提高效能,而非將其作為安全性控制。NSFileManager
定義,而常數則應該被指派為與 NSFileManager
執行個體相關聯之 NSDictionary
中的 NSFileProtectionKey
金鑰值,以及可以透過使用 NSFileManager
函數 (包括 setAttributes:ofItemAtPath:error:
、attributesOfItemAtPath:error:
和 createFileAtPath:contents:attributes:
) 建立檔案或修改其資料保護類別。此外,相對應的資料保護常數會針對 NSData
物件定義為 NSDataWritingOptions
,以便可以做為 options
引數傳遞到 NSData
函數 writeToURL:options:error:
和 writeToFile:options:error:
。NSFileManager
和 NSData
的各種資料保護類別常數定義如下所示:NSFileProtectionComplete
, NSDataWritingFileProtectionComplete
:NSFileProtectionCompleteUnlessOpen
, NSDataWritingFileProtectionCompleteUnlessOpen
:NSFileProtectionCompleteUntilFirstUserAuthentication
, NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication
:NSFileProtectionNone
, NSDataWritingFileProtectionNone
:NSFileProtectionCompleteUnlessOpen
或 NSFileProtectionCompleteUntilFirstUserAuthentication
標記檔案將會使用衍生自使用者密碼和裝置 UID 的金鑰為其加密,但是在某些情況下資料還是保持可存取的狀態。因此,使用 NSFileProtectionCompleteUnlessOpen
或 NSFileProtectionCompleteUntilFirstUserAuthentication
時應該仔細進行檢閱,以確定是否保證提供進一步的 NSFileProtectionComplete
保護。範例 2:在以下範例中,只有在使用者開啟裝置並首次提供密碼後 (直到下次重新開機),所指定的資料才受到保護:
...
filepath = [self.GetDocumentDirectory stringByAppendingPathComponent:self.setFilename];
...
NSDictionary *protection = [NSDictionary dictionaryWithObject:NSFileProtectionCompleteUntilFirstUserAuthentication forKey:NSFileProtectionKey];
...
[[NSFileManager defaultManager] setAttributes:protection ofItemAtPath:filepath error:nil];
...
BOOL ok = [testToWrite writeToFile:filepath atomically:YES encoding:NSUnicodeStringEncoding error:&err];
...
...
filepath = [self.GetDocumentDirectory stringByAppendingPathComponent:self.setFilename];
...
NSData *textData = [textToWrite dataUsingEncoding:NSUnicodeStingEncoding];
...
BOOL ok = [textData writeToFile:filepath options:NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication error:&err];
...
NSFileManager
,而常數則應該被指派為與 NSFileManager
實例相關聯之 Dictionary
中的 NSFileProtectionKey
金鑰值,以及可以透過使用 NSFileManager
函數 (包括 setAttributes(_:ofItemAtPath:)
、attributesOfItemAtPath(_:)
和 createFileAtPath(_:contents:attributes:)
) 建立檔案或修改其資料保護類別。此外,相對應的資料保護常數會針對 NSDataWritingOptions
列舉中的 NSData
物件定義,以便可以做為 options
引數傳遞到 NSData
函數
writeToFile(_:options:)
。NSFileManager
和 NSData
的各種資料保護類別常數定義如下所示:NSFileProtectionComplete
, NSDataWritingOptions.DataWritingFileProtectionComplete
:NSFileProtectionCompleteUnlessOpen
, NSDataWritingOptions.DataWritingFileProtectionCompleteUnlessOpen
:NSFileProtectionCompleteUntilFirstUserAuthentication
, NSDataWritingOptions.DataWritingFileProtectionCompleteUntilFirstUserAuthentication
:NSFileProtectionNone
, NSDataWritingOptions.DataWritingFileProtectionNone
:NSFileProtectionCompleteUnlessOpen
或 NSFileProtectionCompleteUntilFirstUserAuthentication
標記檔案將會使用衍生自使用者密碼和裝置 UID 的金鑰為其加密,但是在某些情況下資料還是保持可存取的狀態。因此,使用 NSFileProtectionCompleteUnlessOpen
或 NSFileProtectionCompleteUntilFirstUserAuthentication
時應該仔細進行檢閱,以確定是否保證提供進一步的 NSFileProtectionComplete
保護。範例 2:在以下範例中,只有在使用者開啟裝置並首次提供密碼後 (直到下次重新開機),所指定的資料才受到保護:
...
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
let filename = "\(documentsPath)/tmp_activeTrans.txt"
let protection = [NSFileProtectionKey: NSFileProtectionCompleteUntilFirstUserAuthentication]
do {
try NSFileManager.defaultManager().setAttributes(protection, ofItemAtPath: filename)
} catch let error as NSError {
NSLog("Unable to change attributes: \(error.debugDescription)")
}
...
BOOL ok = textToWrite.writeToFile(filename, atomically:true)
...
...
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
let filename = "\(documentsPath)/tmp_activeTrans.txt"
...
BOOL ok = textData.writeToFile(filepath, options: .DataWritingFileProtectionCompleteUntilFirstUserAuthentication);
...
kSecAttrAccessible
金鑰的值。各個金鑰鏈可存取性常數的定義如下所示:kSecAttrAccessibleAfterFirstUnlock
:kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
:kSecAttrAccessibleAlways
:kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
:kSecAttrAccessibleAlwaysThisDeviceOnly
:kSecAttrAccessibleWhenUnlocked
:kSecAttrAccessibleWhenUnlockedThisDeviceOnly
:kSecAttrAccessibleAfterFirstUnlock
標記金鑰鏈項目將會使用衍生自使用者密碼和裝置 UID 的金鑰進行加密,但是在某些情況下資料仍然保持可存取狀態。因此,使用 kSecAttrAccessibleAfterFirstUnlock
時應該仔細進行檢閱,確定是否保證提供進一步的保護。
...
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSData *token = [@"secret" dataUsingEncoding:NSUTF8StringEncoding];
// Configure KeyChain Item
[dict setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id) kSecClass];
[dict setObject:token forKey:(__bridge id)kSecValueData];
...
[dict setObject:(__bridge id)kSecAttrAccessibleAfterFirstUnlock forKey:(__bridge id) kSecAttrAccessible];
OSStatus error = SecItemAdd((__bridge CFDictionaryRef)dict, NULL);
...
kSecAttrAccessible
金鑰的值。各個金鑰鏈可存取性常數的定義如下所示:kSecAttrAccessibleAfterFirstUnlock
:kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
:kSecAttrAccessibleAlways
:kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
:kSecAttrAccessibleAlwaysThisDeviceOnly
:kSecAttrAccessibleWhenUnlocked
:kSecAttrAccessibleWhenUnlockedThisDeviceOnly
:kSecAttrAccessibleAfterFirstUnlock
標記金鑰鏈項目將會使用衍生自使用者密碼和裝置 UID 的金鑰進行加密,但是在某些情況下資料仍然保持可存取狀態。因此,使用 kSecAttrAccessibleAfterFirstUnlock
時應該仔細進行檢閱,確定是否保證提供進一步的保護。
...
// Configure KeyChain Item
let token = "secret"
var query = [String : AnyObject]()
query[kSecClass as String] = kSecClassGenericPassword
query[kSecValueData as String] = token as AnyObject?
...
query[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
SecItemAdd(query as CFDictionary, nil)
...
NSFileManager
定義,而常數則應該被指派為與 NSFileManager
執行個體相關聯之 NSDictionary
中的 NSFileProtectionKey
金鑰值,以及可以透過使用 NSFileManager
函數 (包括 setAttributes:ofItemAtPath:error:
、attributesOfItemAtPath:error:
和 createFileAtPath:contents:attributes:
) 建立檔案或修改其資料保護類別。此外,相對應的資料保護常數會針對 NSData
物件定義為 NSDataWritingOptions
,以便可以做為 options
引數傳遞到 NSData
函數 writeToURL:options:error:
和 writeToFile:options:error:
。NSFileManager
和 NSData
的各種資料保護類別常數定義如下所示:NSFileProtectionComplete
, NSDataWritingFileProtectionComplete
:NSFileProtectionCompleteUnlessOpen
, NSDataWritingFileProtectionCompleteUnlessOpen
:NSFileProtectionCompleteUntilFirstUserAuthentication
, NSDataWritingFileProtectionCompleteUntilFirstUserAuthentication
:NSFileProtectionNone
, NSDataWritingFileProtectionNone
:NSFileProtectionNone
也會導致使用完全以裝置 UID 為基礎的衍生金鑰進行加密。如此一來,在裝置開機的任何時候 (包括以密碼鎖定時或正在開機時),此類檔案都會處於可存取的狀態。因此,使用 NSFileProtectionNone
時應該仔細進行檢閱,以確定是否保證使用較嚴格的資料保護類別提供進一步的保護。範例 2:在以下範例中,指定的資料未受保護 (在裝置開機的任何時候均可存取):
...
filepath = [self.GetDocumentDirectory stringByAppendingPathComponent:self.setFilename];
...
NSDictionary *protection = [NSDictionary dictionaryWithObject:NSFileProtectionNone forKey:NSFileProtectionKey];
...
[[NSFileManager defaultManager] setAttributes:protection ofItemAtPath:filepath error:nil];
...
BOOL ok = [testToWrite writeToFile:filepath atomically:YES encoding:NSUnicodeStringEncoding error:&err];
...
...
filepath = [self.GetDocumentDirectory stringByAppendingPathComponent:self.setFilename];
...
NSData *textData = [textToWrite dataUsingEncoding:NSUnicodeStingEncoding];
...
BOOL ok = [textData writeToFile:filepath options:NSDataWritingFileProtectionNone error:&err];
...
NSFileManager
,而常數則應該被指派為與 NSFileManager
實例相關聯之 Dictionary
中的 NSFileProtectionKey
金鑰值。可以透過使用 NSFileManager
函數 (包括 setAttributes(_:ofItemAtPath:)
、attributesOfItemAtPath(_:)
和 createFileAtPath(_:contents:attributes:)
) 建立檔案或修改其資料保護類別。此外,相對應的資料保護常數會針對 NSDataWritingOptions
列舉中的 NSData
物件定義,以便可以做為 options
引數傳遞到 NSData
函數,例如
writeToFile(_:options:)
。NSFileManager
和 NSData
的各種資料保護類別常數定義如下所示:NSFileProtectionComplete
, NSDataWritingOptions.DataWritingFileProtectionComplete
:NSFileProtectionCompleteUnlessOpen
, NSDataWritingOptions.DataWritingFileProtectionCompleteUnlessOpen
:NSFileProtectionCompleteUntilFirstUserAuthentication
, NSDataWritingOptions.DataWritingFileProtectionCompleteUntilFirstUserAuthentication
:NSFileProtectionNone
, NSDataWritingOptions.DataWritingFileProtectionNone
:NSFileProtectionNone
也會導致使用完全以裝置 UID 為基礎的衍生金鑰進行加密。如此一來,在裝置開機的任何時候 (包括以密碼鎖定時或正在開機時),此類檔案都會處於可存取的狀態。因此,使用 NSFileProtectionNone
時應該仔細進行檢閱,以確定是否保證使用較嚴格的資料保護類別提供進一步的保護。範例 2:在以下範例中,指定的資料未受保護 (只要裝置開機便可存取):
...
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
let filename = "\(documentsPath)/tmp_activeTrans.txt"
let protection = [NSFileProtectionKey: NSFileProtectionNone]
do {
try NSFileManager.defaultManager().setAttributes(protection, ofItemAtPath: filename)
} catch let error as NSError {
NSLog("Unable to change attributes: \(error.debugDescription)")
}
...
BOOL ok = textToWrite.writeToFile(filename, atomically:true)
...
...
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0])
let filename = "\(documentsPath)/tmp_activeTrans.txt"
...
BOOL ok = textData.writeToFile(filepath, options: .DataWritingFileProtectionNone);
...
kSecAttrAccessible
金鑰的值。各個金鑰鏈可存取性常數的定義如下所示:kSecAttrAccessibleAfterFirstUnlock
:kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
:kSecAttrAccessibleAlways
:kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
:kSecAttrAccessibleAlwaysThisDeviceOnly
:kSecAttrAccessibleWhenUnlocked
:kSecAttrAccessibleWhenUnlockedThisDeviceOnly
:kSecAttrAccessibleAlways
也會導致使用完全以裝置 UID 為基礎的衍生金鑰進行加密。如此一來,在裝置開機的任何時候 (包括以密碼鎖定時或正在開機時),此類檔案都會處於可存取的狀態。因此,使用 kSecAttrAccessibleAlways
時應該仔細進行檢閱,確定是否能保證具有較嚴格金鑰鏈可存取性層級的進一步保護。
...
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSData *token = [@"secret" dataUsingEncoding:NSUTF8StringEncoding];
// Configure KeyChain Item
[dict setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id) kSecClass];
[dict setObject:token forKey:(__bridge id)kSecValueData];
...
[dict setObject:(__bridge id)kSecAttrAccessibleAlways forKey:(__bridge id) kSecAttrAccessible];
OSStatus error = SecItemAdd((__bridge CFDictionaryRef)dict, NULL);
...
kSecAttrAccessible
金鑰的值。各個金鑰鏈可存取性常數的定義如下所示:kSecAttrAccessibleAfterFirstUnlock
:kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
:kSecAttrAccessibleAlways
:kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
:kSecAttrAccessibleAlwaysThisDeviceOnly
:kSecAttrAccessibleWhenUnlocked
:kSecAttrAccessibleWhenUnlockedThisDeviceOnly
:kSecAttrAccessibleAlways
也會導致使用完全以裝置 UID 為基礎的衍生金鑰進行加密。如此一來,在裝置開機的任何時候 (包括以密碼鎖定時或正在開機時),此類檔案都會處於可存取的狀態。因此,使用 kSecAttrAccessibleAlways
時應該仔細進行檢閱,確定是否能保證具有較嚴格金鑰鏈可存取性層級的進一步保護。
...
// Configure KeyChain Item
let token = "secret"
var query = [String : AnyObject]()
query[kSecClass as String] = kSecClassGenericPassword
query[kSecValueData as String] = token as AnyObject?
...
query[kSecAttrAccessible as String] = kSecAttrAccessibleAlways
SecItemAdd(query as CFDictionary, nil)
...
Realm
資料庫的連線:
Realm realm = Realm.getDefaultInstance();
Realm
資料庫的連線:
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
Realm
資料庫的連線:
let realm = try! Realm()
UIImageWriteToSavedPhotosAlbum
將影像儲存到相簿:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
...
}
UIImageWriteToSavedPhotosAlbum
將影像儲存到相簿:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
// Save image
UIImageWriteToSavedPhotosAlbum(pickedImage!, self, nil, nil)
dismissViewControllerAnimated(true, completion: nil)
}
kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
:kSecAttrAccessibleAlways
:kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
:kSecAttrAccessibleAlwaysThisDeviceOnly
:kSecAttrAccessibleWhenUnlocked
:kSecAttrAccessibleWhenUnlockedThisDeviceOnly
:kSecAttrAccessibleWhenUnlocked
) 進行儲存,如果您的裝置失竊且已設定密碼,竊賊必須先解除鎖定裝置,才能解密金鑰鏈項目。無法輸入正確的密碼將阻止竊賊解密金鑰鏈項目。但是,如果未設定密碼,攻擊者只需滑動手指解除鎖定裝置並讓金鑰鏈解密其項目即可。因此,未在裝置中強制使用密碼可能會削弱金鑰鏈加密機制。
...
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSData *token = [@"secret" dataUsingEncoding:NSUTF8StringEncoding];
// Configure KeyChain Item
[dict setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id) kSecClass];
[dict setObject:token forKey:(__bridge id)kSecValueData];
...
[dict setObject:(__bridge id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(__bridge id) kSecAttrAccessible];
OSStatus error = SecItemAdd((__bridge CFDictionaryRef)dict, NULL);
...
kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
:kSecAttrAccessibleAlways
:kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
:kSecAttrAccessibleAlwaysThisDeviceOnly
:kSecAttrAccessibleWhenUnlocked
:kSecAttrAccessibleWhenUnlockedThisDeviceOnly
:kSecAttrAccessibleWhenUnlocked
) 進行儲存,如果您的裝置失竊且已設定密碼,竊賊必須先解除鎖定裝置,才能解密金鑰鏈項目。無法輸入正確的密碼將阻止竊賊解密金鑰鏈項目。但是,如果未設定密碼,攻擊者只需滑動手指解除鎖定裝置並讓金鑰鏈解密其項目即可。因此,未在裝置中強制使用密碼可能會削弱金鑰鏈加密機制。
...
// Configure KeyChain Item
let token = "secret"
var query = [String : AnyObject]()
query[kSecClass as String] = kSecClassGenericPassword
query[kSecValueData as String] = token as AnyObject?
...
query[kSecAttrAccessible as String] = kSecAttrAccessibleWhenUnlockedThisDeviceOnly
SecItemAdd(query as CFDictionary, nil)
...