-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from Ostorlab/tls_http
Update http KB
- Loading branch information
Showing
3 changed files
with
479 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,237 @@ | ||
Mobile Applications must use Transport Layer Security (TLS) to provide encryption at the transport layer and ensure the | ||
confidentiality and integrity of data in transit. This application does not use SSL/TLS and is vulnerable to traffic | ||
interception and modification. | ||
Clear text HTTP traffic refers to data transmitted between a mobile app and its backend server without any encryption, making it easily readable and susceptible to interception by malicious actors. This lack of encryption poses significant security risks for mobile apps. When sensitive information, such as login credentials, personal data, or financial details, is sent over clear text HTTP connections, it becomes vulnerable to eavesdropping and man-in-the-middle attacks. | ||
|
||
An attacker performing a man-in-the-middle (MITM) attack may: | ||
One of the primary risks associated with clear text HTTP traffic in mobile apps is the potential exposure of user data. Attackers can intercept and extract valuable information, leading to identity theft, unauthorized access to accounts, and misuse of personal data. Moreover, attackers can exploit these vulnerabilities to tamper with the app's content or inject malicious code into the communication channel. | ||
|
||
* Passively intercept the communication to access any sensitive data in transit like usernames, passwords, or credit | ||
card number. | ||
* Actively inject or remove content to forge and omit information or inject malicious scripts | ||
* Actively redirect the communication to the attacker in the context of the initial trusted party | ||
Additionally, clear text HTTP traffic can compromise user privacy. Mobile apps often collect and transmit user data for analytics or targeted advertising purposes. Without encryption, this data is easily accessible to third parties, compromising users' confidentiality and potentially leading to privacy violations. | ||
|
||
Mobile apps that fail to implement secure communication protocols also face the risk of impersonation attacks. Attackers can create fake Wi-Fi networks or use other techniques to intercept traffic, pretending to be the legitimate backend server. Users unknowingly send their data to the attacker, who can then misuse it for malicious purposes. | ||
|
||
|
||
### Examples | ||
|
||
#### Dart | ||
|
||
```dart | ||
import 'dart:convert'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:http/http.dart' as http; | ||
void main() => runApp(LoginApp()); | ||
class LoginApp extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
title: 'Login App', | ||
home: LoginPage(), | ||
); | ||
} | ||
} | ||
class LoginPage extends StatefulWidget { | ||
@override | ||
_LoginPageState createState() => _LoginPageState(); | ||
} | ||
class _LoginPageState extends State<LoginPage> { | ||
final TextEditingController _usernameController = TextEditingController(); | ||
final TextEditingController _passwordController = TextEditingController(); | ||
String _loginStatus = ''; | ||
void _performLogin() async { | ||
final String username = _usernameController.text.trim(); | ||
final String password = _passwordController.text.trim(); | ||
// Simulate an HTTP request for login. | ||
final String apiUrl = 'http://example.com/login'; | ||
final Map<String, dynamic> requestBody = { | ||
'username': username, | ||
'password': password, | ||
}; | ||
final http.Response response = await http.post( | ||
Uri.parse(apiUrl), | ||
headers: {'Content-Type': 'application/json'}, | ||
body: json.encode(requestBody), | ||
); | ||
if (response.statusCode == 200) { | ||
// Successful login | ||
setState(() { | ||
_loginStatus = 'Login successful!'; | ||
}); | ||
} else { | ||
// Failed login | ||
setState(() { | ||
_loginStatus = 'Login failed. Please try again.'; | ||
}); | ||
} | ||
} | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text('Login Page'), | ||
), | ||
body: Padding( | ||
padding: const EdgeInsets.all(16.0), | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
crossAxisAlignment: CrossAxisAlignment.stretch, | ||
children: [ | ||
TextFormField( | ||
controller: _usernameController, | ||
decoration: InputDecoration(labelText: 'Username'), | ||
), | ||
TextFormField( | ||
controller: _passwordController, | ||
obscureText: true, | ||
decoration: InputDecoration(labelText: 'Password'), | ||
), | ||
SizedBox(height: 20), | ||
ElevatedButton( | ||
onPressed: _performLogin, | ||
child: Text('Login'), | ||
), | ||
SizedBox(height: 10), | ||
Text(_loginStatus, textAlign: TextAlign.center), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
``` | ||
|
||
#### Swift | ||
|
||
```swift | ||
import UIKit | ||
|
||
class LoginViewController: UIViewController { | ||
@IBOutlet weak var usernameTextField: UITextField! | ||
@IBOutlet weak var passwordTextField: UITextField! | ||
@IBOutlet weak var loginStatusLabel: UILabel! | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
} | ||
|
||
@IBAction func loginButtonTapped(_ sender: UIButton) { | ||
guard let username = usernameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), | ||
let password = passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) else { | ||
return | ||
} | ||
|
||
// Simulate an HTTP request for login. | ||
let apiUrl = "http://example.com/login" | ||
let requestBody: [String: Any] = [ | ||
"username": username, | ||
"password": password | ||
] | ||
|
||
var request = URLRequest(url: URL(string: apiUrl)!) | ||
request.httpMethod = "POST" | ||
request.setValue("application/json", forHTTPHeaderField: "Content-Type") | ||
|
||
do { | ||
request.httpBody = try JSONSerialization.data(withJSONObject: requestBody, options: []) | ||
} catch { | ||
print("Error creating JSON data: \(error)") | ||
return | ||
} | ||
|
||
let task = URLSession.shared.dataTask(with: request) { data, response, error in | ||
if let error = error { | ||
print("Error: \(error)") | ||
DispatchQueue.main.async { | ||
self.loginStatusLabel.text = "Login failed. Please try again." | ||
} | ||
return | ||
} | ||
|
||
guard let data = data, | ||
let httpResponse = response as? HTTPURLResponse, | ||
httpResponse.statusCode == 200 else { | ||
DispatchQueue.main.async { | ||
self.loginStatusLabel.text = "Login failed. Please try again." | ||
} | ||
return | ||
} | ||
|
||
DispatchQueue.main.async { | ||
self.loginStatusLabel.text = "Login successful!" | ||
} | ||
} | ||
|
||
task.resume() | ||
} | ||
} | ||
``` | ||
|
||
#### Kotlin | ||
|
||
```kotlin | ||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.appcompat.app.AppCompatActivity | ||
import kotlinx.android.synthetic.main.activity_login.* | ||
import org.json.JSONObject | ||
import java.io.OutputStreamWriter | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
|
||
class LoginActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_login) | ||
|
||
loginButton.setOnClickListener { | ||
val username = usernameEditText.text.trim().toString() | ||
val password = passwordEditText.text.trim().toString() | ||
|
||
// Simulate an HTTP request for login. | ||
val apiUrl = "https://example.com/login" | ||
val requestBody = JSONObject().apply { | ||
put("username", username) | ||
put("password", password) | ||
}.toString() | ||
|
||
Thread { | ||
performLogin(apiUrl, requestBody) | ||
}.start() | ||
} | ||
} | ||
|
||
private fun performLogin(apiUrl: String, requestBody: String) { | ||
try { | ||
val url = URL(apiUrl) | ||
val connection = url.openConnection() as HttpURLConnection | ||
connection.requestMethod = "POST" | ||
connection.setRequestProperty("Content-Type", "application/json") | ||
connection.doOutput = true | ||
|
||
val outputStreamWriter = OutputStreamWriter(connection.outputStream) | ||
outputStreamWriter.write(requestBody) | ||
outputStreamWriter.flush() | ||
|
||
val responseCode = connection.responseCode | ||
if (responseCode == HttpURLConnection.HTTP_OK) { | ||
runOnUiThread { | ||
loginStatusLabel.text = "Login successful!" | ||
} | ||
} else { | ||
runOnUiThread { | ||
loginStatusLabel.text = "Login failed. Please try again." | ||
} | ||
} | ||
} catch (e: Exception) { | ||
Log.e("LoginActivity", "Error: ${e.message}") | ||
runOnUiThread { | ||
loginStatusLabel.text = "Login failed. Please try again." | ||
} | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ | |
"V9_1_1" | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.